Skip to content

Instant parsing not working correctly on JS #67

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
jakubgwozdz opened this issue Nov 3, 2020 · 3 comments
Closed

Instant parsing not working correctly on JS #67

jakubgwozdz opened this issue Nov 3, 2020 · 3 comments
Labels
duplicate This issue or pull request already exists

Comments

@jakubgwozdz
Copy link

Code like "2019-10-28T14:00:00+01:00".toInstant() works ok under JVM, but unfortunately not under JS.
This is caused by js-joda problem where Instant cannot use +01:00 offsets (only Zworks here) and causes

> var Instant = require("@js-joda/core").Instant;
undefined
> Instant.parse('2019-10-28T14:00:00+01:00')
Uncaught:
DateTimeParseException: Text '2019-10-28T14:00:00+01:00' could not be parsed at index 19: 2019-10-28T14:00:00+01:00, at index: 19
    at DateTimeFormatter._parseToBuilder (/Users/jakub.gwozdz/tmp/node_modules/@js-joda/core/dist/js-joda.js:6972:19)
    at DateTimeFormatter.parse2 (/Users/jakub.gwozdz/tmp/node_modules/@js-joda/core/dist/js-joda.js:6933:30)
    at DateTimeFormatter.parse (/Users/jakub.gwozdz/tmp/node_modules/@js-joda/core/dist/js-joda.js:6910:23)
    at Function.parse (/Users/jakub.gwozdz/tmp/node_modules/@js-joda/core/dist/js-joda.js:12368:46) {
  parsedString: [Function],
  errorIndex: [Function],
  toString: [Function]
}

solution would be going through ZonedDateTime:

> ZonedDateTime.parse('2019-10-28T14:00:00+01:00').toInstant()
Instant { _seconds: 1572267600, _nanos: 0 }
@ilya-g
Copy link
Member

ilya-g commented Nov 9, 2020

Relates to #56.
I believe relying on ZonedDateTime.parse can allow more input strings than necessary, though a format can be used to limit allowed formats.

@jakubgwozdz
Copy link
Author

Well my use case is in using ktor-client->kotlinx.serialization (in multiplatform, js-browser included). And since the 3rd party rest api endpoint returns datetimes in 2019-10-28T14:00:00+01:00 ISO format, I cannot really use Instant class here.

@ilya-g ilya-g added the duplicate This issue or pull request already exists label Jan 17, 2021
@ilya-g
Copy link
Member

ilya-g commented Jan 17, 2021

I'm closing this as a duplicate of #56 because essentially it's the same issue.

@ilya-g ilya-g closed this as completed Jan 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants