package timmy-jsoo

  1. Overview
  2. Docs

Source file clock.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
open Base
module Timmy = Timmy.Versions.V1_1
include Timmy.Timezone
module Js = Js_of_ocaml.Js

let now () = Ptime_clock.now () |> Timmy.Time.of_ptime

(* JS Date's getTimezoneOffset is designed so that date + offset = GMT. Timmy
   computes the offset from GMT, date - offset = GMT. *)
let timezone_local =
  let name =
    let fmt =
      new%js Js_of_ocaml.Intl.dateTimeFormat_constr Js.undefined Js.undefined
    in
    let options = fmt##resolvedOptions () in
    options##.timeZone |> Js.to_string
  and offset_calendar_time_s ~date:(year, month, day)
      ~time:(hours, minutes, seconds) =
    let () =
      let is_input_ok =
        Result.is_ok (Timmy.Date.of_tuple (year, month, day))
        && Result.is_ok (Timmy.Daytime.of_tuple (hours, minutes, seconds))
      in
      match is_input_ok with
      | true -> ()
      | false ->
        Fmt.failwith
          "Given date and time %04i-%02i-%02i at %02i:%02i:%02i are not valid"
          year month day hours minutes seconds
    in
    let js_date =
      new%js Js.date_sec year (month - 1) day hours minutes seconds
    in
    js_date##getTimezoneOffset * 60 * -1
  and offset_timestamp_s ~unix_timestamp =
    let () =
      if Int64.compare 0L unix_timestamp > 0 then
        Fmt.failwith "Given timestamp is negative"
    in
    let js_date =
      new%js Js.date_fromTimeValue
        (Js.float (Int64.to_float unix_timestamp *. 1000.0))
    in
    js_date##getTimezoneOffset * 60 * -1
  in
  of_implementation ~offset_calendar_time_s ~offset_timestamp_s name

let today () = Timmy.Date.of_time ~timezone:timezone_local @@ now ()
OCaml

Innovation. Community. Security.