Skip to content

new Date() does not work as expected after time zone change #961

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

Closed
NickIliev opened this issue Feb 27, 2018 · 7 comments
Closed

new Date() does not work as expected after time zone change #961

NickIliev opened this issue Feb 27, 2018 · 7 comments

Comments

@NickIliev
Copy link

From @moserc on February 26, 2018 17:25

Issue

Date() does not return correct Timezone Offset unless the app is restarted in the new Timezone.

Creating the Issue

When I am running a Nativescript app in my local timezone, Date() correctly returns:

Mon Feb 26 2018 11:54:27 GMT-0500 (EST)

If I change to CST timezone without restarting the app, Date() returns the correct Timezone but incorrect Timezone Offset:

Mon Feb 26 2018 11:55:45 GMT-0500 (CST)

If I shut down the app and restart it (without changing from CST timezone), Date() correctly returns:

Mon Feb 26 2018 11:00:15 GMT-0600 (CST)

Can you please help us?

More info

Platform: Android
CLI: 3.2.1

-C

Copied from original issue: NativeScript/NativeScript#5469

@NickIliev
Copy link
Author

From @EddyVerbruggen on February 26, 2018 19:6

Googling this a little, I wouldn't be surprised if this is a bug in V8 rather than NativeScript.

@NickIliev
Copy link
Author

From @NathanaelA on February 26, 2018 21:32

An easy way to see would be to run the latest version of Chrome, and do the same JS test.
console.log("", new Date());
-- Change time zone
console.log("", new Date());

This would tell you if it is a bug in V8 vs a NativeScript issue.


Now to get you going; rather than use new Date() in NativeScript you can call the native Java Date method and get it directly from the OS; rather than from JavaScript/NativeScript.

@petekanev
Copy link
Contributor

@moserc I am suspecting that the V8 caches the device locale upon initialization, and is not notified of time zone changes. Perhaps there is no such mechanism in V8. Node.js and Chrome would likely inhibit similar behavior.

@cmoser-edge
Copy link

cmoser-edge commented Feb 28, 2018

@NickIliev your workaround is good for now but we are actually trying to use the momentjs library which seems to be broken in the same way......

@Pip3r4o We used Angular JS to test Date() with Chrome and it seems to work as expected both on my PC and Android device.

html:

<button (click)="refreshDate()" type="submit" >Refresh Date</button>
<label >{{date}}</label>

codebehind:

refreshDate() {
  this.date = Date();
}

Does this show V8 is working correctly?


"You can tell when someone's been a programmer for a while because they develop a deep-seated fear of time zone problems." --Randall Munroe, xkcd 1883

@petekanev
Copy link
Contributor

@moserc
After closer inspection, it seems that there is a notification mechanism in V8 - static void DateTimeConfigurationChangeNotification(Isolate* isolate);.

In Android you can have an intent filter for a time-zone change, however, you'd have to register a broadcast receiver whose implementation will invoke the v8 notification.

I suppose what we could help with from our end is to expose the V8 time zone change notification statically to Java, which NativeScript developers will be able to call at their own discretion.

Right now we can't commit to any ETA on when that will be available.

@petekanev petekanev changed the title Date() does not work as expected new Date() does not work as expected after time zone change Mar 5, 2018
@cmoser-edge
Copy link

@Pip3r4o In an effort to clarify, our app does not necessarily need to be notified of a timezone or DST change. However, we would like the correct timestamp to be available whenever we call Date(). It seems to me that Date() should work the same if called from a webpage on Chrome or if called from a Nativescript app, and it does not. It seems to work perfectly on a webpage.

Maybe this does not change anything since I do not know the underlying implementation of the Date() function on Nativescript but I wanted to make sure you understand the problem we are experiencing.

@petekanev
Copy link
Contributor

There are no underlying Nativescript specifics as far as the Date object is concerned - it comes straight from the V8 engine. I don’t see a straightforward way for us to implement the automatic time zone change notifier.

In the case of the Chrome browser - it’s coded in the Chrome implementation. Chrome being an app that embeds the V8 engine. Similarly, a NativeScript application will have to notify of the time zone change. The way to go is using a broadcast listener in Android, hence my proposal to expose the V8 notification API to Java.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants