Epoch in Nim
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 Nim.
Use getTime()
to get the current time. Then, you can use toUnix()
, toUnixFloat()
to get elapsed time since the Unix epoch in seconds or as a floating-point number of seconds, respectively. To get milliseconds or nanoseconds, multiply the float result by 1000 or 1_000_000_000.
You can also convert integer seconds or floating-point seconds since the epoch into the corresponding Time
using fromUnix()
or fromUnixFloat()
.
When you run this program, you’ll see output similar to this:
Next, we’ll look at another time-related task: time parsing and formatting.