···376376377377 and render_date (date : Date.t) =
378378 let href_attr =
379379- let str = Format.asprintf "%a" Date.pp date in
379379+ let str = Format.asprintf "%a" Date.pp (Date.drop_time date) in
380380 let base = Iri_scheme.base_iri ~host: Params.host in
381381 let iri = Iri.resolve ~base (Iri.of_string str) in
382382 match F.get_article iri with
···386386 X.date
387387 [href_attr]
388388 [
389389- X.year [] "%i" date.yyyy;
390390- date.mm |> X.optional @@ X.month [] "%i";
391391- date.dd |> X.optional @@ X.day [] "%i"
389389+ X.year [] "%i" (Date.year date);
390390+ Date.month date |> X.optional @@ X.month [] "%i";
391391+ Date.day date |> X.optional @@ X.day [] "%i"
392392 ]
393393394394 let render_article (article : T.content T.article) : P.node =
+5
lib/human_datetime/Forester_human_datetime.ml
···99let parse_string str =
1010 let lexbuf = Lexing.from_string str in
1111 parse lexbuf
1212+1313+let parse_string_exn str =
1414+ match parse_string str with
1515+ | None -> failwith "human datetime: parse error"
1616+ | Some dt -> dt
+7-1
lib/human_datetime/Types.ml
···149149 state.date.year <- y;
150150 Option.iter (go_month state) month_opt
151151 in
152152-153152 let state = init_ptime_date_time_state () in
154153 go_year state datetime;
155154 let date = state.date.year, state.date.month, state.date.day in
156155 let time = ((state.time.hour, state.time.minute, state.time.second), state.tz_offset_s) in
157156 Ptime.of_date_time (date, time)
157157+158158+let compare_datetime dt0 dt1 =
159159+ match to_ptime dt0, to_ptime dt1 with
160160+ | Some x0, Some x1 -> Ptime.compare x0 x1
161161+ | None, None -> 0
162162+ | None, Some _ -> -1
163163+ | Some _, None -> 1
+27-80
lib/prelude/Date.ml
···11-open Fun_util
21module HDT = Forester_human_datetime
3244-type t = { yyyy: int; mm: int option; dd: int option }
55-[@@deriving repr]
33+type t = HDT.datetime
6477-let year d = d.yyyy
88-let month d = d.mm
99-let day d = d.dd
55+let drop_time = function
66+ | HDT.Year (y, Some (HDT.Month (m, Some (HDT.Day (d, _))))) ->
77+ HDT.Year (y, Some (HDT.Month (m, Some (HDT.Day (d, None)))))
88+ | dt -> dt
1091111-let now () =
1212- let t = Unix.localtime (Unix.time ()) in
1313- { yyyy = 1900 + t.tm_year; mm = Some (1 + t.tm_mon); dd = Some t.tm_mday }
1010+let t =
1111+ let of_string str = HDT.parse_string_exn str in
1212+ let to_string dt = Format.asprintf "%a" HDT.pp_datetime dt in
1313+ Repr.map Repr.string of_string to_string
14141515-(* approximate, only for sorting *)
1616-let to_ptime (date : t) : Ptime.t =
1717- let dd = Option.value ~default: 1 date.dd in
1818- let mm = Option.value ~default: 1 date.mm in
1919- match Ptime.of_date (date.yyyy, mm, dd) with
2020- | None -> failwith "to_ptime"
2121- | Some t -> t
1515+let pp = HDT.pp_datetime
1616+let parse = HDT.parse_string
1717+let compare = HDT.compare_datetime
22182323-let compare (d0 : t) (d1 : t) =
2424- Ptime.compare (to_ptime d0) (to_ptime d1)
1919+let year = function HDT.Year (y, _) -> y
25202626-let parse_date str =
2727- match String.split_on_char '-' str with
2828- | yyyy :: rest ->
2929- let yyyy = int_of_string yyyy in
3030- begin
3131- match rest with
3232- | mm :: rest ->
3333- let mm = Some (int_of_string mm) in
3434- begin
3535- match rest with
3636- | [dd] ->
3737- let dd = Some (int_of_string dd) in
3838- Some { yyyy; mm; dd }
3939- | _ ->
4040- Some { yyyy; mm; dd = None }
4141- end
4242- | _ ->
4343- Some { yyyy; mm = None; dd = None }
4444- end
4545- | _ ->
4646- None
2121+let month = function
2222+ | HDT.Year (_, Some (HDT.Month (m, _))) -> Some m
2323+ | _ -> None
47244848-let parse str =
4949- match String.split_on_char 'T' str with
5050- | [date] -> parse_date date
5151- | date :: _ -> parse_date date
5252- | _ ->
5353- None
2525+let day = function
2626+ | HDT.Year (_, Some (HDT.Month (_, Some (HDT.Day (d, _))))) -> Some d
2727+ | _ -> None
54285555-let pp fmt date =
5656- Format.fprintf fmt "%04d" date.yyyy;
5757- let@ mm = Option.iter @~ date.mm in
5858- Format.fprintf fmt "-%02d" mm;
5959- let@ dd = Option.iter @~ date.dd in
6060- Format.fprintf fmt "-%02d" dd
6161-6262-let pp_month fmt i =
6363- Format.fprintf fmt "%s" @@
6464- match i with
6565- | 1 -> "January"
6666- | 2 -> "February"
6767- | 3 -> "March"
6868- | 4 -> "April"
6969- | 5 -> "May"
7070- | 6 -> "June"
7171- | 7 -> "July"
7272- | 8 -> "August"
7373- | 9 -> "September"
7474- | 10 -> "October"
7575- | 11 -> "November"
7676- | 12 -> "December"
7777- | _ ->
7878- failwith @@ Format.sprintf "Invalid date: %i" i
7979-8080-let pp_human fmt date =
8181- match date.mm with
8282- | None ->
8383- Format.fprintf fmt "%04d" date.yyyy
8484- | Some mm ->
8585- match date.dd with
8686- | None ->
8787- Format.fprintf fmt "%a %04d" pp_month mm date.yyyy
8888- | Some dd ->
8989- Format.fprintf fmt "%a %i, %04d" pp_month mm dd date.yyyy
2929+let now () =
3030+ let t = Unix.gmtime (Unix.time ()) in
3131+ let second = HDT.Second t.tm_sec in
3232+ let minute = HDT.Minute (t.tm_min, Some second) in
3333+ let hour = HDT.Hour (t.tm_hour, Some minute) in
3434+ let day = HDT.Day (t.tm_mday, Some (hour, HDT.Z)) in
3535+ let month = HDT.Month (1 + t.tm_mon, Some day) in
3636+ HDT.Year (1900 + t.tm_year, Some month)
+4-2
lib/prelude/Date.mli
···11-type t = { yyyy: int; mm: int option; dd: int option }
11+type t
22+23val t : t Repr.ty
3455+val drop_time : t -> t
66+47val pp : Format.formatter -> t -> unit
55-val pp_human : Format.formatter -> t -> unit
68val parse : string -> t option
79val now : unit -> t
810