package odate

  1. Overview
  2. Docs

Source file oDuration.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
type printer = Duration_private.O.t -> Duration_private.O.t -> string

type human_readable = {
  forward : bool;
  ms : int;
  s : int;
  m : int;
  h : int;
  day : int;
  month : int;
  year : int;
}

module type S = sig
  type t

  val zero : t

  val zero_human : human_readable

  val ( + ) : t -> t -> t

  val ( - ) : t -> t -> t

  val max : t -> t -> t

  val min : t -> t -> t

  val is_negative : t -> bool

  val is_positive : t -> bool

  val is_instantenous : t -> bool

  val abs : t -> t

  val make :
    ?forward:bool ->
    ?ms:int ->
    ?s:int ->
    ?m:int ->
    ?h:int ->
    ?day:int ->
    ?week:int ->
    ?month:int ->
    ?year:int ->
    unit ->
    t

  module From : sig
    val ms : int -> t

    val s : int -> t

    val s_float : float -> t

    val h : int -> t

    val m : int -> t

    val day : int -> t

    val month : int -> t

    val week : int -> t

    val year : int -> t

    val human : human_readable -> t
  end

  module To : sig
    val ms : t -> int

    val s : t -> int

    val s_float : t -> float

    val m : t -> int

    val h : t -> int

    val day : t -> int

    val month : t -> int

    val year : t -> int

    val human : t -> human_readable

    val generate_printer : string -> printer option

    val string : printer -> t -> string

    val default_printer : printer
  end
end

module D : S = struct
  include Duration_private
  include O

  let zero_human =
    {
      forward = true;
      ms = 0;
      s = 0;
      m = 0;
      h = 0;
      day = 0;
      month = 0;
      year = 0;
    }

  let max a b = if O.compare a b < 0 then b else a

  let min a b = if O.compare a b < 0 then a else b

  let is_positive d = O.compare d O.zero < 0

  let is_negative d = O.compare d O.zero > 0

  let is_instantenous d = O.compare d O.zero = 0

  let abs d = if is_negative d then O.( - ) zero d else d

  let make ?(forward = true) ?ms ?s ?m ?h ?day ?week ?month ?year () =
    let aux acc (v, fact) : O.t =
      match v with None -> acc | Some v -> O.((of_int v * fact) + acc)
    in
    let r : O.t =
      List.fold_left aux O.zero
        [
          (ms, ms_in_ms);
          (s, ms_in_s);
          (m, ms_in_min);
          (h, ms_in_hour);
          (day, ms_in_day);
          (week, ms_in_week);
          (month, ms_in_month);
          (year, ms_in_year);
        ]
    in
    if forward then r else O.(zero - r)

  module From = struct
    let ms ms = make ~ms ()

    let s s = make ~s ()

    let s_float s = make ~ms:(int_of_float (s *. 1000.)) ()

    let m m = make ~m ()

    let h h = make ~h ()

    let day day = make ~day ()

    let month month = make ~month ()

    let year year = make ~year ()

    let week week = make ~week ()

    let human { ms; s; m; h; day; month; year; forward } =
      make ~forward ~ms ~s ~m ~h ~day ~month ~year ()
  end

  module To = struct
    let ms t = int_of_float (O.to_float t)

    let s_float t = O.(to_float (t / ms_in_s))

    let aux divide t = int_of_float (O.to_float t /. O.to_float divide)

    let s = aux ms_in_s

    let m = aux ms_in_min

    let h = aux ms_in_hour

    let day = aux ms_in_day

    let month = aux ms_in_month

    let year = aux ms_in_year

    let human t =
      let aux (t, h) (f, fact) =
        let d, r = O.div t fact in
        if d > 0 then (r, f h d) else (t, h)
      in
      let t, forward = if is_negative t then (abs t, false) else (t, true) in
      let _, h =
        List.fold_left aux
          (t, { zero_human with forward })
          [
            ((fun t year -> { t with year }), ms_in_year);
            ((fun t month -> { t with month }), ms_in_month);
            ((fun t day -> { t with day }), ms_in_day);
            ((fun t h -> { t with h }), ms_in_hour);
            ((fun t m -> { t with m }), ms_in_min);
            ((fun t s -> { t with s }), ms_in_s);
            ((fun t ms -> { t with ms }), ms_in_ms);
          ]
      in
      h

    let generate_printer s =
      try
        let lexbuf = Lexing.from_string s in
        Some (Duration_parser.main Duration_lexer.tokens lexbuf)
      with exc ->
        Printf.eprintf "exc: %s" (Printexc.to_string exc);
        None

    let generate_or_exit s =
      match generate_printer s with
      | Some p -> p
      | None -> failwith "could not generate printer"

    let default_printer =
      let fmt =
        "[%>:[%D:[#=1:tomorrow :[%s:[#>0:in ]]]]]"
        ^ "[%Y:[#>0:# year[#>1:s] ][#=0:" ^ "[%M:[#>0:# month[#>1:s] ][#=0:"
        ^ "[%D:[#>1:# day[#>1:s] ][#=0:"
        (* we don't print days for #=1, because that was taken care with tomorrow/yesterday *)
        ^ "[%h:[#>0:# hour[#>1:s] ][#=0:"
        ^ "[%m:[#>0:# minute[#>1:s] ][#=0:"
        ^ "[%s:[#>0:# second[#>1:s] :just now ]" ^ "]]]]]]]]]]]"
        ^ "[%<:[%D:[#=1:yesterday :[%s:[#>0:ago]] ]]]"
      in
      generate_or_exit fmt

    let string (printer : printer) t = printer t O.zero
  end
end

include D
OCaml

Innovation. Community. Security.