source: code/trunk/lib/date.ml@ 71

Last change on this file since 71 was 43, checked in by fox, 2 years ago

Relation dates, with conversion condition upon it

File size: 1.1 KB
RevLine 
[3]1type t = { created: string; edited: string }
[2]2let compare = compare
[3]3let rfc_string date = date
4let of_string (rfc : string) = rfc
5let listing date = if date.edited <> "" then date.edited else date.created
[5]6let months = [|"Jan";"Feb";"Mar";"Apr";"May";"Jun";"Jul";"Aug";"Sep";"Oct";"Nov";"Dec"|]
[3]7let pretty_date date =
[5]8 try Scanf.sscanf date "%4s-%d-%2s" (fun y m d -> Printf.sprintf "%s %s, %s" d (months.(m-1)) y)
9 with
10 | Scanf.Scan_failure s as e -> Printf.fprintf stderr "%s for %s\n" s date; raise e
11 | Invalid_argument _s as e -> Printf.fprintf stderr "Parsing %s" date; raise e
[3]12let now () = Unix.time () |> Unix.gmtime |>
13 (fun t -> Printf.sprintf "%4d-%02d-%02dT%02d:%02d:%02dZ"
14 (t.tm_year+1900) (t.tm_mon+1) t.tm_mday t.tm_hour t.tm_min t.tm_sec)
15let to_secs date =
16 Scanf.sscanf date "%4d-%02d-%02dT%02d:%02d:%02d"
17 (fun y mo d h mi s -> (y-1970)*31557600 + mo*2629800 + d*86400 + h*3600 + mi*60 + s)
[43]18let of_secs s =
19 let { Unix.tm_sec=seconds; tm_min=minutes; tm_hour=hours;
20 tm_mday=day; tm_mon=month; tm_year=year; _ } = Unix.localtime (float_of_int s) in
21 Printf.sprintf "%4d-%02d-%02dT%02d:%02d:%02d"
22 (year+1900) (month+1) day hours minutes seconds
Note: See TracBrowser for help on using the repository browser.