package core_unix

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
include module type of struct include Core.Time_ns.Span end with module Private := Core.Time_ns.Span.Private
type t = private Core.Int63.t

t is immediate on 64bit boxes and so plays nicely with the GC write barrier.

include Ppx_hash_lib.Hashable.S with type t := t
type underlying = Core.Int63.t

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

include Bin_prot.Binable.S with type t := t
include Bin_prot.Binable.S_only_functions with type t := t
val bin_size_t : t Bin_prot.Size.sizer
val bin_write_t : t Bin_prot.Write.writer
val bin_read_t : t Bin_prot.Read.reader
val __bin_read_t__ : (int -> t) Bin_prot.Read.reader

This function only needs implementation if t exposed to be a polymorphic variant. Despite what the type reads, this does *not* produce a function after reading; instead it takes the constructor tag (int) before reading and reads the rest of the variant t afterwards.

val bin_shape_t : Bin_prot.Shape.t
val bin_writer_t : t Bin_prot.Type_class.writer
val bin_reader_t : t Bin_prot.Type_class.reader
include Ppx_hash_lib.Hashable.S with type t := t
include Sexplib0.Sexpable.S with type t := t
val t_of_sexp : Sexplib0__.Sexp.t -> t
val sexp_of_t : t -> Sexplib0__.Sexp.t
val t_sexp_grammar : t Sexplib0.Sexp_grammar.t
include Typerep_lib.Typerepable.S with type t := t
val typename_of_t : t Typerep_lib.Typename.t
include Core.Interfaces.Comparable_binable with type t := t
include Base.Comparable.S 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
val min : t -> t -> t
val max : t -> t -> t
val ascending : t -> t -> int
val descending : t -> t -> int
val between : t -> low:t -> high:t -> bool
val clamp_exn : t -> min:t -> max:t -> t
val clamp : t -> min:t -> max:t -> t Base__.Or_error.t
type comparator_witness = Core.Time_ns.Span.comparator_witness
module Replace_polymorphic_compare = Core.Time_ns.Span.Replace_polymorphic_compare
include Core.Comparable.With_zero with type t := t
include Base.Comparable.With_zero with type t := t
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__Comparable_intf.Sign.t
val validate_lbound : min:t Core.Maybe_bound.t -> t Validate.check
val validate_ubound : max:t Core.Maybe_bound.t -> t Validate.check
val validate_bound : min:t Core.Maybe_bound.t -> max:t Core.Maybe_bound.t -> t Validate.check
val validate_positive : t Validate.check
val validate_non_negative : t Validate.check
val validate_negative : t Validate.check
val validate_non_positive : t Validate.check
include Core.Interfaces.Hashable_binable with type t := t
include Ppx_hash_lib.Hashable.S with type t := t
val hash_fold_t : t Base__Ppx_hash_lib.hash_fold
val hash : t -> Base__Ppx_hash_lib.Std.Hash.hash_value
val hashable : t Core__.Hashtbl.Hashable.t
module Hash_set = Core.Time_ns.Span.Hash_set
module Hash_queue = Core.Time_ns.Span.Hash_queue
include Diffable.S_atomic with type t := t
val pp : Base__.Formatter.t -> t -> unit
include Core.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
include Core.Quickcheck.S_range with type t := t
include Core.Quickcheck_intf.S with type t := t
val quickcheck_generator : t Base_quickcheck.Generator.t
val quickcheck_observer : t Base_quickcheck.Observer.t
val quickcheck_shrinker : t Base_quickcheck.Shrinker.t
val gen_incl : t -> t -> t Base_quickcheck.Generator.t

gen_incl lower_bound upper_bound produces values between lower_bound and upper_bound, inclusive. It uses an ad hoc distribution that stresses boundary conditions more often than a uniform distribution, while still able to produce any value in the range. Raises if lower_bound > upper_bound.

val gen_uniform_incl : t -> t -> t Base_quickcheck.Generator.t

gen_uniform_incl lower_bound upper_bound produces a generator for values uniformly distributed between lower_bound and upper_bound, inclusive. Raises if lower_bound > upper_bound.

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

?sign defaults to positive. Setting it to negative is equivalent to negating all the integers.

val to_parts : t -> Parts.t
converters

conversions to and from float

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_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

conversions from int

val of_int_sec : Base.Int.t -> t
val of_int_min : Base.Int.t -> t
val of_int_hr : Base.Int.t -> t
val of_int_day : Base.Int.t -> t

