# How to consider the timezone in unix2datetime?

**URL:** <https://discourse.julialang.org/t/how-to-consider-the-timezone-in-unix2datetime/90179>\
**Category:** General Usage\
**Tags:** question, dates\
**Created:** [November 13, 2022, 1:09am UTC](https://discourse.julialang.org/t/how-to-consider-the-timezone-in-unix2datetime/90179 "2022-11-13T01:09:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [November 13, 2022, 1:09am UTC](https://discourse.julialang.org/t/how-to-consider-the-timezone-in-unix2datetime/90179/1 "2022-11-13T01:09:40Z")

</div>

Hello everyone. In the code below  
my time zone is not being considered in dt. Please, how to make it be considered?

```julia
using Dates

function test()
	f = "local/file"
	u = mtime(f)
	unix2datetime(u) # three hours too long
end

test()
```

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [November 13, 2022, 1:10am UTC](https://discourse.julialang.org/t/how-to-consider-the-timezone-in-unix2datetime/90179/2 "2022-11-13T01:10:54Z")

</div>

use [TimeZones.jl](https://github.com/JuliaTime/TimeZones.jl)’s `ZonedDateTime`

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [November 13, 2022, 1:24am UTC](https://discourse.julialang.org/t/how-to-consider-the-timezone-in-unix2datetime/90179/3 "2022-11-13T01:24:26Z")

</div>

How do I print the dt? I tried this  
`println(ZonedDateTime(dt, localzone()))` but it didn’t work.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [November 13, 2022, 1:33am UTC](https://discourse.julialang.org/t/how-to-consider-the-timezone-in-unix2datetime/90179/4 "2022-11-13T01:33:15Z")

</div>

look at these [examples](https://juliatime.github.io/TimeZones.jl/stable/types/) – you do not need println, just assign the result to a var.

- in a function you can return the result
- in the REPL you will see the result

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [November 13, 2022, 4:16pm UTC](https://discourse.julialang.org/t/how-to-consider-the-timezone-in-unix2datetime/90179/5 "2022-11-13T16:16:51Z")

</div>

Following the suggestions, I had to use two packages and write a small function. Wouldn’t there be something more direct? What if the time in the file is not UTC+0?

```julia
using Dates, TimeZones

"modified file time to local time"
function mtime2dt(f::String)
	isfile(f) || error("`$f` is not a file")
	dt = unix2datetime(mtime(f))
	zdt = ZonedDateTime(dt, tz"UTC")
	zdt = astimezone(zdt, localzone())
	DateTime(zdt)
end

```

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [November 14, 2022, 10:33am UTC](https://discourse.julialang.org/t/how-to-consider-the-timezone-in-unix2datetime/90179/6 "2022-11-14T10:33:54Z")

</div>

> [@marllos](#):
>
> What if the time in the file is not UTC+0

In that case, replace tz”UTC” with whatever is appropriate, such as tz”Europe/Berlin”

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [November 14, 2022, 1:23pm UTC](https://discourse.julialang.org/t/how-to-consider-the-timezone-in-unix2datetime/90179/7 "2022-11-14T13:23:02Z")

</div>

> [@bernhard](#):
>
> In that case, replace tz”UTC”

Ok, but what I’d like to know is if Linux always uses UTC+0 when saving a file, regardless of my time zone. In my Linux, it seems to be the case, because unix2datetime(mtime(“filename”)) is 3 hours ahead, that is, at the same Greenwinch time.

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [November 14, 2022, 1:24pm UTC](https://discourse.julialang.org/t/how-to-consider-the-timezone-in-unix2datetime/90179/8 "2022-11-14T13:24:17Z")

</div>

> [@marllos](#):
>
> Ok, but what I’d like to know is if Linux always uses UTC+0 when saving a file, regardless of my time zone.

I do not know at all.  
While some people on this forum here might know the answer to your question, the question may be better suited for a different forum.

---

<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 14, 2022, 2:05pm UTC](https://discourse.julialang.org/t/how-to-consider-the-timezone-in-unix2datetime/90179/10 "2022-11-14T14:05:51Z")

</div>

Yes, UTC, also on (not too old) Windows. Unless you fiddle with the system, then it is of course possible to get the system clock use a certain time zone. Considering that devices nowadays are networked and relatively often move from one timezone to another, it is normally not a good idea to fiddle with the settings for the system clock.

Normally the NTP ([Network Time Protocol](https://en.wikipedia.org/wiki/Network_Time_Protocol)) runs in the background and makes sure that the billions of systems in the world have the same system time within a few tens of milliseconds, a quiet and impressive achievement in the last 1-2 decades. This makes timestamps of files comparable also between networked systems across the world.

Software arranges that the UTC timestamps (of files etc) are displayed with the desired timezone conventions. On linux see for example [here](https://www.tecmint.com/set-system-locales-in-linux/), and with Julia see the answers above.

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [November 14, 2022, 2:49pm UTC](https://discourse.julialang.org/t/how-to-consider-the-timezone-in-unix2datetime/90179/11 "2022-11-14T14:49:17Z")

</div>

Very interesting and very good to know, thank you. Thank you all.
