I am missing two things in Dates/TimeZones
- convert a unix timestamp into the computer’s local time
- determine the system’s timezone
Maybe I missed something?
For 1) I have found a solution, which could go into Dates a similar way, I think:
@inline locale_offset() = round(now() - unix2datetime(time()), Minute)
unix2locale(x) = unix2datetime(x) + locale_offset()
For 2) I have found a workaround for unix-like systems
locale_timezone() = TimeZone(join(split(readlink("/etc/localtime"), '/')[end-1:end], '/'))
Would be happy to hear what other users think.
julia> Dates.Minute(now()-now(UTC))
60 minutes
# or
julia> Dates.Hour(now()-now(UTC))
1 hour
would be shorter (for the ‘locale_offset’ part)
But this requires TimeZones to be loaded…
And you still risk to end up with one millisecond on top of the hour if you do not perform rounding …
I’d even vote for adding
@include locale_timezonestring() = join(split(readlink("/etc/localtime"), '/')[end-1:end], '/')
to Dates and
locale_timezone() = TimeZone(locale_timezonestring())
to TimeZones
‘now()-now(UTC)’ works here, no 'TimeZones.jl` package. For reliable results the host system must be properly configured regarding timezone, NTP, etc.
Thanks for the clarification. I wasn’t aware of UTC being part of Dates.
Still, rounding to minutes is a good idea
BTW, just looked up the definition of now(UTC)
now(::Type{UTC}) = unix2datetime(time())
Right, rounding is needed to prevent occasional ‘InexactError’ when converting to Minute/Hour.
1 Like