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 match Ptime.of_rfc3339 s with
8 | Ok (t, _, _) -> Some t
9 | Error _ -> None
10
11let format t =
12 Ptime.to_rfc3339 ~frac_s:6 ~tz_offset_s:0 t
13
14let pp ppf t =
15 Format.pp_print_string ppf (format t)
16
17let jsont =
18 let kind = "RFC 3339 timestamp" in
19 let doc = "An RFC 3339 date-time string" in
20 let dec s = match parse s with
21 | Some t -> t
22 | None -> Jsont.Error.msgf Jsont.Meta.none "%s: invalid RFC 3339 timestamp: %S" kind s
23 in
24 let enc = format in
25 Jsont.map ~kind ~doc ~dec ~enc Jsont.string