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 PureScript.
To run this program, you would typically use the PureScript compiler (purs) and Node.js:
In this PureScript version:
We use the Effect.Now module to get the current time.
The unInstant function from Data.DateTime.Instant is used to extract the milliseconds since the Unix epoch.
We manually calculate seconds and nanoseconds from the milliseconds value.
PureScript doesn’t have built-in functions to convert seconds or nanoseconds back to a time type, so we just demonstrate the concept by showing the Seconds and Milliseconds newtypes.
Note that PureScript’s ecosystem might not have exact equivalents for all of Go’s time-related functions, so some adaptations were necessary. The core concept of working with Unix epoch times is preserved.
Next, we’ll look at another time-related task: time parsing and formatting.