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

Dates and ISO 8601 #82

Closed
Boris-c opened this issue Aug 7, 2019 · 4 comments
Closed

Dates and ISO 8601 #82

Boris-c opened this issue Aug 7, 2019 · 4 comments
Labels
bug Something isn't working

Comments

@Boris-c
Copy link

Boris-c commented Aug 7, 2019

ISO 8601 states:
YYYY-MM-DDThh:mm:ss.s

where:
ss = two digits of second (00 through 59)
s = one or more digits representing a decimal fraction of a second

But in Hermes, new Date('2019-08-07T00:00:00.760738998Z') gives 2019-08-15T19:18:58.998Z because 760738998 is interpreted as 760738.998 seconds.

@Boris-c
Copy link
Author

Boris-c commented Aug 8, 2019

Just for clarity, a simpler example of the parsing bug:

new Date('2019-08-07T00:00:00.5Z') gives 2019-08-07T00:00:00.005Z

5 milliseconds is wrong, should be 500 milliseconds

@avp avp added the bug Something isn't working label Aug 9, 2019
@avp
Copy link
Contributor

avp commented Aug 9, 2019

Thanks for the bug report. We'll work on fixing this.

@Boris-c
Copy link
Author

Boris-c commented Aug 12, 2019

Just in case someone would like to find a JavaScript workaround for the bug, here is mine (I don't pretend it's universal, but it can be a starting point for others to wait for your fix instead of simply avoiding to use Hermes).

Just call that once at app startup:

if (__DEV__ || (new Date('2019-08-07T00:00:00.5Z')).getMilliseconds() === 5) {
  const millisDate = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).(\d+)Z$/
  const _Date = Date;

  function MockDate(y, m, d, h, M, s, ms) {
    let date = undefined;

    switch (arguments.length) {
      case 0: return new _Date();
      case 1:
        if (typeof y === 'string') {
          const matches = y.match(millisDate);
          if (matches) {
            const [_, yy, mm, dd, hh, MM, ss, mmss] = matches;
            const fixed = Math.floor(new Number('.' + mmss) * 1000);
            const fixedStr = `${fixed}`.padStart(3, '0');
            return new _Date(`${yy}-${mm}-${dd}T${hh}:${MM}:${ss}.${fixed}Z`);
          }
        }
        return new _Date(y);
      default: return new _Date(y, m, d, h, M, s, ms);
    }
  }

  Object.setPrototypeOf(MockDate, _Date);
  Date = MockDate;
}

@avp
Copy link
Contributor

avp commented Aug 12, 2019

Fixed in a084c4d

@avp avp closed this as completed Aug 12, 2019
mganandraj pushed a commit to mganandraj/hermes that referenced this issue Jun 22, 2022
Fix yarn security vulnerabilities
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants