package lunar

  1. Overview
  2. Docs

doc/lunar/Lunar/Datetime/index.html

Module Lunar.DatetimeSource

Represents a Datetime. A Date.t associated with a Time.t.

Types

Sourcetype t

The type describing a Datetime.

Sourcetype error =
  1. | Invalid_date of Date.error
  2. | Invalid_time of Time.error
  3. | Invalid of Date.error * Time.error
  4. | Invalid_string of string

Type listing errors that may occur when working with datetime.

Sourceexception Invalid_datetime of error

An exception used for unsafe function.

Building datetime

Sourceval make : ?at:(int * int * int) -> year:int -> month:Month.t -> day:int -> unit -> (t, error) result

make ?at ~year ~month ~day () create and validate a datetime.

Sourceval make' : ?at:(int * int * int) -> year:int -> month:int -> day:int -> unit -> (t, error) result

make' ?at ~year ~month ~day () create and validate a datetime. see make. Take an integer rather than a Month.t.

Sourceval make_exn : ?at:(int * int * int) -> year:int -> month:Month.t -> day:int -> unit -> t

make_exn ?at ~year ~month ~day () create and validate a datetime like make but raise Invalid_datetime if the validation doesn't succeed.

Sourceval make_exn' : ?at:(int * int * int) -> year:int -> month:int -> day:int -> unit -> t

make_exn' ?at ~year ~month ~day () create and validate a datetime, like make' but raise Invalid_datetime if the validation doesn't succeed.

Sourceval from_string : string -> (t, error) result

from_string s try to read a date from a string (using the format year-mon-dayThh:mm:ss).

Sourceval from_string_exn : string -> t

from_string_exn s try to read a date from a string (using the format year-mon-dayThh:mm:ss) and raise and exception if it fails.

Sourceval from : Date.t -> Time.t -> t

from d t creates a datetime object for the given date, d and a given time t.

Sourceval from_date : Date.t -> t

from_date d creates a datetime object for the given date, d, at midnight.

Sourceval from_duration : Duration.t -> t

from_duration d converts a duration to a date. 0 is the 1970-01-01 at 00:00:00.

Sourceval epoch : t

Returns the 1st January 1970 at midnight.

Accessors

Information and facts about Datetime.

Sourceval date : t -> Date.t

date dt returns the date part of the datetime.

Sourceval time : t -> Time.t

time dt returns the time part of the datetime.

Sourceval hour : t -> int

hour dt returns the hour.

Sourceval minute : t -> int

hour dt returns the minute.

Sourceval second : t -> int

hour dt returns the second.

Sourceval era : t -> Era.t

era dt returns the era of the given datetime dt.

Sourceval year_of_era : t -> int

year_of_era dt returns the year of the associated era for the given datetime dt.

Sourceval century_of_era : t -> int

century d returns the century of the associated era for the given datetime dt.

Sourceval year_of_century : t -> int

year_of_century d returns the year of the century of the associated era for the given datetime dt.

Sourceval year : t -> int

year dt returns the year from a datetime.

Sourceval quarter : t -> int

quarter dt Returns the quarter number (1-4) in which the given datetime dt falls.

Sourceval month : t -> Month.t

month dt returns the month from a datetime.

Sourceval day_of_month : t -> int

day_of_month dt returns the day of month from a datetime.

Sourceval day_of_week : t -> Weekday.t

day_of_week dt returns the day of week from a datetime.

Sourceval days_in_month : t -> int

days_in_month dt returns the number of days in the month of the given datetime dt.

Sourceval day_of_year : t -> int

day_of_year dt returns the day of year from a datetime.

Sourceval week_of_year : t -> int * int

week_of_year dt returns the pair year and week number for a given datetime dt. See Date.week_of_year.

Aliases

A few aliases for common terms.

Sourceval day : t -> int
Sourceval weekday : t -> Weekday.t

Component lenses

A datetime is simply a pair consisting of a Date.t and Time.t.

Sourceval to_pair : t -> Date.t * Time.t

to_pair dt convert the given datetime dt into a pair of Date.t and Time.t

