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

Converts from unix time (which uses midnight, January 1st, 1970 UTC as its epoch and seconds as its units) to "std time" (which uses midnight, January 1st, 1 A.D. UTC and hnsecs as its units).

long unixTimeToStdTime (
  long unixTime
) 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.

"std time"'s epoch is based on the Proleptic Gregorian Calendar per ISO 8601 and is what SysTime uses internally. However, holding the time as an integer in hnsecs since that epoch technically isn't actually part of the standard, much as it's based on it, so the name "std time" isn't particularly good, but there isn't an official name for it. C# uses "ticks" for the same thing, but they aren't actually clock ticks, and the term "ticks" is used for actual clock ticks for MonoTime, so it didn't make sense to use the term ticks here. So, for better or worse, std.datetime uses the term "std time" for this.

Parameters

NameDescription
unixTime The unix time to convert.

See Also

SysTime.fromUnixTime

Example

import std.datetime.date : DateTime;
import std.datetime.timezone : UTC;

// Midnight, January 1st, 1970
writeln(unixTimeToStdTime(0)); // 621_355_968_000_000_000L
assert(SysTime(unixTimeToStdTime(0)) ==
       SysTime(DateTime(1970, 1, 1), UTC()));

writeln(unixTimeToStdTime(int.max)); // 642_830_804_470_000_000L
assert(SysTime(unixTimeToStdTime(int.max)) ==
       SysTime(DateTime(2038, 1, 19, 3, 14, 07), UTC()));

writeln(unixTimeToStdTime(-127_127)); // 621_354_696_730_000_000L
assert(SysTime(unixTimeToStdTime(-127_127)) ==
       SysTime(DateTime(1969, 12, 30, 12, 41, 13), UTC()));

Authors

Jonathan M Davis

License

Boost License 1.0.