Epoch in D Programming Language
A common requirement in programs is getting the number of seconds, milliseconds, or nanoseconds since the Unix epoch. Here’s how to do it in D.
To run the program:
In D, we use the std.datetime
module to work with time. The Clock.currTime()
function returns the current time as a SysTime
object.
We can use toUnixTime()
to get the number of seconds since the Unix epoch. For milliseconds and nanoseconds, we combine toUnixTime()
with fracSecs
, which represents the fractional seconds.
To convert back from Unix time to SysTime
, we can use SysTime.fromUnixTime()
for seconds precision, or construct a SysTime
object directly for nanosecond precision.
Next, we’ll look at another time-related task: time parsing and formatting.