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

Converts from unix time (i.e. seconds from midnight, January 1st, 1970 in UTC) to a SysTime.

static SysTime fromUnixTime (
  long unixTime,
  immutable(TimeZone) tz = opCall()
) pure nothrow @safe;

The C standard does not specify the representation of time_t, so it is implementation defined. On POSIX systems, unix time is equivalent to time_t, but that's not necessarily true on other systems (e.g. it is not true for the Digital Mars C runtime). So, be careful when using unix time with C functions on non-POSIX systems.

Parameters

NameDescription
unixTime Seconds from midnight, January 1st, 1970 in UTC.
tz The time zone for the SysTime that's returned.

Example

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

assert(SysTime.fromUnixTime(0) ==
       SysTime(DateTime(1970, 1, 1), UTC()));

auto pst = new immutable SimpleTimeZone(hours(-8));
assert(SysTime.fromUnixTime(28800) ==
       SysTime(DateTime(1970, 1, 1), pst));

auto st1 = SysTime.fromUnixTime(1_198_311_285, UTC());
writeln(st1); // SysTime(DateTime(2007, 12, 22, 8, 14, 45), UTC())
assert(st1.timezone is UTC());
writeln(st1); // SysTime(DateTime(2007, 12, 22, 0, 14, 45), pst)

auto st2 = SysTime.fromUnixTime(1_198_311_285, pst);
writeln(st2); // SysTime(DateTime(2007, 12, 22, 8, 14, 45), UTC())
assert(st2.timezone is pst);
writeln(st2); // SysTime(DateTime(2007, 12, 22, 0, 14, 45), pst)

Authors

Jonathan M Davis

License

Boost License 1.0.