rfc3339.ml 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. (*
  2. * _ _ ____ _
  3. * _| || |_/ ___| ___ _ __ _ __ ___ | |
  4. * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
  5. * |_ _|___) | __/ |_) | |_) | (_) |_|
  6. * |_||_| |____/ \___| .__/| .__/ \___/(_)
  7. * |_| |_|
  8. *
  9. * Personal Social Web.
  10. *
  11. * Copyright (C) The #Seppo contributors. All rights reserved.
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *)
  26. type t = T of string
  27. let fallback = Timedesc.Time_zone.(local () |> Option.value ~default:utc)
  28. let epoch = T "1970-01-01 00:00:00Z"
  29. let compare a b =
  30. let T a = a
  31. and T b = b in
  32. a |> String.compare b
  33. let tz_offset_s tz t0 =
  34. match t0
  35. |> Timedesc.Utils.timestamp_of_ptime
  36. |> Timedesc.of_timestamp ~tz_of_date_time:tz with
  37. | None -> 0
  38. | Some x ->
  39. (match x |> Timedesc.offset_from_utc with
  40. | `Single t
  41. | `Ambiguous (t,_) -> t)
  42. |> Timedesc.Timestamp.to_float_s
  43. |> Int.of_float
  44. let to_string ?(tz = fallback) t0 =
  45. let tz_offset_s = t0 |> tz_offset_s tz in
  46. t0 |> Ptime.to_rfc3339 ~tz_offset_s
  47. let of_ptime ?(tz = fallback) t0 =
  48. T (t0 |> to_string ~tz)
  49. let to_ptime (T s) =
  50. match s |> Ptime.of_rfc3339 with
  51. | Error _ -> Error "expected rfc3339"
  52. | Ok (t,_,_) -> Ok t
  53. let to_xml (n : string) (T s) =
  54. `El (((Xml.ns_a,n),[]),[`Data s])