124 lines
3.3 KiB
Clojure
124 lines
3.3 KiB
Clojure
(ns calendar
|
|
"Erlang :calendar module — date and time calculations.
|
|
|
|
In CljElixir: (calendar/local-time), (calendar/universal-time), etc.
|
|
Works with Erlang date/time tuples: {Year Month Day} and {Hour Min Sec}.")
|
|
|
|
(defn local-time
|
|
"Returns the current local datetime as {{Y M D} {H M S}}.
|
|
(calendar/local-time) ;=> {{2024 3 9} {12 30 0}}"
|
|
[])
|
|
|
|
(defn universal-time
|
|
"Returns the current UTC datetime as {{Y M D} {H M S}}.
|
|
(calendar/universal-time) ;=> {{2024 3 9} {12 30 0}}"
|
|
[])
|
|
|
|
(defn local-time-to-universal-time-dst
|
|
"Converts local time to UTC, handling DST. Returns list of possible results."
|
|
[datetime])
|
|
|
|
(defn universal-time-to-local-time
|
|
"Converts UTC to local time."
|
|
[datetime])
|
|
|
|
(defn now-to-datetime
|
|
"Converts erlang:now/0 tuple to datetime tuple."
|
|
[now])
|
|
|
|
(defn now-to-local-time
|
|
"Converts erlang:now/0 to local datetime."
|
|
[now])
|
|
|
|
(defn now-to-universal-time
|
|
"Converts erlang:now/0 to UTC datetime."
|
|
[now])
|
|
|
|
(defn datetime-to-gregorian-seconds
|
|
"Converts {{Y M D} {H M S}} to Gregorian seconds (since year 0).
|
|
(calendar/datetime-to-gregorian-seconds {{2024 1 1} {0 0 0}}) ;=> 63871..."
|
|
[datetime])
|
|
|
|
(defn gregorian-seconds-to-datetime
|
|
"Converts Gregorian seconds back to {{Y M D} {H M S}}.
|
|
(calendar/gregorian-seconds-to-datetime 63871...)"
|
|
[seconds])
|
|
|
|
(defn date-to-gregorian-days
|
|
"Converts {Year Month Day} to Gregorian day count.
|
|
(calendar/date-to-gregorian-days {2024 1 1})"
|
|
([date])
|
|
([year month day]))
|
|
|
|
(defn gregorian-days-to-date
|
|
"Converts Gregorian day count to {Year Month Day}."
|
|
[days])
|
|
|
|
(defn day-of-the-week
|
|
"Returns day of week (1=Monday, 7=Sunday).
|
|
(calendar/day-of-the-week {2024 3 9}) ;=> 6"
|
|
([date])
|
|
([year month day]))
|
|
|
|
(defn is-leap-year
|
|
"Returns true if year is a leap year.
|
|
(calendar/is-leap-year 2024) ;=> true"
|
|
[year])
|
|
|
|
(defn last-day-of-the-month
|
|
"Returns the last day of the month.
|
|
(calendar/last-day-of-the-month 2024 2) ;=> 29"
|
|
[year month])
|
|
|
|
(defn valid-date
|
|
"Returns true if the date is valid.
|
|
(calendar/valid-date {2024 2 29}) ;=> true"
|
|
([date])
|
|
([year month day]))
|
|
|
|
(defn iso-week-number
|
|
"Returns {year week} for a date.
|
|
(calendar/iso-week-number {2024 3 9}) ;=> {2024 10}"
|
|
([date])
|
|
([year month day]))
|
|
|
|
(defn time-difference
|
|
"Returns the time difference between two datetimes.
|
|
(calendar/time-difference dt1 dt2) ;=> {days {hours mins secs}}"
|
|
[datetime1 datetime2])
|
|
|
|
(defn seconds-to-daystime
|
|
"Converts seconds to {days {hours minutes seconds}}.
|
|
(calendar/seconds-to-daystime 90061) ;=> {1 {1 1 1}}"
|
|
[seconds])
|
|
|
|
(defn seconds-to-time
|
|
"Converts seconds to {hours minutes seconds}.
|
|
(calendar/seconds-to-time 3661) ;=> {1 1 1}"
|
|
[seconds])
|
|
|
|
(defn time-to-seconds
|
|
"Converts {hours minutes seconds} to seconds.
|
|
(calendar/time-to-seconds {1 1 1}) ;=> 3661"
|
|
[time])
|
|
|
|
(defn system-time-to-local-time
|
|
"Converts system time to local datetime."
|
|
[time unit])
|
|
|
|
(defn system-time-to-universal-time
|
|
"Converts system time to UTC datetime."
|
|
[time unit])
|
|
|
|
(defn rfc3339-to-system-time
|
|
"Parses RFC 3339 timestamp to system time.
|
|
(calendar/rfc3339-to-system-time \"2024-03-09T12:00:00Z\") ;=> integer"
|
|
([string])
|
|
([string opts]))
|
|
|
|
(defn system-time-to-rfc3339
|
|
"Converts system time to RFC 3339 string.
|
|
(calendar/system-time-to-rfc3339 time) ;=> \"2024-03-09T12:00:00Z\""
|
|
([time])
|
|
([time opts]))
|