package lunar
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=f158deac67b864f3edd7d1242b3340a0f218d4465b9e4108501a02197594d0f9
sha512=acf9a3ea508c3df2351aec6ea8299583be128f02d2d891ab82a2999ad6ba00c967962ede7d96782c27a7edcb7f78db08380293a8bf044a567a749872bbc417a8
doc/lunar/Lunar/Datetime/index.html
Module Lunar.DatetimeSource
Represents a Datetime. A Date.t associated with a Time.t.
Types
The type describing a Datetime.
type error = | Invalid_date of Date.error| Invalid_time of Time.error| Invalid of Date.error * Time.error| Invalid_string of string
Type listing errors that may occur when working with datetime.
Building datetime
val make :
?at:(int * int * int) ->
year:int ->
month:Month.t ->
day:int ->
unit ->
(t, error) resultmake ?at ~year ~month ~day () create and validate a datetime.
make_exn ?at ~year ~month ~day () create and validate a datetime like make but raise Invalid_datetime if the validation doesn't succeed.
make_exn' ?at ~year ~month ~day () create and validate a datetime, like make' but raise Invalid_datetime if the validation doesn't succeed.
from_string s try to read a date from a string (using the format year-mon-dayThh:mm:ss).
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.
from d t creates a datetime object for the given date, d and a given time t.
from_date d creates a datetime object for the given date, d, at midnight.
from_duration d converts a duration to a date. 0 is the 1970-01-01 at 00:00:00.
Accessors
Information and facts about Datetime.
year_of_era dt returns the year of the associated era for the given datetime dt.
century d returns the century of the associated era for the given datetime dt.
year_of_century d returns the year of the century of the associated era for the given datetime dt.
quarter dt Returns the quarter number (1-4) in which the given datetime dt falls.
days_in_month dt returns the number of days in the month of the given datetime dt.
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.
See day_of_month.
See day_of_week.
Component lenses
A datetime is simply a pair consisting of a Date.t and Time.t.
on_date f dt apply f on the date part of the given datetime (dt).
on_time f dt apply f on the time part of the given datetime (dt).
map_date f dt update the date part of the given datetime d using f.
map_time f dt update the time part of the given datetime d using f.
Operation on datetimes
add duration dt compute a new date adding duration to the given dt.
sub duration dt compute a new date substracting duration to the given dt.
add_seconds number_of_seconds datetime add number_of_seconds to the given datetime.
sub_seconds number_of_seconds datetime remove number_of_seconds to the given datetime.
add_minutes number_of_minutes datetime add number_of_minutes to the given datetime.
sub_minutes number_of_minutes datetime remove number_of_minutes to the given datetime.
add_hours number_of_hours datetime add number_of_hours to the given datetime.
sub_hours number_of_hours datetime remove number_of_hours to the given datime.
add_days number_of_days datetime add number_of_days to the given datetime.
sub_days number_of_days datetime remove number_of_days to the given datetime.
add_weeks number_of_weeks datetime add number_of_weeks to the given datetime (a week is 7 days).
sub_weeks number_of_weeks datetime remove number_of_weeks to the given datetime (a week is 7 days).
add_months number_of_months datetime add number_of_months to the given datetime.
add_months number_of_months datetime remove number_of_months to the given datetime.
add_quarters number_of_quarters datetime add number_of_quarters to the given datetime.
add_quarters number_of_quarters datetime remove number_of_quarters to the given datetime.
add_years number_of_years datetime add number_of_years to the given datetime.
sub_years number_of_years datetime remove number_of_years to the given datetime.
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.
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.
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.
succ_day_of_week weekday from returns the first following datetime corresponding to the specified day of the week.
pred_day_of_week weekday from returns the first previous datetime corresponding to the specified day of the week.
succ_weekday from returns the first following day of week (not weekend).
pred_weekday from returns the first previous day of week (not weekend).
succ_week dt returns the first day of the next week.
pred_week dt returns the first day of the previous week.
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.
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
with_time time datetime replace the time of the given datetime.
with_date date datetime replace the date of the given datetime.
start_of_minute dt returns the datetime at the start of the current minute.
end_of_minute dt returns the datetime at the end of the current minute.
start_of_hour dt returns the datetime at the start of the current hour.
start_of_day dt Returns a datetime set to the start of the day (midnight).
at_noon is start_of_afternoon.
start_of_week ?week_start dt Returns the first day of the week (defined by week_start; default: Monday).
end_of_week ?week_start dt Returns the last day of the week (defined by week_start - 1; default: Monday).
start_of_month dt returns the first day of the month of the given datetime dt.
end_of_month dt returns the last day of the month of the given datetime dt.
start_of_quarter dt returns the first day of the quarter of the given datetime dt.
end_of_quarter dt returns the last day of the quarter of the given datetime dt.
start_of_year dt returns the first day of the year of the given datetime d.
end_of_year dt returns the last day of the year of the given datetime dt.
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.
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.
floor resolution d is truncate resolution d.
round resolution d rounds d to the nearest boundary of the period specified by resolution. See Date.round and Time.round.
Predicates
is_am dt returns true if the time of the given dt is between 00:00:00 and 11:59:59, false otherwise.
is_pm dt returns true if the time of the given dt is between 12:00:00 and 23:59:59, false otherwise.
is_noon dt returns true if the time of the given dt is exactly 12:00:00, false otherwise.
is_midnight dt returns true if the time of the given dt is exactly 00:00:00, false otherwise.
is_morning dt returns true if the time of the given dt is between 05:00:00 and 11:59:59, false otherwise.
is_afternoon dt returns true if the time of the given dt is between 12:00:00 and 16:59:59, false otherwise.
is_evening dt returns true if the time of the given dt is between 17:00:00 and 20:59:59, false otherwise.
is_night dt returns true if the time of the given dt is between 21:00:00 and 04:59:59, false otherwise.
is_weekend dt returns true if the date of the given dt is on Saturday or Sunday.
is_weekday dt returns true if the date of the given dt is not on Saturday or Sunday.
is_day_of_week wd d returns true if the current day of week is the given one wd, false otherwise.
is_first_day_of_week ?week_start d returns true if the given date d is the first day of the week.
is_last_day_of_week ?week_start d returns true if the given date d is the last day of the week.
is_leap_year dt returns true if year d is a leap year, false otherwise.
is_first_day_of_month dt returns true if the date of the given dt is the first day of the month.
is_last_day_of_month dt returns true if the date of the given dt is the last day of the month.
is_first_day_of_quarter d returns true if the date of the given dt is the first day of the current quarter.
is_last_day_of_q d returns true if the date of the given dt is the last day of the current quarter.
is_first_day_of_year dt returns true if the date of the given dt is the first day of the year.
is_last_day_of_year dt returns true if the date of the given dt is the last day of the year.
Comparison
clamp ~min ~max x restricts the x to the inclusive interval [min, max].
is_earlier ~than x returns true if x is (strictly) earlier than than, false otherwise.
is_later ~than x returns true if x is (strictly) later than than, false otherwise.
Conversion
to_duration dt returns a duration since epoch for the given datetime dt.