Jump to content
Tommi Prami

Does anyone have good routines for parsing ISO8601 date(time) strings

Recommended Posts

Posted (edited)

Yellow,

 

Seems that Delphis own routines are bit flaky. There has been bugs over years. Last problem I had was that TryISO8601ToDate will raise exception on string it can't parse. Will handle/eat the exception but not most optimal solution I think.

 

By good,m I mean that handle nicely error cases, maybe has more options that Delphi version. Obviously very well tested. If also fast, I wouldn't complain.


-Tee-

Edited by Tommi Prami

Share this post


Link to post

There's one in DWScript: https://github.com/EricGrange/DWScript/blob/c3a26f0e68a5010767681ea1f0cd49726cd4af5d/Source/dwsUtils.pas#L3169

I haven't used it though but knowing Eric, it's probably both fast and well tested. Pity about the 3-space indents though; Very French 🙂

 

Other than that: https://github.com/search?q=lang%3Apascal+iso8601&type=code

  • Like 1

Share this post


Link to post
1 hour ago, Tommi Prami said:

TryISO8601ToDate will raise exception on string it can't parse. Will handle/eat the exception but not most optimal solution I think

That is indeed annoying, but we haven't stumbled upon a "real" bug. We are using String/DateTime conversion for Iso8601 from the System.DateUtils all the time.

Share this post


Link to post

If the date doesn't follow spec, do you really want to attempt to import it?

Share this post


Link to post
6 hours ago, Tommi Prami said:

Last problem I had was that TryISO8601ToDate will raise exception on string it can't parse.

Disappointing, as other Tryxxx routines avoid raising exceptions.

  • Like 1

Share this post


Link to post
Posted (edited)
On 4/18/2024 at 1:32 AM, Tommi Prami said:

Last problem I had was that TryISO8601ToDate will raise exception on string it can't parse. Will handle/eat the exception but not most optimal solution I think.

By definition, a TryXXX() function does not raise an exception into user code.  TryISO8601ToDate() is no different.

 

Now, it may be that it generates an internal exception on failure, but that is merely an implementation detail, such an exception will certainly not escape into user code.  If you don't want to see the exception inside the debugger, you can configure it to ignore EDateTimeException and EConvertError exceptions.

 

Regarding that exception, TryISO8601ToDate() is implemented backwards than other TryXXX() functions.  In most RTL functions, DoX() calls TryDoX() to do the actual work, and then raises an exception if TryDoX() fails.  But TryISO8601ToDate() is different - it calls ISO8601ToDate() to do the actual work, and then suppresses any exception that ISO8601ToDate() raises.

 

What they SHOULD have done instead is move ISO8601ToDate()'s logic into TryISO8601ToDate(), and then make ISO8601ToDate() raise if TryISO8601ToDate() fails.  But, it turns out that ISO8601ToDate() raises a different exception message depending on why it fails.  Perhaps they could have created a new internal function do perform the actual work and have it report the failure reason to the caller, then TryISO8601ToDate() could ignore the reason, and ISO8601ToDate() could raise an exception based on the reason.  Oh well...

Edited by Remy Lebeau
  • Like 1

Share this post


Link to post
17 hours ago, Der schöne Günther said:

That is indeed annoying, but we haven't stumbled upon a "real" bug. We are using String/DateTime conversion for Iso8601 from the System.DateUtils all the time.

We had quite a long time own version, because some reported bug, Think there was some reported bug even quite recently.

 

-Tee-

Share this post


Link to post
5 hours ago, Remy Lebeau said:

By definition, a TryXXX() function does not raise an exception into user code.  TryISO8601ToDate() is no different.

 

No it does not, that is OK, but debugger breaks to it anyhow, as it raised in the TryISO8601ToDate, and that is kind of exception I would not like to ignore. IF it would be TryISO8601ToDateException then yes. But generalm date conversion exception I would like to raise my attention.

 

-tee- 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×