Skip to content

add Android timezone offset support #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.reactcommunity.rndatetimepicker;

import java.util.Calendar;
import java.util.TimeZone;
import android.os.Bundle;

public class RNDate {
private Calendar now;

public RNDate(Bundle args) {
now = Calendar.getInstance();
if (args != null && args.containsKey("timeZoneOffsetInMinutes")) {
now.setTimeZone(TimeZone.getTimeZone("GMT"));
Integer timeZoneOffsetInMinutes = args.getInt("timeZoneOffsetInMinutes");
now.add(Calendar.MILLISECOND, timeZoneOffsetInMinutes * 60000);
}

if (args != null && args.containsKey(RNConstants.ARG_VALUE)) {
set(args.getLong(RNConstants.ARG_VALUE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ private Bundle createFragmentArguments(ReadableMap options) {
if (options.hasKey(RNConstants.ARG_INTERVAL) && !options.isNull(RNConstants.ARG_INTERVAL)) {
args.putInt(RNConstants.ARG_INTERVAL, options.getInt(RNConstants.ARG_INTERVAL));
}
if (options.hasKey("timeZoneOffsetInMinutes") && !options.isNull("timeZoneOffsetInMinutes")) {
args.putInt("timeZoneOffsetInMinutes", options.getInt("timeZoneOffsetInMinutes"));
}
return args;
}
}
15 changes: 13 additions & 2 deletions src/datetimepicker.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function getPicker({
maximumDate,
neutralButtonLabel,
minuteInterval,
timeZoneOffsetInMinutes,
}) {
switch (mode) {
case MODE_TIME:
Expand All @@ -47,6 +48,7 @@ function getPicker({
minuteInterval,
is24Hour,
neutralButtonLabel,
timeZoneOffsetInMinutes,
});
case MODE_DATE:
default:
Expand All @@ -72,6 +74,7 @@ export default function RNDateTimePicker(props: AndroidNativeProps) {
maximumDate,
neutralButtonLabel,
minuteInterval,
timeZoneOffsetInMinutes,
} = props;
const valueTimestamp = value.getTime();

Expand All @@ -92,11 +95,12 @@ export default function RNDateTimePicker(props: AndroidNativeProps) {
maximumDate,
neutralButtonLabel,
minuteInterval,
timeZoneOffsetInMinutes,
});

picker.then(
function resolve({action, day, month, year, minute, hour}) {
const date = new Date(valueTimestamp);
let date = new Date(valueTimestamp);
const event: AndroidEvent = {
type: 'set',
nativeEvent: {},
Expand All @@ -109,7 +113,14 @@ export default function RNDateTimePicker(props: AndroidNativeProps) {
break;

case TIME_SET_ACTION:
event.nativeEvent.timestamp = date.setHours(hour, minute);
date.setHours(hour, minute);
if (timeZoneOffsetInMinutes !== undefined) {
const offset =
date.getTimezoneOffset() * 60000 +
timeZoneOffsetInMinutes * 60000;
date = new Date(date.getTime() - offset);
}
event.nativeEvent.timestamp = date;
onChange(event, date);
break;

Expand Down
1 change: 1 addition & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export type AndroidNativeProps = $ReadOnly<{|
*/
display: Display,

timeZoneOffsetInMinutes?: ?number,
/**
* The interval at which minutes can be selected.
*/
Expand Down