View source code
Display the source code in std/datetime/systime.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.datetime.systime.SysTime.fromISOString

Creates a SysTime from a string with the format YYYYMMDDTHHMMSS.FFFFFFFTZ (where F is fractional seconds and TZ is the time zone). Whitespace is stripped from the given string.

SysTime fromISOString(S) (
  scope const S isoString,
  immutable TimeZone tz = null
) @safe
if (isSomeString!S);

The exact format is exactly as described in toISOString except that trailing zeroes are permitted - including having fractional seconds with all zeroes. The time zone and fractional seconds are optional, however, a decimal point with nothing following it is invalid. Also, while toISOString will never generate a string with more than 7 digits in the fractional seconds (because that's the limit with hecto-nanosecond precision), it will allow more than 7 digits in order to read strings from other sources that have higher precision (however, any digits beyond 7 will be truncated).

If there is no time zone in the string, then LocalTime is used. If the time zone is "Z", then UTC is used. Otherwise, a SimpleTimeZone which corresponds to the given offset from UTC is used. To get the returned SysTime to be a particular time zone, pass in that time zone and the SysTime to be returned will be converted to that time zone (though it will still be read in as whatever time zone is in its string).

The accepted formats for time zone offsets are +HH, -HH, +HHMM, and -HHMM.

Warning: Previously, toISOString did the same as toISOExtString and generated +HH:MM or -HH:MM for the time zone when it was not LocalTime or UTC, which is not in conformance with ISO 8601 for the non-extended string format. This has now been fixed. However, for now, fromISOString will continue to accept the extended format for the time zone so that any code which has been writing out the result of toISOString to read in later will continue to work. The current behavior will be kept until July 2019 at which point, fromISOString will be fixed to be standards compliant.

Parameters

NameDescription
isoString A string formatted in the ISO format for dates and times.
tz The time zone to convert the given time to (no conversion occurs if null).

Throws

DateTimeException if the given string is not in the ISO format or if the resulting SysTime would not be valid.

Example

import core.time : hours, msecs, usecs, hnsecs;
import std.datetime.date : DateTime;
import std.datetime.timezone : SimpleTimeZone, UTC;

assert(SysTime.fromISOString("20100704T070612") ==
       SysTime(DateTime(2010, 7, 4, 7, 6, 12)));

assert(SysTime.fromISOString("19981225T021500.007") ==
       SysTime(DateTime(1998, 12, 25, 2, 15, 0), msecs(7)));

assert(SysTime.fromISOString("00000105T230959.00002") ==
       SysTime(DateTime(0, 1, 5, 23, 9, 59), usecs(20)));

assert(SysTime.fromISOString("20130207T043937.000050392") ==
       SysTime(DateTime(2013, 2, 7, 4, 39, 37), hnsecs(503)));

assert(SysTime.fromISOString("-00040105T000002") ==
       SysTime(DateTime(-4, 1, 5, 0, 0, 2)));

assert(SysTime.fromISOString(" 20100704T070612 ") ==
       SysTime(DateTime(2010, 7, 4, 7, 6, 12)));

assert(SysTime.fromISOString("20100704T070612Z") ==
       SysTime(DateTime(2010, 7, 4, 7, 6, 12), UTC()));

assert(SysTime.fromISOString("20100704T070612-0800") ==
       SysTime(DateTime(2010, 7, 4, 7, 6, 12),
               new immutable SimpleTimeZone(hours(-8))));

assert(SysTime.fromISOString("20100704T070612+0800") ==
       SysTime(DateTime(2010, 7, 4, 7, 6, 12),
               new immutable SimpleTimeZone(hours(8))));

Authors

Jonathan M Davis

License

Boost License 1.0.