tempo/naive_datetime
Functions
pub fn add(
datetime: NaiveDateTime,
duration duration_to_add: Duration,
) -> NaiveDateTime
Adds a duration to a naive datetime.
Examples
naive_datetime.literal("2024-06-21T23:17:00")
|> naive_datetime.add(duration.minutes(3))
// -> naive_datetime.literal("2024-06-21T23:20:00")
pub fn compare(a: NaiveDateTime, to b: NaiveDateTime) -> Order
Compares two naive datetimes.
Examples
naive_datetime.literal("2024-06-21T23:17:00")
|> naive_datetime.compare(to: naive_datetime.literal("2024-06-21T23:17:00"))
// -> order.Eq
naive_datetime.literal("2023-05-11T13:15:00")
|> naive_datetime.compare(to: naive_datetime.literal("2024-06-21T23:17:00"))
// -> order.Lt
pub fn difference(
from a: NaiveDateTime,
to b: NaiveDateTime,
) -> Period
Returns the difference between two naive datetimes as a period between them.
Examples
naive_datetime.literal("2024-06-12T23:17:00")
|> naive_datetime.difference(
from: naive_datetime.literal("2024-06-16T01:16:12"),
)
|> period.as_days
// -> 3
naive_datetime.literal("2024-06-12T23:17:00")
|> naive_datetime.difference(
from: naive_datetime.literal("2024-06-16T01:18:12"),
)
|> period.format
// -> "3 days, 2 hours, and 1 minute"
pub fn drop_time(datetime: NaiveDateTime) -> NaiveDateTime
Drops the time of a naive datetime, setting it to zero.
Examples
naive_datetime.literal("2024-06-13T23:17:00")
|> naive_datetime.drop_time
// -> datetime.literal("2024-06-13T00:00:00")
pub fn from_string(
datetime: String,
) -> Result(NaiveDateTime, Error)
Parses a naive datetime string in the format YYYY-MM-DDThh:mm:ss.s
,
YYYY-MM-DD hh:mm:ss.s
, YYYY-MM-DD
, YYYY-M-D
, YYYY/MM/DD
,
YYYY/M/D
, YYYY.MM.DD
, YYYY.M.D
, YYYY_MM_DD
, YYYY_M_D
,
YYYY MM DD
, YYYY M D
, or YYYYMMDD
.
Examples
naive_datetime.from_string("20240612")
// -> Ok(naive_datetime.literal("2024-06-12T00:00:00"))
naive_datetime.from_string("2024-06-21 23:17:00")
// -> Ok(naive_datetime.literal("2024-06-21T23:17:00"))
naive_datetime.from_string("24-06-12|23:17:00")
// -> Error(tempo.NaiveDateTimeInvalidFormat)
pub fn get_date(datetime: NaiveDateTime) -> Date
Gets the date of a naive datetime.
Examples
naive_datetime.literal("2024-06-21T23:17:00")
|> naive_datetime.get_date
// -> date.literal("2024-06-21")
pub fn get_time(datetime: NaiveDateTime) -> Time
Gets the time of a naive datetime.
Examples
naive_datetime.literal("2024-06-21T23:17:00")
|> naive_datetime.get_time
// -> time.literal("23:17:00")
pub fn is_earlier(
a: NaiveDateTime,
than b: NaiveDateTime,
) -> Bool
Checks if the first naive datetime is earlier than the second naive datetime.
Examples
naive_datetime.literal("2024-06-21T23:17:00")
|> naive_datetime.is_earlier(
than: naive_datetime.literal("2024-06-21T23:17:00"),
)
// -> False
naive_datetime.literal("2013-06-12T23:17:00")
|> naive_datetime.is_earlier(
than: naive_datetime.literal("2024-06-12T23:17:00"),
)
// -> True
pub fn is_earlier_or_equal(
a: NaiveDateTime,
to b: NaiveDateTime,
) -> Bool
Checks if the first naive datetime is earlier or equal to the second naive datetime.
Examples
naive_datetime.literal("2024-08-12T23:17:00")
|> naive_datetime.is_earlier_or_equal(
to: naive_datetime.literal("2024-06-12T00:00:00"),
)
// -> False
naive_datetime.literal("2024-06-21T23:17:00")
|> naive_datetime.is_earlier_or_equal(
to: naive_datetime.literal("2024-06-21T23:17:00"),
)
// -> True
pub fn is_equal(a: NaiveDateTime, to b: NaiveDateTime) -> Bool
Checks if the first naive datetime is equal to the second naive datetime.
Examples
naive_datetime.literal("2024-06-21T23:17:00")
|> naive_datetime.is_equal(
to: naive_datetime.literal("2024-06-21T23:17:00"),
)
// -> True
naive_datetime.literal("2024-06-21T23:17:00")
|> naive_datetime.is_equal(
to: naive_datetime.literal("2024-06-21T23:17:01"),
)
// -> False
pub fn is_later(a: NaiveDateTime, than b: NaiveDateTime) -> Bool
Checks if the first naive datetime is later than the second naive datetime.
Examples
naive_datetime.literal("2024-06-21T23:17:00")
|> naive_datetime.is_later(
than: naive_datetime.literal("2024-06-21T23:17:00"),
)
// -> False
naive_datetime.literal("2024-06-21T23:17:00")
|> naive_datetime.is_later(
than: naive_datetime.literal("2022-04-12T00:00:00"),
)
// -> True
pub fn is_later_or_equal(
a: NaiveDateTime,
to b: NaiveDateTime,
) -> Bool
Checks if the first naive datetime is later or equal to the second naive datetime.
Examples
naive_datetime.literal("2024-06-21T23:17:00")
|> naive_datetime.is_later_or_equal(
to: naive_datetime.literal("2024-06-21T23:17:00"),
)
// -> True
naive_datetime.literal("2022-04-12T00:00:00")
|> naive_datetime.is_later_or_equal(
to: naive_datetime.literal("2024-06-21T23:17:00"),
)
// -> False
pub fn literal(naive_datetime: String) -> NaiveDateTime
Creates a new naive datetime value from a string literal, but will panic if the string is invalid.
Useful for declaring date literals that you know are valid within your
program.
Examples
naive_datetime.literal("2024-06-21T23:04:00.009")
|> naive_datetime.to_string
// -> "2024-06-21T23:04:00.009"
naive_datetime.literal("2024-06-21T23:04:00.009-04:00")
// panic
pub fn new(date: Date, time: Time) -> NaiveDateTime
Creates a new naive datetime from a date and time value.
Examples
naive_datetime.new(
date.literal("2024-06-21"),
time.literal("23:04:00.009"),
)
// -> naive_datetime.literal("2024-06-21T23:04:00.009")
pub fn set_offset(
datetime: NaiveDateTime,
offset: Offset,
) -> DateTime
Sets a naive datetime’s offset to the provided offset, leaving the date and time unchanged while returning a datetime value.
Examples
naive_datetime.literal("2024-06-21T23:17:00")
|> naive_datetime.set_offset(offset.literal("+10:00"))
// -> datetime.literal("2024-06-21T23:17:00+10:00")
pub fn set_utc(datetime: NaiveDateTime) -> DateTime
Sets a naive datetime’s offset to UTC, leaving the date and time unchanged
while returning a datetime value.
Alias for set_offset(naive_datetime, offset.utc)
.
Examples
naive_datetime.literal("2024-06-21T23:17:00")
|> naive_datetime.set_utc
// -> datetime.literal("2024-06-21T23:17:00Z")
pub fn subtract(
datetime: NaiveDateTime,
duration duration_to_subtract: Duration,
) -> NaiveDateTime
Subtracts a duration from a naive datetime.
Examples
naive_datetime.literal("2024-06-12T23:17:00")
|> naive_datetime.subtract(duration.days(3))
// -> naive_datetime.literal("2024-06-09T23:17:00")
pub fn time_left_in_day(naive_datetime: NaiveDateTime) -> Time
Gets the time left in the day.
Does not account for leap seconds like the rest of the package.
Examples
naive_datetime.literal("2015-06-30T23:59:03")
|> naive_datetime.time_left_in_day
// -> time.literal("00:00:57")
naive_datetime.literal("2024-06-18T08:05:20")
|> naive_datetime.time_left_in_day
// -> time.literal("15:54:40")
pub fn to_micro_precision(
naive_datetime: NaiveDateTime,
) -> NaiveDateTime
Sets a naive datetime’s time value to a microsecond precision. Drops any nanoseconds from the underlying time value.
Example
naive_datetime.literal("2024-06-13T13:42:11.195423534")
|> naive_datetime.to_micro_precision
|> naive_datetime.to_string
// -> "2024-06-13T13:42:11.195423"
pub fn to_milli_precision(
naive_datetime: NaiveDateTime,
) -> NaiveDateTime
Sets a naive datetime’s time value to a millisecond precision. Drops any microseconds from the underlying time value.
Example
naive_datetime.literal("2024-06-13T13:42:11.195423")
|> naive_datetime.to_milli_precision
|> naive_datetime.to_string
// -> "2024-06-13T13:42:11.195"
pub fn to_nano_precision(
naive_datetime: NaiveDateTime,
) -> NaiveDateTime
Sets a naive datetime’s time value to a nanosecond precision. Leaves the underlying time value unchanged.
Example
naive_datetime.literal("2024-06-13T13:42:11.195")
|> naive_datetime.to_nano_precision
|> naive_datetime.to_string
// -> "2024-06-13T13:42:11.195000000"
pub fn to_period(
start start: NaiveDateTime,
end end: NaiveDateTime,
) -> Period
Creates a period between two naive datetimes.
Examples
naive_datetime.to_period(
start: naive_datetime.literal("2024-06-12T23:17:00")
end: naive_datetime.literal("2024-06-16T01:16:12"),
)
|> period.as_days
// -> 3
naive_datetime.to_period(
start: naive_datetime.literal("2024-06-12T23:17:00"),
end: naive_datetime.literal("2024-06-16T01:18:12"),
)
|> period.format
// -> "3 days, 2 hours, and 1 minute"
pub fn to_second_precision(
naive_datetime: NaiveDateTime,
) -> NaiveDateTime
Sets a naive datetime’s time value to a second precision. Drops any milliseconds from the underlying time value.
Example
naive_datetime.literal("2024-06-13T13:42:11.195423")
|> naive_datetime.to_second_precision
|> naive_datetime.to_string
// -> "2024-06-13T13:42:11"