Skip to content

The time library

Tim Hardcastle edited this page Feb 29, 2024 · 5 revisions

The time library wraps around the Go time library.

The data types are somewhat different. Durations are simply given as integers; the Time type is an ordinary struct; and the Weekday enum has its elements in SCREAMING_SNAKE_CASE:

Time = struct(year, month, day, hour, min, sec, nsec int, loc string)

Weekday = enum SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY

The API varies from the Go time library in the usual systematic ways: there are no methods, these being replaced by functions; the names of functions are in camelCase. So t.YearDay() becomes yearDay(t).

Besides this, the various methods and functions competing to be called UnixMilli, etc, are now more verbosely called timeToUnixMilli and unixMilliToTime, etc, to make the direction of conversion explicit. The In method has been renamed timeIn.

This gives us functions with the following signatures (all duration parameters being expressed in nanoseconds):

  • add (t Time, duration int) -> Time
  • addDate(t Time, years, months, days int) -> Time
  • after(t, u Time) -> bool
  • before(t, u Time) -> bool
  • compare(t, u Time) -> int
  • equal(t, u Time) -> bool
  • format(t Time, layout string) -> string
  • isDst(t Time) -> bool
  • isoWeek(t Time) -> int
  • isZero(t Time) -> bool
  • local(t Time) -> Time
  • parse(layout, value string) -> Time
  • parseDuration(s string) -> int
  • round(t Time, duration int) -> Time
  • sub(t, u Time) -> int
  • timeIn(t Time, location string) -> Time
  • timeToUnix(t Time) -> int
  • timeToUnixMicro(t Time) -> int
  • timeToUnixMilli(t Time) -> int
  • timeToUnixNano(t Time) -> int
  • truncate(t Time, duration int) -> Time
  • unixToTime(sec, usec int) -> Time
  • unixMicroToTime(usec int) -> Time
  • unixMilliToTime(msec int) -> Time
  • utc(t Time) -> Time
  • weekday(t Time) -> Weekday
  • yearDay(t Time) -> int

You may be wondering how you get the present time. That's in the automatically-imported, null-namespaced world library (see the page on Basic IO for more details). As getting the time is impure, you can only do it in the command section of your script, using get <variable name> from Clock().

Clone this wiki locally