Next: , Previous: , Up: The Standard Library   [Contents][Index]


21.12 Dates and Times

Often a format encodes a date and time expressed as the number of seconds since midnight, January 1st 1970. You could map these simply as integers. However, the standard library provides two types POSIX_Time32 and POSIX_Time64 which include pretty-printers to display the date in a human readable format. The definition is:

type POSIX_Timesize = struct
{
  uint<size>  seconds;

  method _print = void:
  { … }
}

where size is either 32 or 64. When pretty printing is enabled, a mapped value of these types will display similar to

#<2019-Dec-12 8:54:56>

whereas when pretty printing is not enabled, this example would be displayed as:

POSIX_Time32 {seconds=1576140896U}

Note that timestamps of this type do not account for leap seconds and are agnostic towards timezone.

Occasionally you might wish to print an unmapped timestamp value in a human readable format. To do this, you can use the ptime function, which is defined as follows:

fun ptime = (uint<64> seconds) void:
{ … }

This pickle also provides a type Timespec that can store Unix times to nanosecond precision, and has the form:

type Timespec = struct
{
  int<64> sec;
  int<64> nsec;
};

The function gettimeofday uses the gettime builtin in order to construct an instance of Timespec. For example:

(poke) gettimeofday
Timespec {
  sec=1604918423L,
  nsec=753492546L
}
(poke) ptime (gettimeofday.sec)
2020-Nov-9 10:40:36