Units.TimeFunctions for converting and displaying units of time.
This functor needs to be applied to a module of type NUM representing your preferred base time unit resolution.
Example: module Time_int = Time (Int)
This module is intended to provide informal human-readable representations of approximate time durations, converting a value of type t to friendly strings like "3 weeks, 1 day" or "3 hours and 10 minutes".
Accuracy is not the raison d'etre of this module; for example, months are defined as 31 days long, and years are defined as 12 months long! Leap years and leap seconds are completely ignored.
Examples:
(minutes !12 + seconds !15) * !6 |> long = "1 hour, 13 minutes and 30 seconds"of_string "7:03" |> long = "7 minutes and 3 seconds"N.B. For computing with accurate representations of time durations, see e.g. Daniel Bünzli's excellent modules:
type t = N.tThe type of base time units.
val (!) : int -> N.tPrefix operator to convert an int to a t.
val factors : (N.t * string * string) listMultiplicative factors for each unit, in descending order, with the unit's name and the plural form of the name.
For example, (!24, "day", "days") says that one day is 24 of the unit that follows it in the list, which is "hour".
This list may contain units that cannot be represented in a given base number type N; see Units.
factors contains the following units:
val units : (N.t * string * string) listAll the units, with their multipliers, in descending order, that are resolvable with base number type N.
For example, with Units.Int base units, units contains (86400000000, "day", "days"), because one day is 86_400_000_000 microseconds, which is the minimum factor or "base unit".
If you use a small base unit, then units may not contain all the units in factors. For example:
module T = Units.Time (struct
include Int32
let ( + ), ( - ), ( * ), ( / ), (mod) = add, sub, mul, div, rem
end)
T.(minimum,maximum) = ((1l, "microsecond", "microseconds"), (60000000l, "minute", "minutes"))val maximum : N.t * string * stringmaximum is the maximum resolvable unit with base type t.
val minimum : N.t * string * stringminimum is the minimum resolvable unit with base type t.
val long : ?zero:string -> ?sep:string -> ?last:string -> N.t -> string(long ?zero ?sep ?last t) is a long-format string representation of time t.
~zero is returned when (t = N.zero) (default: N.(to_string zero))
~sep is the string that separates all the units of the time description (default: ", "), except that ~last is used as the separator between the last unit and the one preceding it.
Examples:
(days !22 + minutes !10 |> long) = ("3 weeks, 1 day and 10 minutes")(days !22 |> long) = ("3 weeks and 1 day")(days !1 |> long) = ("1 day")(days !0 |> long) = ("0")(days !0 |> long ~zero:"no days, baby") = ("no days, baby")val of_string : ?min:string -> ?sep:string -> string -> N.t(of_string ?min ?sep str) converts a string in a certain format to a t.
The format of the string that of positive integers (with optional leading zeroes) separated by any of the characters in ~sep (default: ":"). ~min is (singular form of) the name of minimal unit i.e. the unit of the rightmost integer (default: "second").
For example, (of_string 10) is 10 seconds in base units (due to the default ~sep), while (of_string "1:12:04") is 1 hour, 12 minutes and 4 seconds.
module Month : sig ... endFunctions to convert between month names and month numbers.