OCaml library for JSONfeed parsing and creation
1(*---------------------------------------------------------------------------
2 Copyright (c) 2024 Anil Madhavapeddy. All rights reserved.
3 SPDX-License-Identifier: ISC
4 ---------------------------------------------------------------------------*)
5
6(** RFC 3339 date/time handling for JSON Feed.
7
8 Provides parsing, formatting, and jsont combinators for RFC 3339 timestamps
9 as required by the JSON Feed specification.
10
11 @see <https://www.rfc-editor.org/rfc/rfc3339> RFC 3339 *)
12
13
14val jsont : Ptime.t Jsont.t
15(** [jsont] is a bidirectional JSON type for RFC 3339 timestamps.
16
17 On decode: accepts JSON strings in RFC 3339 format (e.g., "2024-11-03T10:30:00Z")
18 On encode: produces UTC timestamps with 'Z' suffix
19
20 {b Example:}
21 {[
22 let time = Ptime.of_float_s (Unix.time ()) |> Option.get in
23 Jsont_bytesrw.encode_string Rfc3339.jsont time
24 ]} *)
25
26val parse : string -> Ptime.t option
27(** [parse s] parses an RFC 3339 timestamp string.
28
29 Accepts various formats:
30 - "2024-11-03T10:30:00Z" (UTC)
31 - "2024-11-03T10:30:00-08:00" (with timezone offset)
32 - "2024-11-03T10:30:00.123Z" (with fractional seconds)
33
34 Returns [None] if the string is not valid RFC 3339. *)
35
36val format : Ptime.t -> string
37(** [format t] formats a timestamp as RFC 3339.
38
39 Always uses UTC timezone (Z suffix) and includes fractional seconds
40 if the timestamp has sub-second precision.
41
42 {b Example output:} ["2024-11-03T10:30:45.123Z"] *)
43
44val pp : Format.formatter -> Ptime.t -> unit
45(** [pp ppf t] pretty prints a timestamp in RFC 3339 format. *)