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

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

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 hnescs 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 core.time.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.

By default, the return type is time_t (which is normally an alias for int on 32-bit systems and long on 64-bit systems), but if a different size is required than either int or long can be passed as a template argument to get the desired size.

If the return type is int, and the result can't fit in an int, then the closest value that can be held in 32 bits will be used (so int.max if it goes over and int.min if it goes under). However, no attempt is made to deal with integer overflow if the return type is long.

Prototype

T stdTimeToUnixTime(T)(
  long stdTime
) pure nothrow @safe
if (is(T == int) || is(T == long));

Parameters

NameDescription
T The return type (int or long). It defaults to time_t, which is normally 32 bits on a 32-bit system and 64 bits on a 64-bit system.
stdTime The std time to convert.

Returns

A signed integer representing the unix time which is equivalent to the given std time.

See Also

SysTime.toUnixTime

Example

// Midnight, January 1st, 1970 UTC
assert(stdTimeToUnixTime(621_355_968_000_000_000L) == 0);

// 2038-01-19 03:14:07 UTC
assert(stdTimeToUnixTime(642_830_804_470_000_000L) == int.max);

Authors

Jonathan M Davis and Kato Shoichi

License

Boost License 1.0.

Comments