SysTime.fromISOString

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 13 01:52:28 PDT 2017


On Saturday, May 13, 2017 07:33:35 Russel Winder via Digitalmars-d-learn 
wrote:
> A priori I thought 2015 was a perfectly valid ISO8601 date-time
> specification. It appears that SysTime.fromISOString disagrees:
>
> core.time.TimeException@/usr/include/d/std/datetime.d(8553): Invalid
> ISO String: 2015
>
> So am I right or wrong.
> https://en.wikipedia.org/wiki/ISO_8601https://en.wikipedia.org/wiki/ISO_8
> 601

The ISO representation of a date is YYYYMMDD, and the extended ISO
representation is YYYY-MM-DD. YYYY would be a truncated representation. The
standard has language such as "If, by agreement, truncated representations
are used" to talk about truncated representations, but it's not part of the
actual, standard string representation. It's just giving guidance on what
applications should do if they decide to not use the full string
representation when communicating with one another, and it's not expected
that an application that supports the standard would support truncated
representations. It's only "by agreement," between two applications, not
standard. std.datetime only supports the full, standard string
representation, and for SysTime, that also includes the time, not just the
date. std.datetime.Date would be the closest, but it represents a date, not
just a year, so it's YYYYMMDD or YYYY-MM-DD. If you want to pass a string to
SysTime.fromISOString, it's going to need to be YYYYMMDDTHHMMSS (optionally
with fractional seconds and a time zone).

And really, there isn't much useful that can be done with a SysTime that was
constructed from just a year anyway. The best SysTime could do would be to
assume you meant 2015-01-01T00:00:00. It operates on the system time in
hecto-nanoseconds and really isn't meant be operating on dates (that's what
Date and DateTime are for). Converting 2015 to a SysTime with no information
would be like trying to construct a time_t in C with just 2015. That usually
doesn't make much sense. Nothing in std.datetime operates simply on a year.
The closest that you're going to get to that is Date.

Regardless, if you know that you're just dealing with a year, and that's it,
you can just call to!int on it and pass it to the constructor of SysTime,
DateTime, or Date (depending on which you want to use) with whatever values
you want to set for the month, day, etc. Using a function like fromISOString
would just be overkill if all you have is a year, even if it did accept
truncated representations. But the truncated representations are not
required by the standard, so they're not supported.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list