aboutsummaryrefslogtreecommitdiff
path: root/lib/models/snowflake.ml
blob: f228bd7094b973ceded334ef4ca1e652f6cd50da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
open Core

type t = Int.t [@@deriving sexp]

let of_yojson_exn d = Yojson.Safe.Util.to_string d |> Int.of_string

let of_yojson d =
    try Ok (of_yojson_exn d)
    with Yojson.Safe.Util.Type_error (why,_) -> Error why

let to_yojson s : Yojson.Safe.t = `String (Int.to_string s)

let timestamp snowflake =  Int64.(+) (Int64.of_int (snowflake lsr 22)) 1_420_070_400_000L

let time_of_t snowflake =
    let t = timestamp snowflake |> Int64.to_float in
    Time.(Span.of_ms t
    |> of_span_since_epoch)

let timestamp_iso snowflake =
    time_of_t snowflake
    |> Time.(to_string_iso8601_basic ~zone:Zone.utc)