t_time.ml 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. (*
  2. * _ _ ____ _
  3. * _| || |_/ ___| ___ _ __ _ __ ___ | |
  4. * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
  5. * |_ _|___) | __/ |_) | |_) | (_) |_|
  6. * |_||_| |____/ \___| .__/| .__/ \___/(_)
  7. * |_| |_|
  8. *
  9. * Personal Social Web.
  10. *
  11. * test_time.ml
  12. *
  13. * Copyright (C) The #Seppo contributors. All rights reserved.
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *)
  28. let test_ptime () =
  29. Assrt.equals_float __LOC__ 1e-3 1.0 1.001;
  30. (* How would I get the timezone offset for a given Timezone (e.g. local or
  31. Europe/Amsterdam at a given datetime? *)
  32. let str0 = "2021-09-28T09:25:00+02:00" in
  33. match Ptime.of_rfc3339 str0 with
  34. | Ok (t0, Some tz_s, 25) -> (
  35. Assrt.equals_string __LOC__ (Ptime.to_rfc3339 ~tz_offset_s:tz_s t0) str0;
  36. match "2021-09-28T09:37:00+02:00" |> Ptime.of_rfc3339 with
  37. | Ok (t1, Some 7200, 25) ->
  38. Ptime.to_float_s t1 -. Ptime.to_float_s t0
  39. |> Assrt.equals_float __LOC__ 1e-90 (12.0 *. 60.0);
  40. | _ -> assert false)
  41. | _ -> assert false
  42. let test_timedesc () =
  43. (* let tzV = Option.get (Timedesc.Time_zone.local ()) in
  44. assert ("Europe/Vienna" = Timedesc.Time_zone.name tzV); *)
  45. let tz = "Europe/Zurich" |> Timedesc.Time_zone.make |> Option.get in
  46. tz |> Timedesc.Time_zone.name |> Assrt.equals_string __LOC__ "Europe/Zurich";
  47. let t0 = Timedesc.make_exn ~tz ~year:2021 ~month:9 ~day:21 ~hour:9 ~minute:11 ~second:0 ()
  48. and t1 = Timedesc.make_exn ~tz ~year:2021 ~month:9 ~day:21 ~hour:9 ~minute:17 ~second:0 ()
  49. in
  50. t0 |> Timedesc.to_rfc3339 |> Assrt.equals_string __LOC__ "2021-09-21T09:11:00+02:00";
  51. t1 |> Timedesc.to_rfc3339 |> Assrt.equals_string __LOC__ "2021-09-21T09:17:00+02:00";
  52. let dt = Timedesc.to_timestamp_float_s_single t1 -. Timedesc.to_timestamp_float_s_single t0 in
  53. Assrt.equals_float __LOC__ 1e-90 (6.0 *. 60.0) dt
  54. let test_zoneless_to_zoned () =
  55. let tz = Timedesc.Time_zone.make_exn "Europe/Zurich"
  56. and t = Timedesc.Time.make_exn ~hour:1 ~minute:2 ~second:3 ()
  57. and d0 = Timedesc.Date.of_iso8601_exn "2021-09-28" in
  58. let zl0 = Timedesc.Zoneless.of_date_and_time d0 t in
  59. let zo0 = Result.get_ok (Timedesc.Zoneless.to_zoned ~tz zl0) in
  60. Assrt.equals_string __LOC__ "2021-09-28T01:02:03+02:00" (Timedesc.to_rfc3339 zo0);
  61. let d1 = Timedesc.Date.of_iso8601_exn "2021-12-28" in
  62. let zl1 = Timedesc.Zoneless.of_date_and_time d1 t in
  63. let zo1 = Result.get_ok (Timedesc.Zoneless.to_zoned ~tz zl1) in
  64. Assrt.equals_string __LOC__ "2021-12-28T01:02:03+01:00" (Timedesc.to_rfc3339 zo1)
  65. let test_zone () =
  66. let tz_offset_s (tz : Timedesc.Time_zone.t) (t : Ptime.t) : int =
  67. (match t
  68. |> Timedesc.Utils.timestamp_of_ptime
  69. |> Timedesc.of_timestamp_exn ~tz_of_date_time:tz
  70. |> Timedesc.offset_from_utc with
  71. | `Single t -> t
  72. | `Ambiguous (t,_) -> t)
  73. |> Timedesc.Timestamp.to_float_s
  74. |> Int.of_float
  75. in
  76. let tz = Timedesc.Time_zone.make_exn "Europe/Amsterdam" in
  77. let t0,io,i = Ptime.of_rfc3339 "2023-10-17T13:48:37-01:00" |> Result.get_ok in
  78. io |> Option.get |> Assrt.equals_int __LOC__ (-3600);
  79. i |> Assrt.equals_int __LOC__ 25;
  80. t0 |> Ptime.to_rfc3339 |> Assrt.equals_string __LOC__ "2023-10-17T14:48:37-00:00";
  81. t0 |> Ptime.to_rfc3339 ~tz_offset_s:(tz_offset_s tz t0) |> Assrt.equals_string __LOC__ "2023-10-17T16:48:37+02:00";
  82. let t0,io,i = Ptime.of_rfc3339 "2023-10-17T13:48:37Z" |> Result.get_ok in
  83. io |> Option.get |> Assrt.equals_int __LOC__ 0;
  84. i |> Assrt.equals_int __LOC__ 20;
  85. t0 |> Ptime.to_rfc3339 |> Assrt.equals_string __LOC__ "2023-10-17T13:48:37-00:00";
  86. t0 |> Ptime.to_rfc3339 ~tz_offset_s:(tz_offset_s tz t0) |> Assrt.equals_string __LOC__ "2023-10-17T15:48:37+02:00";
  87. "" |> Assrt.equals_string __LOC__ ""
  88. let () =
  89. test_ptime ();
  90. test_timedesc ();
  91. test_zoneless_to_zoned ();
  92. test_zone ();
  93. assert true