Sourceval on_date : (Date.t -> 'a) -> t -> 'a

on_date f dt apply f on the date part of the given datetime (dt).

Sourceval on_time : (Time.t -> 'a) -> t -> 'a

on_time f dt apply f on the time part of the given datetime (dt).

Sourceval map_date : (Date.t -> Date.t) -> t -> t

map_date f dt update the date part of the given datetime d using f.

Sourceval map_time : (Time.t -> Time.t) -> t -> t

map_time f dt update the time part of the given datetime d using f.

Operation on datetimes

Sourceval add : Duration.t -> t -> t

add duration dt compute a new date adding duration to the given dt.

Sourceval sub : Duration.t -> t -> t

sub duration dt compute a new date substracting duration to the given dt.

Sourceval add_seconds : int -> t -> t

add_seconds number_of_seconds datetime add number_of_seconds to the given datetime.

Sourceval sub_seconds : int -> t -> t

sub_seconds number_of_seconds datetime remove number_of_seconds to the given datetime.

Sourceval add_minutes : int -> t -> t

add_minutes number_of_minutes datetime add number_of_minutes to the given datetime.

Sourceval sub_minutes : int -> t -> t

sub_minutes number_of_minutes datetime remove number_of_minutes to the given datetime.

Sourceval add_hours : int -> t -> t

add_hours number_of_hours datetime add number_of_hours to the given datetime.

Sourceval sub_hours : int -> t -> t

sub_hours number_of_hours datetime remove number_of_hours to the given datime.

Sourceval add_days : int -> t -> t

add_days number_of_days datetime add number_of_days to the given datetime.

Sourceval sub_days : int -> t -> t

sub_days number_of_days datetime remove number_of_days to the given datetime.

Sourceval add_weeks : int -> t -> t

add_weeks number_of_weeks datetime add number_of_weeks to the given datetime (a week is 7 days).

Sourceval sub_weeks : int -> t -> t

sub_weeks number_of_weeks datetime remove number_of_weeks to the given datetime (a week is 7 days).

Sourceval add_months : int -> t -> t

add_months number_of_months datetime add number_of_months to the given datetime.

Sourceval sub_months : int -> t -> t

add_months number_of_months datetime remove number_of_months to the given datetime.

Sourceval add_quarters : int -> t -> t

add_quarters number_of_quarters datetime add number_of_quarters to the given datetime.

Sourceval sub_quarters : int -> t -> t

add_quarters number_of_quarters datetime remove number_of_quarters to the given datetime.

Sourceval add_years : int -> t -> t

add_years number_of_years datetime add number_of_years to the given datetime.

Sourceval sub_years : int -> t -> t

sub_years number_of_years datetime remove number_of_years to the given datetime.

Sourceval diff : t -> t -> Duration.t

diff d1 d2 returns the difference (in Duration.t) between d1 and d2.

Succ and Pred

The main difference between the add/sub and succ/pred operations lies in how the result is truncated. add and sub are standard arithmetic operations: you add or subtract a duration. succ and pred, on the other hand, calculate the next datetime step. See Time.succ and Date.succ.

Sourceval succ : t -> t

succ dt is add_seconds 1.

Sourceval pred : t -> t

pred dt is sub_seconds 1.

Sourceval succ_second : t -> t

succ_second dt returns the datetime at the next second. See succ.

Sourceval pred_second : t -> t

pred_second dt returns the datetime at the previous second. See pred.

Sourceval succ_minute : t -> t

succ_minute dt returns the datetime at the next minute.

Sourceval pred_minute : t -> t

pred_minute t returns the datetime at the previous minute.

Sourceval succ_hour : t -> t

succ_hour t returns the datetime at the next hour.

Sourceval pred_hour : t -> t

pred_hour t returns the datetime at the previous hour.

Sourceval succ_day : ?where:(Date.t -> bool) -> t -> t

succ_day ?where:pred from returns the first following datetime that satisfies the predicate pred starting from the date from (exclusive). Note: If the predicate always returns false, the function never terminates.

Sourceval pred_day : ?where:(Date.t -> bool) -> t -> t

pred_day ~where:pred from returns the first previous datetime that satisfies the predicate pred starting from the date from (exclusive). Note: If the predicate always returns false, the function never terminates.

Sourceval succ_day_of_week : Weekday.t -> t -> t

succ_day_of_week weekday from returns the first following datetime corresponding to the specified day of the week.

Sourceval pred_day_of_week : Weekday.t -> t -> t

pred_day_of_week weekday from returns the first previous datetime corresponding to the specified day of the week.

Sourceval succ_weekday : t -> t

succ_weekday from returns the first following day of week (not weekend).

Sourceval pred_weekday : t -> t

pred_weekday from returns the first previous day of week (not weekend).

Sourceval succ_week : ?week_start:Weekday.t -> t -> t

succ_week dt returns the first day of the next week.

Sourceval pred_week : ?week_start:Weekday.t -> t -> t

pred_week dt returns the first day of the previous week.

Sourceval succ_month : t -> t

succ_month dt returns the first day of the next month.

Sourceval pred_month : t -> t

pred_month dt returns the first day of the previous month.

Sourceval succ_quarter : t -> t

succ_quarter dt returns the first day of the next quarter.

Sourceval pred_quarter : t -> t

pred_quarter dt returns the first day of the previous quarter.

Sourceval succ_year : t -> t

succ_year dt returns the first day of the next year.

Sourceval pred_year : t -> t

pred_year dt returns the first day of the previous year.

On duration

Arithmetic operations, such as add and sub, rely on conversions to Duration.t, which means, for example, that in the expression: Infix.(date + d1 + d2 + d3), datetime is converted to duration, added, converted back to date, added again, and so on.

For simply adding a single value, this is fine, but when you want to build more complex operations, this back-and-forth is a bit tedious. as_duration allows you to avoid these trips back and forth.

Sourceval as_duration : (Duration.t -> Duration.t) -> t -> t

as_duration f dt Converts the given datetime dt to a duration, applies the function f to this duration, and returns the result as a date. Useful for performing multiple operations on a single datetime.

Common Operations

Sourceval with_time : Time.t -> t -> t

with_time time datetime replace the time of the given datetime.

Sourceval with_date : Date.t -> t -> t

with_date date datetime replace the date of the given datetime.

Sourceval tomorrow : t -> t

tomorrow dt get the next day of the given dt. See succ_day.

Sourceval yesterday : t -> t

yesterday dt get the previous day of the given dt. See pred_day.

Sourceval start_of_minute : t -> t

start_of_minute dt returns the datetime at the start of the current minute.

Sourceval end_of_minute : t -> t

end_of_minute dt returns the datetime at the end of the current minute.

Sourceval start_of_hour : t -> t

start_of_hour dt returns the datetime at the start of the current hour.

Sourceval end_of_hour : t -> t

end_of_hour dt returns the datetime at the end of the current hour.

Sourceval start_of_day : t -> t

start_of_day dt Returns a datetime set to the start of the day (midnight).

Sourceval end_of_day : t -> t

end_of_day dt Returns a datetime set to the end of the day (23:59:59).

Sourceval start_of_morning : t -> t

start_of_morning dt returns the datetime at 05:00:00.

Sourceval end_of_morning : t -> t

end_of_morning dt returns the datetime at 11:59:59.

Sourceval start_of_afternoon : t -> t

start_of_afternoon dt returns the datetime at 12:00:00.

Sourceval at_noon : t -> t

at_noon is start_of_afternoon.

Sourceval end_of_afternoon : t -> t

end_of_afternoon dt returns the datetime at 16:59:59.

Sourceval start_of_evening : t -> t

start_of_evening dt returns the datetime at 17:00:00.

Sourceval end_of_evening : t -> t

end_of_evening dt returns the datetime at 20:59:59.

Sourceval start_of_night : t -> t

start_of_night dt returns the datetime at 21:00:00.

Sourceval end_of_night : t -> t

end_of_night dt returns the datetime at 04:59:59.

Sourceval start_of_week : ?week_start:Weekday.t -> t -> t

start_of_week ?week_start dt Returns the first day of the week (defined by week_start; default: Monday).

Sourceval end_of_week : ?week_start:Weekday.t -> t -> t

end_of_week ?week_start dt Returns the last day of the week (defined by week_start - 1; default: Monday).

Sourceval start_of_month : t -> t

start_of_month dt returns the first day of the month of the given datetime dt.

Sourceval end_of_month : t -> t

end_of_month dt returns the last day of the month of the given datetime dt.

Sourceval start_of_quarter : t -> t

start_of_quarter dt returns the first day of the quarter of the given datetime dt.

Sourceval end_of_quarter : t -> t

end_of_quarter dt returns the last day of the quarter of the given datetime dt.

Sourceval start_of_year : t -> t

start_of_year dt returns the first day of the year of the given datetime d.

Sourceval end_of_year : t -> t

end_of_year dt returns the last day of the year of the given datetime dt.

Sourceval age : birthday:Date.t -> t -> int

age ~birthday current returns the age calculated from birthday using the given datetime current as the current date.

Round and truncate

A negative duration can produce very strange results, which is why durations are converted to absolute values in rounding functions.

Sourceval truncate : [< Resolution.t ] -> t -> t

truncate resolution d truncates d to the beginning of the period specified by resolution. See Date.truncate and Time.truncate.

All units smaller than resolution are discarded.

Sourceval floor : [< Resolution.t ] -> t -> t

floor resolution d is truncate resolution d.

Sourceval round : [< Resolution.t ] -> t -> t

round resolution d rounds d to the nearest boundary of the period specified by resolution. See Date.round and Time.round.

Sourceval ceil : [< Resolution.t ] -> t -> t

ceil resolution d rounds d up to the next boundary of the period specified by resolution. If d is already aligned with resolution, it is returned unchanged. See Date.ceil and Time.ceil.

Predicates

Sourceval is_am : t -> bool

is_am dt returns true if the time of the given dt is between 00:00:00 and 11:59:59, false otherwise.

Sourceval is_pm : t -> bool

is_pm dt returns true if the time of the given dt is between 12:00:00 and 23:59:59, false otherwise.

Sourceval is_noon : t -> bool

is_noon dt returns true if the time of the given dt is exactly 12:00:00, false otherwise.

Sourceval is_midnight : t -> bool

is_midnight dt returns true if the time of the given dt is exactly 00:00:00, false otherwise.

Sourceval is_morning : t -> bool

is_morning dt returns true if the time of the given dt is between 05:00:00 and 11:59:59, false otherwise.

Sourceval is_afternoon : t -> bool

is_afternoon dt returns true if the time of the given dt is between 12:00:00 and 16:59:59, false otherwise.

Sourceval is_evening : t -> bool

is_evening dt returns true if the time of the given dt is between 17:00:00 and 20:59:59, false otherwise.

Sourceval is_night : t -> bool

is_night dt returns true if the time of the given dt is between 21:00:00 and 04:59:59, false otherwise.

Sourceval is_weekend : t -> bool

is_weekend dt returns true if the date of the given dt is on Saturday or Sunday.

Sourceval is_weekday : t -> bool

is_weekday dt returns true if the date of the given dt is not on Saturday or Sunday.

Sourceval is_day_of_week : Weekday.t -> t -> bool

is_day_of_week wd d returns true if the current day of week is the given one wd, false otherwise.

Sourceval is_first_day_of_week : ?week_start:Weekday.t -> t -> bool

is_first_day_of_week ?week_start d returns true if the given date d is the first day of the week.

Sourceval is_last_day_of_week : ?week_start:Weekday.t -> t -> bool

is_last_day_of_week ?week_start d returns true if the given date d is the last day of the week.

Sourceval is_leap_year : t -> bool

is_leap_year dt returns true if year d is a leap year, false otherwise.

Sourceval is_first_day_of_month : t -> bool

is_first_day_of_month dt returns true if the date of the given dt is the first day of the month.

Sourceval is_last_day_of_month : t -> bool

is_last_day_of_month dt returns true if the date of the given dt is the last day of the month.

Sourceval is_first_day_of_quarter : t -> bool

is_first_day_of_quarter d returns true if the date of the given dt is the first day of the current quarter.

Sourceval is_last_day_of_quarter : t -> bool

is_last_day_of_q d returns true if the date of the given dt is the last day of the current quarter.

Sourceval is_first_day_of_year : t -> bool

is_first_day_of_year dt returns true if the date of the given dt is the first day of the year.

Sourceval is_last_day_of_year : t -> bool

is_last_day_of_year dt returns true if the date of the given dt is the last day of the year.

Comparison

Sourceval equal : t -> t -> bool

Equality between dates.

Sourceval compare : t -> t -> int

compare a b comparison between dates, following OCaml convention.

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.

Conversion

Sourceval to_duration : t -> Duration.t

to_duration dt returns a duration since epoch for the given datetime dt.

Sourceval to_string : t -> string

to_string dt returns a string representation of the given dt.

Map, Set and Range

Sourcemodule Map : Map.S with type key = t
Sourcemodule Set : Set.S with type elt = t
Sourcemodule Range : sig ... end

Infix Operators

Sourcemodule Infix : sig ... end

Common and useful infix operators.

include module type of Infix
Sourceval (+) : t -> Duration.t -> t

d + dur is add

Sourceval (-) : t -> Duration.t -> t

d - dur is sub

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.