Skip to content

Instantly share code, notes, and snippets.

@NightBlues
Created February 22, 2018 11:18
Show Gist options
  • Save NightBlues/2e817c7c923ec883e73016d9fec61e2b to your computer and use it in GitHub Desktop.
Save NightBlues/2e817c7c923ec883e73016d9fec61e2b to your computer and use it in GitHub Desktop.
OCaml Unix iso datetime converting
module Unix = struct
include Unix
let tm_of_iso date =
let make y m d h mi s = Unix.(
{tm_sec=s;tm_min=mi;tm_hour=h;tm_mday=d;tm_mon=(m - 1);tm_year=(y - 1900);
tm_wday=0;tm_yday=0;tm_isdst=false}) in
Scanf.sscanf date "%u-%u-%uT%u:%u:%uZ" make
let iso_of_tm tm =
Unix.(Printf.sprintf "%u-%02u-%02uT%02u:%02u:%02uZ" (tm.tm_year + 1900) (tm.tm_mon + 1) tm.tm_mday
tm.tm_hour tm.tm_min tm.tm_sec)
let cmp_tm d1 d2 =
let ts1, _ = mktime d1 in
let ts2, _ = mktime d2 in
compare ts1 ts2
let tm_of_yojson = function
`String s -> Result.Ok (tm_of_iso s)
| _ -> Result.Error "Unknown json type for date time"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment