Epoch in Pascal
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 Pascal.
In this Pascal program, we use the SysUtils
and DateUtils
units to work with date and time functions.
We use SysUtils.Now
to get the current date and time.
To get the Unix timestamp (seconds since epoch), we use the DateTimeToUnix
function.
For milliseconds, we multiply the Unix timestamp by 1000 and add the milliseconds of the current time.
Pascal doesn’t have built-in nanosecond precision, so we approximate it by multiplying the milliseconds by 1,000,000.
We can also convert Unix timestamps back to TDateTime
using the UnixToDateTime
function.
Here’s an example of what the output might look like:
Note that the exact values will depend on when you run the program.
Next, we’ll look at another time-related task: time parsing and formatting.