Tommi Prami 130 Posted April 18 (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 April 18 by Tommi Prami Share this post Link to post
Anders Melander 1783 Posted April 18 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 1 Share this post Link to post
Der schöne Günther 316 Posted April 18 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
Lars Fosdal 1792 Posted April 18 If the date doesn't follow spec, do you really want to attempt to import it? Share this post Link to post
dummzeuch 1505 Posted April 18 I don't know whether it fits your definition of "good", but I have got: https://sourceforge.net/p/dzlib/code/HEAD/tree/dzlib/trunk/src/u_dzIso8601.pas and https://sourceforge.net/p/dzlib/code/HEAD/tree/dzlib/trunk/src/u_dzDateUtils.pas Share this post Link to post
Bill Meyer 337 Posted April 18 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. 1 Share this post Link to post
Remy Lebeau 1394 Posted April 18 (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 April 19 by Remy Lebeau 1 Share this post Link to post
Tommi Prami 130 Posted April 19 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
Tommi Prami 130 Posted April 19 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
Tommi Prami 130 Posted April 19 19 hours ago, Anders Melander said: 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 🙂 Have to check that out, thanks... Share this post Link to post