conversions from other size integer types for seconds

val of_int32_seconds : Core.Int32.t -> t
val of_int63_seconds : Core.Int63.t -> t
val to_int63_seconds_round_down_exn : t -> Core.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

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 -> 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 is the smallest representable span greater than t (and therefore representation-dependent)

val prev : t -> t

prev t is the largest representable span less than t (and therefore representation-dependent)

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 -> Core.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 : Core.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:Core.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 : ?state:Core__.Import.Random.State.t -> t -> percent:Core.Percent.t -> t

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

val of_sec_with_microsecond_precision : Base.Float.t -> t
val of_int_us : Base.Int.t -> t
val of_int_ms : Base.Int.t -> t
val to_int_us : t -> Base.Int.t
val to_int_ms : t -> Base.Int.t
val to_int_sec : t -> Base.Int.t

Approximations of float conversions using multiplication instead of division.

val to_us_approx : t -> Base.Float.t
val to_ms_approx : t -> Base.Float.t
val to_sec_approx : t -> Base.Float.t
val to_min_approx : t -> Base.Float.t
val to_hr_approx : t -> Base.Float.t
val to_day_approx : t -> Base.Float.t
val min_value_representable : t

The minimum representable time span.

val max_value_representable : t

The maximum representable time span.

val min_value_for_1us_rounding : t

The minimum span that rounds to a Time.Span.t with microsecond precision.

val max_value_for_1us_rounding : t

The maximum span that rounds to a Time.Span.t with microsecond precision.

val min_value : t

An alias for min_value_for_1us_rounding.

  • deprecated [since 2019-02] use [min_value_representable] or [min_value_for_1us_rounding] instead
val max_value : t

An alias for max_value_for_1us_rounding.

  • deprecated [since 2019-02] use [max_value_representable] or [max_value_for_1us_rounding] instead
val scale_int : t -> Base.Int.t -> t

overflows silently

val scale_int63 : t -> Core.Int63.t -> t

overflows silently

val div : t -> t -> Core.Int63.t

Rounds down, and raises unless denominator is positive.

val to_int63_ns : t -> Core.Int63.t

Fast, implemented as the identity function.

val of_int63_ns : Core.Int63.t -> t

Fast, implemented as the identity function.

val to_int_ns : t -> Base.Int.t

Will raise on 32-bit platforms. Consider to_int63_ns instead.

val of_int_ns : Base.Int.t -> t
val since_unix_epoch : Base.Unit.t -> t
val random : ?state:Core__.Import.Random.State.t -> Base.Unit.t -> t
val to_span : t -> Base.Float.t

WARNING!!! to_span and of_span both round to the nearest 1us.

Around 135y magnitudes to_span and of_span raise.

  • deprecated [since 2019-01] use [to_span_float_round_nearest] or [to_span_float_round_nearest_microsecond]
val of_span : Base.Float.t -> t
  • deprecated [since 2019-01] use [of_span_float_round_nearest] or [of_span_float_round_nearest_microsecond]

*_round_nearest vs *_round_nearest_microsecond: If you don't know that you need microsecond precision, use the *_round_nearest version. *_round_nearest_microsecond is for historical purposes.

val to_span_float_round_nearest : t -> Base.Float.t
val to_span_float_round_nearest_microsecond : t -> Base.Float.t
val of_span_float_round_nearest : Base.Float.t -> t
val of_span_float_round_nearest_microsecond : Base.Float.t -> t
module Rounding_direction = Core.Time_ns.Span.Rounding_direction

These operations round the span to the nearest whole-number of the given factor.

val round_up : t -> to_multiple_of:t -> t
val round_down : t -> to_multiple_of:t -> t
val round_nearest : t -> to_multiple_of:t -> t
val round_towards_zero : t -> to_multiple_of:t -> t
val round : t -> dir:Rounding_direction.t -> to_multiple_of:t -> t
module Alternate_sexp = Core.Time_ns.Span.Alternate_sexp

Note that we expose a sexp format that is not the one exposed in Core.

val arg_type : t Command.Arg_type.t
module Option = Core.Time_ns.Span.Option

Span.Option.t is like Span.t option, except that the value is immediate on architectures where Int63.t is immediate. This module should mainly be used to avoid allocations.

module Stable = Core.Time_ns.Span.Stable
OCaml

Innovation. Community. Security.