SysTime.fromISOString
Russel Winder via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat May 13 06:49:07 PDT 2017
On Sat, 2017-05-13 at 05:03 -0700, Jonathan M Davis via Digitalmars-d-
learn wrote:
>
[…]
> SysTime st;
> try
> {
> // If it's YYYY-MM-DDTHH:MM:SS.FFFFFFFZ
> // If it's YYYY-MM-DDTHH:MM:SS.FFFFFFZ
> // If it's YYYY-MM-DDTHH:MM:SS.FFFFFZ
> // If it's YYYY-MM-DDTHH:MM:SS.FFFFZ
> // If it's YYYY-MM-DDTHH:MM:SS.FFFZ
> // If it's YYYY-MM-DDTHH:MM:SS.FFZ
> // If it's YYYY-MM-DDTHH:MM:SS.FZ
> // or YYYY-MM-DDTHH:MM:SSZ
> // or YYYY-MM-DDTHH:MM:SS.FFFFFFF
> // or YYYY-MM-DDTHH:MM:SS.FFFFFF
> // or YYYY-MM-DDTHH:MM:SS.FFFFF
> // or YYYY-MM-DDTHH:MM:SS.FFFF
> // or YYYY-MM-DDTHH:MM:SS.FFF
> // or YYYY-MM-DDTHH:MM:SS.FF
> // or YYYY-MM-DDTHH:MM:SS.F
> // or YYYY-MM-DDTHH:MM:SS
> st = SysTime.fromISOExtString(str);
> }
> catch(DateTimeException)
> {
> try
> {
> // If it's just YYYY-MM-DD
> st = SysTime(Date.fromISOExtString(str));
> }
A variation on this is where I have gone, so that you are not telling
me I am doing it wrong, means it is probably the least worst solution.
I do a cascade of attempting to construct using SysTime.fromISOString,
SysTime.fromISOExtString, DateTime.fromISOString,
DateTime.fromISOExtString, Date.fromISOString, Date.fromISAExtString
(creating a SysTime in the latter four cases obviously).
> catch(DateTimeException)
> {
> // If it's just HH:MM:SS
> st = SysTime(Date.init, TimeOfDay.fromISOExtString(str));
> }
> }
I haven't got to this stage as yet, but I suspect I am going to have
to.
> But that would be downright bizarre in the case where it was just
> HH:MM:SS,
> because then you'd end up with a SysTime on January 1st, 1 A.D. And
> while
> getting just the date is less awkward, it's still kind off to then
> operate
> on that as a SysTime. The time would always be at midnight.
I think you have to use the current date/time and extract the Date to
initialise. Times have to be in extended format to avoid the YYYY HHMM
conflict you pointed out.
> Really, just parsing an "ISO formatted string" and allowing either
> the date
> or time to be optional doesn't make much sense to me. That implies
> that you
> don't really know what it's for, since the meaning of something that
> is just
> a date and the meaning of something that is just a time are
> completely
> different.
I disagree. There is now and some temporal location in the past.
Separating out durations makes that easy. Using a time means sometime
today before now or is an error. Using a date/time means before today
or is an error. This is not a general case, it is a specific need for
the use of two date/times, and it seems all viable to me. Actually I
can probably separate date/times and times as well as separating out
durations before now to make things easier.
> Regardless, std.datetime was designed with the idea that you knew
> whether
> you were dealing with a date, a time, or both a date and a time when
> dealing
> with ISO formatted strings.
I'll get round it. Now I have "from the horses mouth" the guidance
around my questions, I am fairly confident of where to go to achieve
the goal. It will not be pretty, but it will be doable without having
to create a magic parsing function.
Thanks for your exchanges on this, most constructive and most helpful.
--
Russel.
=============================================================================
Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder at ekiga.net
41 Buckmaster Road m: +44 7770 465 077 xmpp: russel at winder.org.uk
London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20170513/4f793091/attachment.sig>
More information about the Digitalmars-d-learn
mailing list