How to consider the timezone in unix2datetime?

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

using Dates

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

test()

use TimeZones.jl’s ZonedDateTime

1 Like

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

look at these examples – 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
1 Like

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?

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

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

1 Like

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.

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.

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) 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, and with Julia see the answers above.

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