View source code Display the source code in std/datetime.d from which this page was generated on github. 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. Page wiki View or edit the community-maintained wiki page associated with this page.

Function std.datetime.SysTime.fromSimpleString

Creates a SysTime from a string with the format YYYY-MM-DD HH:MM:SS.FFFFFFFTZ (where F is fractional seconds is the time zone). Whitespace is stripped from the given string.

The exact format is exactly as described in toSimpleString except that trailing zeroes are permitted - including having fractional seconds with all zeroes. However, a decimal point with nothing following it is invalid.

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 +H, -H, +HH, -HH, +H:MM, -H:MM, +HH:MM, and -HH:MM.

Prototype

SysTime fromSimpleString(S)(
  S simpleString,
  TimeZone tz = null
) @safe
if (isSomeString!S);

Parameters

NameDescription
simpleString A string formatted in the way that toSimpleString formats 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

assert(SysTime.fromSimpleString("2010-Jul-04 07:06:12") ==
       SysTime(DateTime(2010, 7, 4, 7, 6, 12)));
assert(SysTime.fromSimpleString("1998-Dec-25 02:15:00.007") ==
       SysTime(DateTime(1998, 12, 25, 2, 15, 0), msecs(7)));
assert(SysTime.fromSimpleString("0000-Jan-05 23:09:59.00002") ==
       SysTime(DateTime(0, 1, 5, 23, 9, 59), usecs(20)));
assert(SysTime.fromSimpleString("-0004-Jan-05 00:00:02") ==
       SysTime(DateTime(-4, 1, 5, 0, 0, 2)));
assert(SysTime.fromSimpleString(" 2010-Jul-04 07:06:12 ") ==
       SysTime(DateTime(2010, 7, 4, 7, 6, 12)));

assert(SysTime.fromSimpleString("2010-Jul-04 07:06:12Z") ==
       SysTime(DateTime(2010, 7, 4, 7, 6, 12), UTC()));
assert(SysTime.fromSimpleString("2010-Jul-04 07:06:12-8:00") ==
       SysTime(DateTime(2010, 7, 4, 7, 6, 12),
               new immutable SimpleTimeZone(dur!"hours"(-8))));
assert(SysTime.fromSimpleString("2010-Jul-04 07:06:12+8:00") ==
       SysTime(DateTime(2010, 7, 4, 7, 6, 12),
               new immutable SimpleTimeZone(dur!"hours"(8))));


Authors

Jonathan M Davis and Kato Shoichi

License

Boost License 1.0.

Comments