How do you get the location name of the local timezone?

For some reason I can only get Local as the output rather than something like Europe/Berlin within Crystal.

Time::Location.local.name # => "Local"
Time.local.location.name # => "Local"
Time.local.to_s("%Z") # => "Local"

Time::Location gets the information from the environment. See location.cr#L353-370 for detail.

I wonder if this could be improved, as most linux distros for example don’t expose the location as an ENVVAR :thinking: TZ is generally unset.

If TZ is unset, the location is loaded from /etc/localtime and has the name Local.

/etc/localtime is usually a link into the time zone database and by following that link you might be able to find the respective name:

target = Path[File.readlink("/etc/localtime")]
Path[target.parts[-2..]] # => Path["Europe/Berlin"]

Perhaps Time::Location.load_local could defer the name like that if /etc/localtime is a link into the TZDB.

2 Likes