tempo/period
Types
Functions
pub fn as_days(period: Period) -> Int
Returns the number of days in the period.
Examples
period.new(
start: naive_datetime.literal("2024-06-13T15:47:00"),
end: naive_datetime.literal("2024-06-21T07:16:12"),
)
|> period.as_days
// -> 7
pub fn as_days_fractional(period: Period) -> Float
Returns the number of days in the period.
Does not account for leap seconds like the rest of the package.
Examples
period.new(
start: naive_datetime.literal("2024-06-13T15:47:00"),
end: naive_datetime.literal("2024-06-21T07:16:12"),
)
|> period.as_days_fractional
// -> 7.645277777777778
pub fn as_duration(period: Period) -> Duration
Returns a period as a duration, losing the context of the start and end datetimes.
Example
period.new(
start: naive_datetime.literal("2024-06-13T15:47:00"),
end: naive_datetime.literal("2024-06-21T07:16:12"),
)
|> period.as_duration
|> duration.as_weeks
// -> 1
pub fn as_seconds(period: Period) -> Int
Returns the number of seconds in the period.
Does not account for leap seconds like the rest of the package.
Examples
period.new(
start: naive_datetime.literal("2024-06-13T07:16:32"),
end: naive_datetime.literal("2024-06-13T07:16:12"),
)
|> period.as_seconds
// -> 20
pub fn contains_date(period: Period, date: Date) -> Bool
Checks if a date is contained within a period, inclusive of the start and end datetimes.
Examples
period.from_month(tempo.Jun, 2024)
|> period.contains_date(date.literal("2024-06-30"))
// -> True
period.from_month(tempo.Jun, 2024)
|> period.contains_date(date.literal("2024-07-22"))
// -> False
date.literal("2024-06-13")
|> date.difference(from: date.literal("2024-06-21"))
|> period.contains_date(date.literal("2024-06-21"))
// -> True
date.literal("2024-06-13")
|> date.difference(from: date.literal("2024-06-21"))
|> period.contains_date(date.literal("2024-06-27"))
// -> False
pub fn contains_datetime(
period: Period,
datetime: DateTime,
) -> Bool
Checks if a datetime is contained within a period, inclusive of the start and end datetimes.
Examples
period.from_month(tempo.Jun, 2024)
|> period.contains_datetime(
datetime.literal("2024-06-30T24:00:00-07:00"),
)
// -> True
datetime.as_period(
start: datetime.literal("2024-06-13T15:47:00+06:00"),
end: datetime.literal("2024-06-21T07:16:12+06:00"),
)
|> period.contains_datetime(
datetime.literal("2024-06-20T07:16:12+06:00"),
)
// -> True
pub fn contains_naive_datetime(
period: Period,
naive_datetime: NaiveDateTime,
) -> Bool
Checks if a naive datetime is contained within a period, inclusive of the start and end datetimes.
Examples
period.from_month(tempo.Jun, 2024)
|> period.contains_naive_datetime(
naive_datetime.literal("2024-06-30T24:00:00"),
)
// -> True
period.from_month(tempo.Jun, 2024)
|> period.contains_naive_datetime(
naive_datetime.literal("2024-07-22T24:00:00"),
)
// -> False
date.literal("2024-06-13")
|> date.difference(from: date.literal("2024-06-21"))
|> period.contains_naive_datetime(
naive_datetime.literal("2024-06-21T13:50:00"),
)
// -> False
date.as_period(
start: date.literal("2024-06-13"),
end: date.literal("2024-06-21"),
)
|> period.contains_naive_datetime(
naive_datetime.literal("2024-06-21T13:50:00"),
)
// -> True
pub fn from_month(month: Month, year: Int) -> Period
Creates a period of the specified month, starting at 00:00:00 on the first day of the month and ending at 24:00:00 on the last day of the month.
Examples
period.from_month(tempo.Feb, 2024)
|> period.contains_date(date.literal("2024-06-21"))
// -> False
pub fn new(start start: DateTime, end end: DateTime) -> Period
Creates a new period from the start and end datetimes.
Examples
period.new(
start: datetime.literal("2024-06-13T15:47:00+06:00"),
end: datetime.literal("2024-06-21T07:16:12+06:00"),
)
|> period.as_days
// -> 7
pub fn new_naive(
start start: NaiveDateTime,
end end: NaiveDateTime,
) -> Period
Creates a new period from the start and end naive datetimes.
Examples
period.new_naive(
start: naive_datetime.literal("2024-06-13T15:47:00"),
end: naive_datetime.literal("2024-06-21T07:16:12"),
)
|> period.as_days
// -> 7