package core_kernel

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
type underlying = float

Span.t represents a span of time (e.g. 7 minutes, 3 hours, 12.8 days). The span may be positive or negative.

type t = private underlying
include sig ... end
val bin_read_t : t Bin_prot.Read.reader
val __bin_read_t__ : (Base.Int.t -> t) Bin_prot.Read.reader
val bin_reader_t : t Bin_prot.Type_class.reader
val bin_size_t : t Bin_prot.Size.sizer
val bin_write_t : t Bin_prot.Write.writer
val bin_writer_t : t Bin_prot.Type_class.writer
val bin_shape_t : Bin_prot.Shape.t
val t_of_sexp : Sexplib.Sexp.t -> t
val sexp_of_t : t -> Sexplib.Sexp.t
module Parts : sig ... end
include Interfaces.Comparable_binable with type t := t
include Base.Comparable_intf.S with type t := t
include Base.Comparable_intf.Polymorphic_compare with type t := t
include Base.Comparisons.Infix with type t := t
val (>=) : t -> t -> bool
val (<=) : t -> t -> bool
val (=) : t -> t -> bool
val (>) : t -> t -> bool
val (<) : t -> t -> bool
val (<>) : t -> t -> bool
val equal : t -> t -> bool
val compare : t -> t -> int

compare t1 t2 returns 0 if t1 is equal to t2, a negative integer if t1 is less than t2, and a positive integer if t1 is greater than t2.

val min : t -> t -> t
val max : t -> t -> t
val ascending : t -> t -> int

ascending is identical to compare. descending x y = ascending y x. These are intended to be mnemonic when used like List.sort ~cmp:ascending and List.sort ~cmp:descending, since they cause the list to be sorted in ascending or descending order, respectively.

val descending : t -> t -> int
val between : t -> low:t -> high:t -> bool
val clamp_exn : t -> min:t -> max:t -> t

clamp_exn t ~min ~max returns t', the closest value to t such that between t' ~low:min ~high:max is true.

Raises if not (min <= max).

val clamp : t -> min:t -> max:t -> t Base.Or_error.t
include Base.Comparator.S with type t := t
type comparator_witness
include Base.Comparable_intf.Validate with type t := t
val validate_lbound : min:t Base.Maybe_bound.t -> t Base.Validate.check
val validate_ubound : max:t Base.Maybe_bound.t -> t Base.Validate.check
val validate_bound : min:t Base.Maybe_bound.t -> max:t Base.Maybe_bound.t -> t Base.Validate.check
include Comparable.With_zero with type t := t
val validate_positive : t Base.Validate.check
val validate_non_negative : t Base.Validate.check
val validate_negative : t Base.Validate.check
val validate_non_positive : t Base.Validate.check
val is_positive : t -> bool
val is_non_negative : t -> bool
val is_negative : t -> bool
val is_non_positive : t -> bool
val sign : t -> Base__.Sign0.t

Returns Neg, Zero, or Pos in a way consistent with the above functions.

include Interfaces.Hashable_binable with type t := t
include sig ... end
val hash_fold_t : Base.Hash.state -> t -> Base.Hash.state
val hash : t -> Base.Hash.hash_value
val hashable : t Hashtbl.Hashable.t
module Table : Hashtbl.S_binable with type key = t
module Hash_set : Hash_set.S_binable with type elt = t
module Hash_queue : Hash_queue.S with type Key.t = t
include Base.Pretty_printer.S with type t := t
val pp : Caml.Format.formatter -> t -> unit
include Interfaces.Robustly_comparable with type t := t
val (>=.) : t -> t -> bool
val (<=.) : t -> t -> bool
val (=.) : t -> t -> bool
val (>.) : t -> t -> bool
val (<.) : t -> t -> bool
val (<>.) : t -> t -> bool
val robustly_compare : t -> t -> int
val to_string : t -> Base.String.t

String converters and sexp converters allow for specifying of time spans in various units after a leading float (e.g. 45s, 3h, or 1d):

ms - milliseconds s - seconds m - minutes h - hours d - days

The outgoing conversion functions use these units as well, choosing the largest available type. For instance, if it's a bit greater than or equal to 1 hour, the span will be rendered in hours, (Time.to_string (Time.of_string "66m") = "1.1h").

As of Stable.V2, of_string and t_of_sexp also accept "us"=microseconds and "ns"=nanoseconds suffixes. Stable.V2 will produce these suffixes, but for compatibility with Stable.V1, ordinary to_string and sexp_of_t will not, for now. Once use of the new of_ family is more widespread, we will switch the to_ family to the more expressive format. In the meantime, you can get ns and us suffixes by using to_string_hum.

val of_string : Base.String.t -> t
values
val nanosecond : t
val microsecond : t
val millisecond : t
val second : t
val minute : t
val hour : t
val day : t
val robust_comparison_tolerance : t

10^-6 seconds, used in robustly comparable operators (<., >., =., ...) to determine equality

val zero : t
val create : ?sign:Sign.t -> ?day:Base.Int.t -> ?hr:Base.Int.t -> ?min:Base.Int.t -> ?sec:Base.Int.t -> ?ms:Base.Int.t -> ?us:Base.Int.t -> ?ns:Base.Int.t -> Base.Unit.t -> t

create ?sign ?day ?hr ?min ?sec ?ms ?us ?ns () Create a span from the given parts. All parts are assumed to be positive (no checking is done by the function) and the sign of the final span is given by sign which is positive by default.

val to_parts : t -> Parts.t
converters
val of_ns : Base.Float.t -> t
val of_us : Base.Float.t -> t
val of_ms : Base.Float.t -> t
val of_sec : Base.Float.t -> t
val of_int_sec : Base.Int.t -> t
val of_int32_seconds : Int32.t -> t
val of_int63_seconds : Int63.t -> t
val of_min : Base.Float.t -> t
val of_hr : Base.Float.t -> t
val of_day : Base.Float.t -> t
val to_ns : t -> Base.Float.t
val to_us : t -> Base.Float.t
val to_ms : t -> Base.Float.t
val to_sec : t -> Base.Float.t
val to_min : t -> Base.Float.t
val to_hr : t -> Base.Float.t
val to_day : t -> Base.Float.t
val to_int63_seconds_round_down_exn : t -> Int63.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.

val to_proportional_float : t -> Base.Float.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.

Basic operations on spans
val (+) : t -> t -> t
val (-) : t -> t -> t
val abs : t -> t

absolute value

val neg : t -> t

negation

val scale : t -> Base.Float.t -> t
val (/) : t -> Base.Float.t -> t
val (//) : t -> t -> Base.Float.t
val next : t -> t

next t return the next t (next t > t)

val prev : t -> t

prev t return the previous t (prev t < t)

val to_short_string : t -> Base.String.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

  • "4h" = 4 hours
  • "5m" = 5 minutes
  • "4s" = 4 seconds
  • "10ms" = 10 milliseconds

only the most significant denomination is shown.

val to_unit_of_time : t -> Unit_of_time.t

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.

val of_unit_of_time : Unit_of_time.t -> t

of_unit_of_time unit_of_time produces a t representing the corresponding span.

val to_string_hum : ?delimiter:Base.Char.t -> ?decimals:Base.Int.t -> ?align_decimal:Base.Bool.t -> ?unit_of_time:Unit_of_time.t -> t -> Base.String.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.

val randomize : t -> percent:Percent.t -> t

randomize t ~percent returns a span +/- percent * original span. Percent must be between 0% and 100% inclusive, and must be positive.

module Stable : sig ... end
OCaml

Innovation. Community. Security.