Skip to content

Commit 9eef368

Browse files
feat: support timeZoneOffsetInMinutes for date mode on android (#407)
* Fix for adding Android timeoffset * Fixd * Fixed the bad constant * new * Added mode data timeZoneOffsetInMinutes * New * New function timeZoneOffsetDateSetter * new semi * new local * New * new * Moved order * new * New * new * new * new * New * update * Upgraded detox 16.9.2 * New Co-authored-by: bbell <[email protected]>
1 parent ddf4a23 commit 9eef368

File tree

10 files changed

+76
-21
lines changed

10 files changed

+76
-21
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The module is still published on `npm` under the old namespace (as documented) b
1313
[![Lean Core Badge][lean-core-badge]][lean-core-issue]
1414

1515
React Native date & time picker component for iOS, Android and Windows.
16-
16+
1717
<table>
1818
<tr><td colspan=2><strong>iOS</strong></td></tr>
1919
<tr>
@@ -52,7 +52,7 @@ React Native date & time picker component for iOS, Android and Windows.
5252
- [`value` (`required`)](#value-required)
5353
- [`maximumDate` (`optional`)](#maximumdate-optional)
5454
- [`minimumDate` (`optional`)](#minimumdate-optional)
55-
- [`timeZoneOffsetInMinutes` (`optional`, `iOS or Android only`)](#timezoneoffsetinminutes-optional-ios-only)
55+
- [`timeZoneOffsetInMinutes` (`optional`, `iOS or Android only`)](#timezoneoffsetinminutes-optional-ios-and-android-only)
5656
- [`timeZoneOffsetInSeconds` (`optional`, `Windows only`)](#timezoneoffsetinsecond-optional-windows-only)
5757
- [`dayOfWeekFormat` (`optional`, `Windows only`)](#dayOfWeekFormat-optional-windows-only)
5858
- [`dateFormat` (`optional`, `Windows only`)](#dateFormat-optional-windows-only)

android/src/main/java/com/reactcommunity/rndatetimepicker/RNConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public final class RNConstants {
99
public static final String ARG_IS24HOUR = "is24Hour";
1010
public static final String ARG_DISPLAY = "display";
1111
public static final String ARG_NEUTRAL_BUTTON_LABEL = "neutralButtonLabel";
12-
public static final String ARG_TZOFFSET_MIN = "timeZoneOffsetInMinutes";
12+
public static final String ARG_TZOFFSET_MINS = "timeZoneOffsetInMinutes";
1313
public static final String ACTION_DATE_SET = "dateSetAction";
1414
public static final String ACTION_TIME_SET = "timeSetAction";
1515
public static final String ACTION_DISMISSED = "dismissedAction";

android/src/main/java/com/reactcommunity/rndatetimepicker/RNDate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ public class RNDate {
99

1010
public RNDate(Bundle args) {
1111
now = Calendar.getInstance();
12-
if (args != null && args.containsKey(RNConstants.ARG_TZOFFSET_MIN)) {
12+
if (args != null && args.containsKey(RNConstants.ARG_TZOFFSET_MINS)) {
1313
now.setTimeZone(TimeZone.getTimeZone("GMT"));
14-
Integer timeZoneOffsetInMinutes = args.getInt(RNConstants.ARG_TZOFFSET_MIN);
14+
Integer timeZoneOffsetInMinutes = args.getInt(RNConstants.ARG_TZOFFSET_MINS);
1515
now.add(Calendar.MILLISECOND, timeZoneOffsetInMinutes * 60000);
1616
}
1717

android/src/main/java/com/reactcommunity/rndatetimepicker/RNDatePickerDialogModule.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ private Bundle createFragmentArguments(ReadableMap options) {
171171
if (options.hasKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL) && !options.isNull(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
172172
args.putString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL, options.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL));
173173
}
174+
if (options.hasKey(RNConstants.ARG_TZOFFSET_MINS) && !options.isNull(RNConstants.ARG_TZOFFSET_MINS)) {
175+
args.putLong(RNConstants.ARG_TZOFFSET_MINS, (long) options.getDouble(RNConstants.ARG_TZOFFSET_MINS));
176+
}
174177
return args;
175178
}
176179
}

android/src/main/java/com/reactcommunity/rndatetimepicker/RNTimePickerDialogModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ private Bundle createFragmentArguments(ReadableMap options) {
148148
if (options.hasKey(RNConstants.ARG_INTERVAL) && !options.isNull(RNConstants.ARG_INTERVAL)) {
149149
args.putInt(RNConstants.ARG_INTERVAL, options.getInt(RNConstants.ARG_INTERVAL));
150150
}
151-
if (options.hasKey(RNConstants.ARG_TZOFFSET_MIN) && !options.isNull(RNConstants.ARG_TZOFFSET_MIN)) {
152-
args.putInt(RNConstants.ARG_TZOFFSET_MIN, options.getInt(RNConstants.ARG_TZOFFSET_MIN));
151+
if (options.hasKey(RNConstants.ARG_TZOFFSET_MINS) && !options.isNull(RNConstants.ARG_TZOFFSET_MINS)) {
152+
args.putInt(RNConstants.ARG_TZOFFSET_MINS, options.getInt(RNConstants.ARG_TZOFFSET_MINS));
153153
}
154154
return args;
155155
}

example/App.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,24 @@ export const App = () => {
224224
title="hide picker"
225225
/>
226226
</View>
227+
<View style={styles.button}>
228+
<Button
229+
testID="setTzZero"
230+
onPress={() => {
231+
setTzOffsetInMinutes(0);
232+
setShow(true);
233+
}}
234+
title="setTzOffsetInMinutes to 0"
235+
/>
236+
</View>
227237
<View style={styles.button}>
228238
<Button
229239
testID="setTz"
230240
onPress={() => {
231241
setTzOffsetInMinutes(60);
232242
setShow(true);
233243
}}
234-
title="setTz"
244+
title="setTzOffsetInMinutes to 60"
235245
/>
236246
</View>
237247
{show && (

example/e2e/detoxTest.spec.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,47 @@ describe('Example', () => {
130130
}
131131
});
132132

133-
async function userOpensPickerTz({mode, display, interval}) {
133+
async function userOpensPickerSetTimeZoneOffset({
134+
mode,
135+
display,
136+
interval,
137+
type,
138+
}) {
134139
await element(by.text(mode)).tap();
135140
await element(by.text(display)).tap();
136141
if (interval) {
137142
await element(by.text(String(interval))).tap();
138143
}
139-
await element(by.id('setTz')).tap();
144+
await element(by.id(type)).tap();
140145
}
141146

142-
it('setTz should change time text when time changes 60 minutes', async () => {
143-
await userOpensPickerTz({mode: 'time', display: 'default'});
147+
it('should update dateTimeText when date changes and set setTzOffsetInMinutes to 0', async () => {
148+
await userOpensPickerSetTimeZoneOffset({
149+
mode: 'date',
150+
display: 'default',
151+
type: 'setTzZero',
152+
});
153+
const dateText = getDateText();
154+
155+
if (isIOS()) {
156+
const testElement = getDateTimePickerIOS();
157+
await testElement.setColumnToValue(0, 'November');
158+
await testElement.setColumnToValue(1, '3');
159+
await testElement.setColumnToValue(2, '1800');
160+
161+
await expect(dateText).toHaveText('11/03/1800');
162+
} else {
163+
await userTapsOkButtonAndroid();
164+
await expect(dateText).toHaveText('08/21/2020');
165+
}
166+
});
167+
168+
it('setTz should change time text when setTzOffsetInMinutes is 60 minutes', async () => {
169+
await userOpensPickerSetTimeZoneOffset({
170+
mode: 'time',
171+
display: 'default',
172+
type: 'setTz',
173+
});
144174
const timeText = getTimeText();
145175

146176
if (isIOS()) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"@semantic-release/git": "^9.0.0",
6868
"@testing-library/react-native": "^7.0.1",
6969
"babel-jest": "^26.0.1",
70-
"detox": "^16.5.1",
70+
"detox": "16.9.2",
7171
"eslint": "^7.6.0",
7272
"eslint-plugin-prettier": "^3.1.2",
7373
"flow-bin": "^0.113.0",

src/datetimepicker.android.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,24 @@ function getPicker({
5959
minimumDate,
6060
maximumDate,
6161
neutralButtonLabel,
62+
timeZoneOffsetInMinutes,
6263
});
6364
}
6465
}
6566

67+
function timeZoneOffsetDateSetter(date, timeZoneOffsetInMinutes) {
68+
let localDate = date;
69+
if (
70+
typeof timeZoneOffsetInMinutes !== 'undefined' &&
71+
timeZoneOffsetInMinutes >= 0
72+
) {
73+
const offset =
74+
localDate.getTimezoneOffset() * MIN_MS + timeZoneOffsetInMinutes * MIN_MS;
75+
localDate = new Date(date.getTime() - offset);
76+
}
77+
return localDate;
78+
}
79+
6680
export default function RNDateTimePicker(props: AndroidNativeProps) {
6781
validateProps(props);
6882
const {
@@ -109,18 +123,16 @@ export default function RNDateTimePicker(props: AndroidNativeProps) {
109123

110124
switch (action) {
111125
case DATE_SET_ACTION:
112-
event.nativeEvent.timestamp = date.setFullYear(year, month, day);
126+
date.setFullYear(year, month, day);
127+
date.setHours(0, 0);
128+
date = timeZoneOffsetDateSetter(date, timeZoneOffsetInMinutes);
129+
event.nativeEvent.timestamp = date;
113130
onChange(event, date);
114131
break;
115132

116133
case TIME_SET_ACTION:
117134
date.setHours(hour, minute);
118-
if (timeZoneOffsetInMinutes !== undefined) {
119-
const offset =
120-
date.getTimezoneOffset() * MIN_MS +
121-
timeZoneOffsetInMinutes * MIN_MS;
122-
date = new Date(date.getTime() - offset);
123-
}
135+
date = timeZoneOffsetDateSetter(date, timeZoneOffsetInMinutes);
124136
event.nativeEvent.timestamp = date;
125137
onChange(event, date);
126138
break;

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3363,7 +3363,7 @@ detect-newline@^3.0.0:
33633363
resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-3.1.0.tgz#576f5dfc63ae1a192ff192d8ad3af6308991b651"
33643364
integrity sha512-TLz+x/vEXm/Y7P7wn1EJFNLxYpUD4TgMosxY6fAVJUnJMbupHBOncxyWUG9OpTaH9EBD7uFI5LfEgmMOc54DsA==
33653365

3366-
detox@^16.5.1:
3366+
detox@16.9.2:
33673367
version "16.9.2"
33683368
resolved "https://registry.yarnpkg.com/detox/-/detox-16.9.2.tgz#3b99be3df54ef0f35ffe12d3aa87b556d00a50d3"
33693369
integrity sha512-yi74zL3hHFRU131B5tgZiYh0hPWvpryntllAKEpxRGRRuz+11s2+TjpuS0M02jGOdDMFBk5BzcXFGM57FWbWNA==

0 commit comments

Comments
 (0)