package lunar

  1. Overview
  2. Docs

Module Lunar.Duration

Describes a duration in seconds (Lunar is essentially a Calendar library, so greater precision does not seem particularly useful). This is the most primitive type in the library, and the API exposes minimal set of arithmetic operations.

Type

A duration is an abstract representation of Int64.t.

type t

A type describing a duration.

Creating durations

val zero : t

zero is zero second.

val one : t

one is one second.

val from_int64 : int64 -> t

from_int64 i converts int64 to duration (without applying multiplication, so expressed in seconds).

val from_seconds : int -> t

from_seconds i converts the given int i into a duration.

val from_minutes : int -> t

from_minutes i converts the given int i into a duration. from_minutes 1 = from_seconds 60

val from_hours : int -> t

from_hours i converts the given int i into a duration. from_hours 1 = from_seconds 3600

val from_days : int -> t

from_days i converts the given int i into a duration. from_days 1 = from_seconds 86400

val from_datetime : year:int -> month:int -> day:int -> hour:int -> min:int -> sec:int -> t

from_datetime ~year ~month ~day ~hour ~min ~sec builds a duration based on a date. Duration is the number of seconds that have elapsed since January 1, 1970, at 00:00:00.

Warning: The function is generally lax, performing no checks on input arguments, and is mainly used internally.

Conversion

val to_datetime : t -> int * int * int * int * int * int

to_datetime d returns an approximation of the date calculated based on the number of seconds elapsed since January 1, 1970, at 00:00:00. The return value is in the form of the following tuple: year * month * day_of_month * hour * minute * second.

val to_int64 : t -> int64

to_int64 d returns the int64 representation of a duration.

val wdhms : t -> int * int * int * int * int

wdhms duration returns the number of weeks, days, hours, minutes, and seconds that describe the duration.

val dhms : t -> int * int * int * int

dhms duration returns the number of days, hours, minutes, and seconds that describe the duration.

val hms : t -> int * int * int

hms duration returns the number of hours, minutes, and seconds that describe the duration.

val to_days : t -> int

to_days duration get a day approx for a duration.

val weekday : t -> Weekday.t

weekday d returns the Weekday.t for the given duration since epoch.

Comparison

val compare : t -> t -> int

compare a b comparison between duration, following OCaml convention.

val equal : t -> t -> bool

equal a b equality between duration.

val min : t -> t -> t

min a b returns the smaller of two arguments.

val max : t -> t -> t

max a b returns the greater of two arguments.

val clamp : min:t -> max:t -> t -> t

clamp ~min ~max x restricts the x to the inclusive interval [min, max].

val is_earlier : than:t -> t -> bool

is_earlier ~than x returns true if x is (strictly) earlier than than, false otherwise.

val is_later : than:t -> t -> bool

is_later ~than x returns true if x is (strictly) later than than, false otherwise.

Arithmetic operation

val abs : t -> t

abs d returns the absolute value of d.

val add : t -> t -> t

add a b is the addition of a and b.

val sub : t -> t -> t

sub a b is the substraction of a and b.

val mul : t -> int -> t

mul d i multiplies a duration by an integer.

val neg : t -> t

neg x is -x.

val succ : t -> t

succ d adds 1 second to d.

val pred : t -> t

pred d remove 1 second to d.

Infix Operators

module Infix : sig ... end

Common and useful infix operators.

include module type of Infix
val (+) : t -> t -> t

d1 + d2 is add d1 d2, see add.

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

d1 - d2 is sub d1 d2, see sub.

val (*) : t -> int -> t

d * i is mul d i, see mul.

val (=) : t -> t -> bool

v1 = v2 is equal v1 v2.

val (<>) : t -> t -> bool

v1 <> v2 is not (equal v1 v2).

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

v1 > v2 returns true if v1 is greater than v2, false otherwise.

val (>=) : t -> t -> bool

v1 >= v2 returns true if v1 is greater or equal to v2, false otherwise.

val (<) : t -> t -> bool

v1 < v2 returns true if v2 is greater than v1, false otherwise.

val (<=) : t -> t -> bool

v1 <= v2 returns true if v2 is greater or equal to v1, false otherwise.