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 13val jsont : Ptime.t Jsont.t 14(** [jsont] is a bidirectional JSON type for RFC 3339 timestamps. 15 16 On decode: accepts JSON strings in RFC 3339 format (e.g., 17 "2024-11-03T10:30:00Z") On encode: produces UTC timestamps with 'Z' suffix 18 19 {b Example:} 20 {[ 21 let time = Ptime.of_float_s (Unix.time ()) |> Option.get in 22 Jsont_bytesrw.encode_string Rfc3339.jsont time 23 ]} *) 24 25val parse : string -> Ptime.t option 26(** [parse s] parses an RFC 3339 timestamp string. 27 28 Accepts various formats: 29 - "2024-11-03T10:30:00Z" (UTC) 30 - "2024-11-03T10:30:00-08:00" (with timezone offset) 31 - "2024-11-03T10:30:00.123Z" (with fractional seconds) 32 33 Returns [None] if the string is not valid RFC 3339. *) 34 35val format : Ptime.t -> string 36(** [format t] formats a timestamp as RFC 3339. 37 38 Always uses UTC timezone (Z suffix) and includes fractional seconds if the 39 timestamp has sub-second precision. 40 41 {b Example output:} ["2024-11-03T10:30:45.123Z"] *) 42 43val pp : Format.formatter -> Ptime.t -> unit 44(** [pp ppf t] pretty prints a timestamp in RFC 3339 format. *)