Current Time
now
/ System Time Zone
Julia provides the now()
method to retrieve your current system's time as a DateTime
. The TimeZones.jl package provides an additional now(::TimeZone)
method providing the current time as a ZonedDateTime
:
now(tz"Europe/Warsaw")
To get the TimeZone
currently specified on you system you can use localzone()
. Combining this method with the new now
method produces the current system time in the current system's time zone:
now(localzone())
today
Similar to now
the TimeZones package also provides a today(::TimeZone)
method which allows you to determine the current date as a Date
in the specified TimeZone
.
julia> a, b = now(tz"Pacific/Midway"), now(tz"Pacific/Apia")
(ZonedDateTime(2024, 10, 30, 10, 15, 9, 408, tz"Pacific/Midway"), ZonedDateTime(2024, 10, 31, 11, 15, 9, 408, tz"Pacific/Apia"))
julia> a - b
0 milliseconds
julia> today(tz"Pacific/Midway"), today(tz"Pacific/Apia")
(Date("2024-10-30"), Date("2024-10-31"))
You should be careful not to use today()
when working with ZonedDateTime
s as you may end up using the wrong day. For example:
julia> midway, apia = tz"Pacific/Midway", tz"Pacific/Apia"
(tz"Pacific/Midway", tz"Pacific/Apia")
julia> ZonedDateTime(today() + Time(11), midway)
2024-10-30T11:00:00-11:00
julia> ZonedDateTime(today() + Time(11), apia) # Wrong date; with the current rules apia should be one day ahead of midway
2024-10-30T11:00:00+14:00
julia> ZonedDateTime(today(midway) + Time(11), midway)
2024-10-30T11:00:00-11:00
julia> ZonedDateTime(today(apia) + Time(11), apia)
2024-10-31T11:00:00+14:00
Alternatively, you can use the todayat
function which takes care of this for you:
julia> todayat(Time(11), tz"Pacific/Midway")
2024-10-30T11:00:00-11:00
julia> todayat(Time(11), tz"Pacific/Apia")
2024-10-31T11:00:00+14:00