Fable: DateTime parsing

Created on 9 Jun 2018  Â·  14Comments  Â·  Source: fable-compiler/Fable

Description

This code is running over .Net but not Fable 2.

let dateString = "05/01/2008 +1:00"
let offsetDate = DateTimeOffset.Parse(dateString)
discussion

Most helpful comment

IMHO; the best way would be to only allow ISO dates YYYY-MM-DD (optionally with times). This could easily be enforced with a regex, and will always yield the same result. Will likely also prevent people from shooting themselves in the foot.

All 14 comments

Should be fixed in dotnet-fable 2.0.0-alpha-018

When executing:

System.DateTimeOffset.Parse("27/6/2018 -02:00")

I am getting an exception:

RangeError: Invalid time value
      at Date.toISOString (<anonymous>)
      at toStringWithOffset (tests/bin/fable-core.2.0.0-alpha-020/Date.js:125:14)
      at toString (tests/bin/fable-core.2.0.0-alpha-020/Date.js:167:32)
      at toString (tests/bin/fable-core.2.0.0-alpha-020/Util.js:178:57)
      at str2.replace (tests/bin/fable-core.2.0.0-alpha-020/String.js:182:34)
      at String.replace (<anonymous>)
      at formatOnce (tests/bin/fable-core.2.0.0-alpha-020/String.js:164:15)
      at args (tests/bin/fable-core.2.0.0-alpha-020/String.js:206:17)
      at /Users/maximemangel/Workspaces/Github/MangelMaxime/Thot/tests/bin/Tests.Json.Decode.js:266:52
      at /Users/maximemangel/Workspaces/Github/MangelMaxime/Thot/tests/bin/Util.js:18:5
      at Context.<anonymous> (tests/bin/Main.js:30:15)

I'm getting an error when parsing this in .NET too. Probably depends on the culture which unfortunately we cannot set up with Fable. If you try something like "1/6/2018 -02:00" it should work.

Using the format yyyy-MM-dd works too (what the HTML date input elements give).

Hum, does this mean we assume an English culture by default ?

X/Y/Z where

  • X -> month
  • Y -> day
  • Z -> year

TBH, I don't know. So far I was thinking it depended on the browser settings, but the MDN documentation says Date.parse is different depending on the environment and shouldn't be trusted :/ It was the easiest way to implement parsing for dates (we have now an extra regex for some edge cases) and I wanted to avoid adding dependencies to fable-core... Also, implementing proper .NET Globalization in JS would be a lot of work and we would also need to do it for numbers, etc.

No worries I am not asking for the Globalization, I just want the tests to pass in Thoth.Json.

For the DateTime, in the error I mention the fact that if the parser failed then we encourage them to use the format ISO 8601 which works on both side. Date manipulation is really messy... :)

IMHO; the best way would be to only allow ISO dates YYYY-MM-DD (optionally with times). This could easily be enforced with a regex, and will always yield the same result. Will likely also prevent people from shooting themselves in the foot.

Let's close this for now, as the first sample is working and the second doesn't work in .NET either. I know date parsing is not perfect but in order to improve it we would need much more code or to import a library, and I'm not sure if the gain will compensate the increase in bundle size.

If you find other examples that work on .NET but doesn't in JS (and don't depend on the culture) please feel free to reopen.

I really agree with Alxandr that anything except ISO should be explicitly disallowed. Anything else will cause people to have wrong expectations.

It's a tricky balance, for example in #1637 users are asking to not comply with ISO ;)

Yeah, its never easy.

My opinion would be to restrict to ISO dates here, because the rest isn't guaranteed to work. (Edit: __it being system specific is the big breaker__)

In the Guid case I would NOT follow ISO, because .NET doesn't, Guid.Empty breaks, and I don't think there are any failures from doing so.

I agree. Also, the guid parsing will not be system specific.

On Wed, Dec 12, 2018, 02:09 Lukas Rieger <[email protected] wrote:

Yeah, its never easy.

My opinion would be to restrict to ISO dates here, because the rest isn't
guaranteed to work.

In the Guid case I would NOT follow ISO, because .NET doesn't, Guid.Empty
breaks, and I don't think there are any failures from doing so.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/fable-compiler/Fable/issues/1422#issuecomment-446423190,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAG2zkLsIhITWhGJBuWnkPls_R1-2t2kks5u4Fc1gaJpZM4UhdSY
.

Closing in favor or #2126 which has a discussion about adding CultureInfo.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alfonsogarciacaro picture alfonsogarciacaro  Â·  3Comments

MangelMaxime picture MangelMaxime  Â·  3Comments

funlambda picture funlambda  Â·  4Comments

nozzlegear picture nozzlegear  Â·  3Comments

forki picture forki  Â·  3Comments