Internet timestamp formats: RFC 3339, HTTP-date and Unix epoch
README.md

timestamp #

Internet timestamp formats for OCaml: RFC 3339, HTTP-date, RFC 2822 and Unix
epoch, with calendar dates and ISO 8601 weeks.

Internet protocols write time as text in several ways. JSON APIs use RFC 3339
("2024-11-03T10:30:00Z"), HTTP headers use the date of RFC 9110
("Sun, 06 Nov 1994 08:49:37 GMT"), and many APIs send POSIX epoch seconds or
milliseconds as bare numbers. timestamp parses each of these into a
Ptime.t and formats it back, and
gives each a nox-json codec that a larger JSON codec can use for a
timestamp field.

Contents #

Installation #

Install with opam:

$ opam install timestamp

If opam cannot find the package, it may not yet be released in the public
opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install timestamp

It needs OCaml 4.14 or later, ptime, fmt and nox-json.

Usage #

Each format parses to a Ptime.t option and formats a Ptime.t back:

# Timestamp.Rfc3339.parse "2024-11-03T10:30:00Z" |> Option.map (Timestamp.Rfc3339.format ~frac_s:3);;
- : string option = Some "2024-11-03T10:30:00.000Z"
# Timestamp.Rfc3339.parse "2024-11-03 10:30";;
- : Ptime.t option = None
# Timestamp.Http.parse "Sunday, 06-Nov-94 08:49:37 GMT" |> Option.map Timestamp.Http.format;;
- : string option = Some "Sun, 06 Nov 1994 08:49:37 GMT"
# Timestamp.Unix.of_s 1_730_629_800 |> Option.map Timestamp.Unix.to_s;;
- : int option = Some 1730629800

parse_with_offset keeps the offset of the input, in seconds, so a
timestamp can be written back the way it came:

# match Timestamp.Rfc3339.parse_with_offset "2024-11-03T12:30:00+02:00" with
  | Some (t, tz_offset_s) -> Some (Timestamp.Rfc3339.format ?tz_offset_s t)
  | None -> None;;
- : string option = Some "2024-11-03T12:30:00+02:00"

RSS dates are RFC 2822 dates, often bent: a missing time zone or seconds,
a full month name. Rfc2822.parse takes them leniently:

# Timestamp.Rfc2822.parse "Mon, 22 Jul 2024 09:00 EDT" |> Option.map Timestamp.Rfc3339.format;;
- : string option = Some "2024-07-22T13:00:00Z"

Calendar dates and ISO weeks are integers, computed without Ptime:

# let d = Result.get_ok (Timestamp.Date.of_string "2010-01-03");;
val d : Timestamp.Date.t = <abstr>
# Timestamp.(Week.to_string (Week.of_date d), Date.to_day d);;
- : string * int = ("2009-W53", 14612)
# Timestamp.(Date.to_string (Week.monday (Week.of_date d)));;
- : string = "2009-12-28"

The codecs slot into a nox-json record codec as the codec of a field:

type event = { name : string; at : Ptime.t }

let event_codec =
  Json.Codec.Object.map ~kind:"event" (fun name at -> { name; at })
  |> Json.Codec.Object.member "name" Json.Codec.string ~enc:(fun e -> e.name)
  |> Json.Codec.Object.member "at" (Timestamp.Rfc3339.codec ())
       ~enc:(fun e -> e.at)
  |> Json.Codec.Object.seal
# Json.of_string event_codec {|{"name":"launch","at":"2024-11-03T10:30:00Z"}|}
  |> Result.map (fun e -> Json.to_string event_codec e);;
- : (string, Json.Error.t) result =
Ok "{\"name\":\"launch\",\"at\":\"2024-11-03T10:30:00Z\"}"

API #

lib/timestamp.mli is the reference, which
dune build @doc renders with odoc. Each format has its own submodule with
a parser, a formatter and, where one makes sense, a JSON codec of type
Ptime.t Json.Codec.t.

Timestamp.Rfc3339 handles RFC 3339,
a strict profile of ISO 8601.

  • parse applies the UTC offset and returns the instant.
  • parse_with_offset also returns the offset of the input in seconds.
  • format ?frac_s ?tz_offset_s writes an instant with frac_s fractional
    digits and the given offset.
  • codec ?frac_s () is the JSON string codec.

Timestamp.Http handles the HTTP date of
RFC 9110 Section 5.6.7.
parse accepts the preferred IMF-fixdate and also the two obsolete forms
that a recipient still has to accept, RFC 850 and ANSI C asctime. format
always writes IMF-fixdate in GMT. One instant in the three forms reads
Sun, 06 Nov 1994 08:49:37 GMT (IMF-fixdate),
Sunday, 06-Nov-94 08:49:37 GMT (RFC 850, with a two-digit year) and
Sun Nov 6 08:49:37 1994 (asctime).

Timestamp.Rfc2822 handles the date of
RFC 2822 Section 3.3,
which RSS uses. parse accepts the obsolete forms of Section 4.3 (comments,
two- and three-digit years, named and military zones) and what feeds write
besides (no seconds, no time, no zone, UTC, +05:30, full names);
parse_with_offset also returns the offset, None when the zone gives
none. format ?tz_offset_s writes the strict form.

Timestamp.Date is a calendar date from 0001-01-01 to 9999-12-31, an
immediate integer: of_string and to_string read and write YYYY-MM-DD,
of_prefix reads the date an RFC 3339 timestamp starts with, to_day and
of_day convert to and from the days since 1970-01-01,
add_days and weekday compute on them, and of_ptime and to_ptime
convert to Ptime.t. Timestamp.Week is an ISO 8601 week: of_date is the
week of a date, monday its first day, weeks y the 52 or 53 weeks of a
year, add and diff step and count weeks, and of_string and to_string read and write YYYY-Www. The day
arithmetic is integer only and allocates nothing.

Timestamp.Unix converts POSIX epoch seconds (of_s and to_s, with the
int64 versions of_s_int64 and to_s_int64) and milliseconds (of_ms,
to_ms), and drops any fraction of the unit. The conversions go through
Ptime spans, which count picoseconds, and never through floats. A double
that holds today's epoch in seconds, about 1.7 x 10^9, has neighbours 2^-22
s apart, about 0.24 microseconds, so it cannot hold a span's precision and a
conversion through it can round a time to the next value. codec_s and
codec_ms are the JSON number codecs.

ptime represents POSIX time, and
timestamp adds format conversions to Ptime.t, and calendar dates that
do not need it.
ISO8601 and
timedesc parse dates and times
in general, while timestamp handles only the Internet protocol forms listed
here and their JSON codecs.

License #

ISC. See LICENSE.md.