SysTime.fromISOString

Russel Winder via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 13 04:06:57 PDT 2017


On Sat, 2017-05-13 at 01:52 -0700, Jonathan M Davis via Digitalmars-d-
learn wrote:
> […]
> 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).

My reading is that YYYY-MM is an allowed form: I was wrong to suggest
YYYY was allowed.

For times hh:mm is allowed as well as hh:mm:ss.

fromIsoString should allow for YYYYMMDDTHHMMSS and YYYY-MM-DDThh:mm:ss
shouldn't it?

> 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.

I agree I need to construct a Date or  DateTime from the string and
then a SysTime from that. I was just being lazy earlier.
 
> 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.

Indeed. However I believe the standard defines more than is accepted.
The code:

auto newDate = Date.fromISOString(date_buffer);

leads to:

core.time.TimeException@/usr/include/d/std/datetime.d(12932): Invalid ISO String: 2015-05-10

I'd suggest 2015-05-10 is a perfectly good ISO8601 compliant date and
so highlights a bug/

-- 
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/50c285a7/attachment.sig>


More information about the Digitalmars-d-learn mailing list