123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215open!ImportopenStd_internal(** Parts represents the individual parts of a Span as if it were written out (it is the
counterpart to [Span.create]). For example, 90 seconds is represented by:
{[
{sign = Pos; hr = 0; min = 1; sec = 30; ms = 0; ns = 0}
]}
The fields will always be non-negative, and will never be large enough to form the
next larger unit (e.g., [min < 60]). *)moduletypeParts=sigtypet=private{sign:Sign.t;hr:int;min:int;sec:int;ms:int;us:int;ns:int}[@@derivingcompare,sexp,sexp_grammar]endmoduletypeS=sig(** Span.t represents a span of time (e.g. 7 minutes, 3 hours, 12.8 days). The span
may be positive or negative. *)typeunderlyingtypet=privateunderlying[@@derivingbin_io,hash,sexp,sexp_grammar,typerep]moduleParts:PartsincludeComparable_binablewithtypet:=tincludeComparable.With_zerowithtypet:=tincludeHashable_binablewithtypet:=tincludePretty_printer.Swithtypet:=tincludeRobustly_comparablewithtypet:=tincludeQuickcheck.S_rangewithtypet:=t(** Time spans are denominated as a float suffixed by a unit of time; the valid suffixes
are listed below:
d - days
h - hours
m - minutes
s - seconds
ms - milliseconds
us - microseconds
ns - nanoseconds
[to_string] and [sexp_of_t] use a mixed-unit format, which breaks the input span
into parts and concatenates them in descending order of unit size. For example, pi
days is rendered as "3d3h23m53.60527015815s". If the span is negative, a single "-"
precedes the entire string. For extremely large (>10^15 days) or small (<1us) spans,
a unit may be repeated to ensure the string conversion round-trips.
[of_string] and [t_of_sexp] accept any combination of (nonnegative float
string)(unit of time suffix) in any order, without spaces, and sums up the durations
of each of the parts for the magnitude of the span. The input may be prefixed by "-"
for negative spans.
String and sexp conversions round-trip precisely, that is:
{[ Span.of_string (Span.to_string t) = t ]}
*)valto_string:t->stringvalof_string:string->t(** {6 values} *)valnanosecond:tvalmicrosecond:tvalmillisecond:tvalsecond:tvalminute:tvalhour:tvalday:t(** 10^-6 seconds, used in robustly comparable operators (<., >., =., ...) to determine
equality *)valrobust_comparison_tolerance:tvalzero:t(** [?sign] defaults to positive. Setting it to negative is equivalent to negating all
the integers. *)valcreate:?sign:Sign.t->?day:int->?hr:int->?min:int->?sec:int->?ms:int->?us:int->?ns:int->unit->tvalto_parts:t->Parts.t(** {6 converters} *)(** conversions to and from [float] *)valof_ns:float->tvalof_us:float->tvalof_ms:float->tvalof_sec:float->tvalof_min:float->tvalof_hr:float->tvalof_day:float->tvalto_ns:t->floatvalto_us:t->floatvalto_ms:t->floatvalto_sec:t->floatvalto_min:t->floatvalto_hr:t->floatvalto_day:t->float(** conversions from [int] *)valof_int_ns:int->tvalof_int_us:int->tvalof_int_ms:int->tvalof_int_sec:int->tvalof_int_min:int->tvalof_int_hr:int->tvalof_int_day:int->t(** conversions from other size integer types for seconds *)valof_int32_seconds:Int32.t->tvalof_int63_seconds:Int63.t->t(** [to_int63_seconds_round_down_exn t] returns the number of seconds represented by
[t], rounded down, raising if the result is not representable as an [Int63.t]. *)valto_int63_seconds_round_down_exn:t->Int63.t(** The only condition [to_proportional_float] is supposed to satisfy is that for all
[t1, t2 : t]: [to_proportional_float t1 /. to_proportional_float t2 = t1 // t2]. *)valto_proportional_float:t->float(** {6 Basic operations on spans}
The arithmetic operations rely on the behavior of the underlying representation of a
span. For example, if addition overflows with float-represented spans, the result is
an infinite span; with fixed-width integer-represented spans, the result silently
wraps around as in two's-complement arithmetic. *)val(+):t->t->tval(-):t->t->t(** absolute value *)valabs:t->t(** negation *)valneg:t->tvalscale:t->float->tval(/):t->float->tval(//):t->t->float(** [next t] is the smallest representable span greater than [t] (and therefore
representation-dependent) *)valnext:t->t(** [prev t] is the largest representable span less than [t] (and therefore
representation-dependent) *)valprev:t->t(** [to_short_string t] pretty-prints approximate time span using no more than
five characters if the span is positive, and six if the span is negative.
Examples
{ul
{li ["4h"] = 4 hours}
{li ["5m"] = 5 minutes}
{li ["4s"] = 4 seconds}
{li ["10ms"] = 10 milliseconds}
}
only the most significant denomination is shown.
*)valto_short_string:t->string(** [to_unit_of_time t] = [Day] if [abs t >= day], [Hour] if [abs t >= hour], and so on
down to [Microsecond] if [abs t >= microsecond], and [Nanosecond] otherwise. *)valto_unit_of_time:t->Unit_of_time.t(** [of_unit_of_time unit_of_time] produces a [t] representing the corresponding span. *)valof_unit_of_time:Unit_of_time.t->t(** [to_string_hum t ~delimiter ~decimals ~align_decimal ~unit_of_time] formats [t] using
the given unit of time, or the largest appropriate units if none is specified, among
"d"=day, "h"=hour, "m"=minute, "s"=second, "ms"=millisecond, "us"=microsecond, or
"ns"=nanosecond. The magnitude of the time span in the chosen unit is formatted by:
[Float.to_string_hum ~delimiter ~decimals ~strip_zero:(not align_decimal)]
If [align_decimal] is true, the single-character suffixes are padded with an extra
space character. In combination with not stripping zeroes, this means that the
decimal point will occur a fixed number of characters from the end of the string. *)valto_string_hum:?delimiter:char(** defaults to ['_'] *)->?decimals:int(** defaults to 3 *)->?align_decimal:bool(** defaults to [false] *)->?unit_of_time:Unit_of_time.t(** defaults to [to_unit_of_time t] *)->t->string(** [randomize t ~percent] returns a span +/- percent * original span. Percent must be
between 0% and 100% inclusive, and must be positive. *)valrandomize:?state:Random.State.t->t->percent:Percent.t->tend