Epoch in Scheme
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 Scheme.
To run the program, save it to a file (e.g., epoch.scm
) and use your Scheme interpreter to execute it. The exact command may vary depending on your Scheme implementation.
Note that the exact output will depend on when you run the program.
In Scheme, we use current-second
to get the current time in seconds since the Unix epoch. For more precise measurements, we use current-jiffy
and jiffies-per-second
. The jiffy
is a unit of time in Scheme, but its exact duration can vary between implementations. We convert jiffies to milliseconds and nanoseconds by dividing by jiffies-per-second
and then multiplying by 1000 or 1000000000, respectively.
The seconds->date
function is used to convert seconds since the epoch to a date object, which is roughly equivalent to converting integer seconds to a time object in the original example.
Next, we’ll look at another time-related task: time parsing and formatting.