# Retrieve current timezone

**URL:** <https://discourse.julialang.org/t/retrieve-current-timezone/122763>\
**Category:** General Usage\
**Tags:** dates, time, datetime\
**Created:** [November 18, 2024, 10:51am UTC](https://discourse.julialang.org/t/retrieve-current-timezone/122763 "2024-11-18T10:51:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [November 18, 2024, 10:51am UTC](https://discourse.julialang.org/t/retrieve-current-timezone/122763/1 "2024-11-18T10:51:10Z")

</div>

I am missing two things in Dates/TimeZones

1. convert a unix timestamp into the computer’s local time
2. 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:

```julia
@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

```julia
locale_timezone() = TimeZone(join(split(readlink("/etc/localtime"), '/')[end-1:end], '/'))

```

Would be happy to hear what other users think.

---

<div class="post-metadata">

**Author:** ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)\
**Post date:** [November 18, 2024, 11:15am UTC](https://discourse.julialang.org/t/retrieve-current-timezone/122763/2 "2024-11-18T11:15:36Z")

</div>

```julia
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)

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [November 18, 2024, 11:24am UTC](https://discourse.julialang.org/t/retrieve-current-timezone/122763/3 "2024-11-18T11:24:06Z")

</div>

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 …

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [November 18, 2024, 11:29am UTC](https://discourse.julialang.org/t/retrieve-current-timezone/122763/4 "2024-11-18T11:29:25Z")

</div>

I’d even vote for adding

```julia
@include locale_timezonestring() = join(split(readlink("/etc/localtime"), '/')[end-1:end], '/')

```

to Dates and

```julia
locale_timezone() = TimeZone(locale_timezonestring())

```

to TimeZones

---

<div class="post-metadata">

**Author:** ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)\
**Post date:** [November 18, 2024, 11:29am UTC](https://discourse.julialang.org/t/retrieve-current-timezone/122763/5 "2024-11-18T11:29:42Z")

</div>

‘now()-now(UTC)’ works here, no 'TimeZones.jl` package. For reliable results the host system must be properly configured regarding timezone, NTP, etc.

---

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [November 18, 2024, 11:36am UTC](https://discourse.julialang.org/t/retrieve-current-timezone/122763/6 "2024-11-18T11:36:52Z")

</div>

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)`

```julia
now(::Type{UTC}) = unix2datetime(time())

```

---

<div class="post-metadata">

**Author:** ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)\
**Post date:** [November 18, 2024, 11:41am UTC](https://discourse.julialang.org/t/retrieve-current-timezone/122763/7 "2024-11-18T11:41:36Z")

</div>

Right, rounding is needed to prevent occasional ‘InexactError’ when converting to Minute/Hour.
