OCaml library for JSONfeed parsing and creation
1(*---------------------------------------------------------------------------
2 Copyright (c) 2024 Anil Madhavapeddy. All rights reserved.
3 SPDX-License-Identifier: ISC
4 ---------------------------------------------------------------------------*)
5
6let parse s =
7 Ptime.of_rfc3339 s |> Result.to_option |> Option.map (fun (t, _, _) -> t)
8
9let format t = Ptime.to_rfc3339 ~frac_s:6 ~tz_offset_s:0 t
10let pp ppf t = Format.pp_print_string ppf (format t)
11
12let jsont =
13 let kind = "RFC 3339 timestamp" in
14 let doc = "An RFC 3339 date-time string" in
15 let dec s =
16 match parse s with
17 | Some t -> t
18 | None ->
19 Jsont.Error.msgf Jsont.Meta.none "%s: invalid RFC 3339 timestamp: %S"
20 kind s
21 in
22 let enc = format in
23 Jsont.map ~kind ~doc ~dec ~enc Jsont.string