calendar/date
Types
A Date struct and functions.
The Date struct contains the fields year, month, day and calendar.
New dates can be built with the new function.
pub type Date {
Date(year: Int, month: Int, day: Int, calendar: String)
}
Constructors
-
Date(year: Int, month: Int, day: Int, calendar: String)
pub type DateError {
InvalidDate
InvalidYear
InvalidMonth
InvalidDay
InvalidFormat
InvalidCalendar
IncompatibleCalendars
}
Constructors
-
InvalidDate -
InvalidYear -
InvalidMonth -
InvalidDay -
InvalidFormat -
InvalidCalendar -
IncompatibleCalendars
pub type DateFormat {
Extended
Basic
}
Constructors
-
Extended -
Basic
Values
pub fn after(date1: Date, date2: Date) -> Bool
Returns true if the first date is strictly later than the second.
pub fn before(date1: Date, date2: Date) -> Bool
Returns true if the first date is strictly earlier than the second.
pub fn beginning_of_month(date: Date) -> Date
Calculates a date that is the first day of the month for the given date.
pub fn beginning_of_week(date: Date) -> Date
Calculates a date that is the first day of the week for the given date.
pub fn beginning_of_week_starting_on(
date: Date,
starting_on: Int,
) -> Date
Calculates beginning of week with custom starting day.
pub fn compare(date1: Date, date2: Date) -> order.Order
Compare two dates. Returns order.Lt, order.Eq, or order.Gt.
pub fn convert(
date: Date,
target_calendar: String,
) -> Result(Date, DateError)
Converts the given date from its calendar to the given calendar.
pub fn day_of_era(date: Date) -> #(Int, Int)
Calculates the day-of-era and era for a given calendar date.
pub fn day_of_week(date: Date) -> Int
Calculates the ordinal day of the week of a given date.
pub fn day_of_week_starting_on(
date: Date,
starting_on: Int,
) -> Int
Calculates the ordinal day of the week with custom starting day.
pub fn day_of_year(date: Date) -> Int
Calculates the day of the year of a given date.
pub fn days_in_month(year: Int, month: Int) -> Int
Returns the number of days in a given month and year.
pub fn days_in_month_for_date(date: Date) -> Int
Returns the number of days in the given date month.
pub fn diff(date1: Date, date2: Date) -> Int
Calculates the difference between two dates, in a full number of days.
pub fn end_of_month(date: Date) -> Date
Calculates a date that is the last day of the month for the given date.
pub fn end_of_week(date: Date) -> Date
Calculates a date that is the last day of the week for the given date.
pub fn end_of_week_starting_on(
date: Date,
starting_on: Int,
) -> Date
Calculates end of week with custom starting day.
pub fn from_days_since_unix_epoch(
days: Int,
calendar: String,
) -> Result(Date, DateError)
Create a date from days since Unix epoch (1970-01-01).
pub fn from_erl(
tuple: #(Int, Int, Int),
) -> Result(Date, DateError)
Converts an Erlang date tuple to a Date struct.
pub fn from_erl_with_calendar(
tuple: #(Int, Int, Int),
calendar: String,
) -> Result(Date, DateError)
Converts an Erlang date tuple to a Date struct with specified calendar.
pub fn from_gregorian_days(days: Int) -> Date
Converts a number of gregorian days to a Date struct.
pub fn from_gregorian_days_with_calendar(
days: Int,
calendar: String,
) -> Date
Converts a number of gregorian days to a Date struct with specified calendar.
pub fn from_iso8601_with_calendar(
string: String,
calendar: String,
) -> Result(Date, DateError)
Parses an ISO 8601 date string with specified calendar.
pub fn from_timestamp(timestamp: Int) -> Result(Date, DateError)
Create a date from Unix timestamp (seconds since epoch).
pub fn leap_year(date: Date) -> Bool
Returns true if the year in the given date is a leap year.
pub fn months_in_year(date: Date) -> Int
Returns the number of months in the given date year.
pub fn new(
year: Int,
month: Int,
day: Int,
calendar: String,
) -> Result(Date, DateError)
Creates a new Date struct.
pub fn new_iso(
year: Int,
month: Int,
day: Int,
) -> Result(Date, DateError)
Creates a new Date struct with ISO calendar as default.
pub fn new_simple(
year: Int,
month: Int,
day: Int,
) -> Result(Date, DateError)
Creates a new Date struct with ISO calendar as default.
pub fn quarter_of_year(date: Date) -> Int
Calculates the quarter of the year of a given date.
pub fn range(
first: Date,
last: Date,
) -> Result(List(Date), DateError)
Creates a date range between two dates.
pub fn range_with_step(
first: Date,
last: Date,
step: Int,
) -> Result(List(Date), DateError)
Creates a date range with a step.
pub fn shift(
date: Date,
duration: duration.Duration,
) -> Result(Date, DateError)
Shifts given date by duration according to its calendar.
pub fn subtract_days(
date: Date,
days: Int,
) -> Result(Date, DateError)
Subtract days from a date.
pub fn to_erl(date: Date) -> #(Int, Int, Int)
Converts the given date to an Erlang date tuple.
pub fn to_gregorian_days(date: Date) -> Int
Converts a date struct to a number of gregorian days.
pub fn to_iso8601_with_format(
date: Date,
format: DateFormat,
) -> String
Converts the given date to ISO 8601 with format option.
pub fn to_string(date: Date) -> String
Converts a Date to a string in ISO8601 format (YYYY-MM-DD).
pub fn to_timestamp(date: Date) -> Int
Convert date to Unix timestamp (approximation).
pub fn utc_today_with_calendar(calendar: String) -> Date
Returns the current date in UTC with specified calendar.
pub fn year_of_era(date: Date) -> #(Int, Int)
Calculates the year-of-era and era for a given calendar year.