Skip to content
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

hotfix: fix time format issue #4108

Merged
merged 3 commits into from
Aug 25, 2021
Merged

Conversation

JunweiSUN
Copy link
Contributor

@JunweiSUN JunweiSUN commented Aug 24, 2021

We should not use Date().toLocaleString() to get a formatted time string since it uses the local time format on user's machine and the format is different across platforms (windows, linux, mac) even node versions. This will lead to a failure when starting the rest server. Instead, we construct the time format by ourselves (since using libraries like strftime require extra dependences) which should be consistent across different platforms.

BTW, not sure why we add a ' UTC' to a local time and convert it to a UTC time. This will lead to a incorrect time if you are not in +0000.

// `time.toLocaleString('sv')` trick does not work for Windows
const isoTime = new Date(new Date().toLocaleString() + ' UTC').toISOString();
const time = isoTime.slice(0, 10) + ' ' + isoTime.slice(11, 19);
const jsonTime = new Date().toJSON();
Copy link
Contributor

@cruiseliu cruiseliu Aug 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will print UTC time. In log we want local time.
The trick above has been tested in en_US and zh_CN locales, but there are hundreds of other locales over the world.
It would be nice if you can find a better method to do the converting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My fault. Didn't notice that toJSON() prints UTC time. But the previous trick seems didn't work on en_US windows machines and zh_CN mac machines. I have updated the pr to calculate a correct time format by our own.

const now = new Date();
const date = now.getFullYear() + '-' + zeroPad(now.getMonth() + 1) + '-' + zeroPad(now.getDate());
const time = zeroPad(now.getHours()) + ':' + zeroPad(now.getMinutes()) + ':' + zeroPad(now.getSeconds());
const datetime = date + ' ' + time;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really tedious. But I guess there's no better way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The only better way may be using a third-party strftime library.

@acured acured merged commit 862c67d into microsoft:master Aug 25, 2021
@liuzhe-lz
Copy link
Contributor

Have you tried new Date().toLocaleString("sv") on your machine? The last time with node.js 12.x and windows 10 it does not work for me. With node.js v16.3.0 it seems to work now.

@JunweiSUN
Copy link
Contributor Author

JunweiSUN commented Aug 25, 2021

Have you tried new Date().toLocaleString("sv") on your machine? The last time with node.js 12.x and windows 10 it does not work for me. With node.js v16.3.0 it seems to work now.

Yes, node 16.x could work while 12.x couldn't. Seems that the format of this function changes a lot across different versions. If we use this function, we must make sure to use the node under nni_node dir to start the server. Also I only test it on windows en_US (or maybe zh_CN? not sure) locale, not sure if the behavior will change on different platforms and locales.

@JunweiSUN
Copy link
Contributor Author

Shoule we create a hotfix version wheel (like 2.4.1?) to pip to avoid affecting more users? @liuzhe-lz

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

Successfully merging this pull request may close these issues.

4 participants