Git Bash date command ignoring TZ env

89 Views Asked by At

The date(1) command is supposed to take its timezone from the TZ environment variable.

Here's an example from the manpage:

68 mak:> date --date='TZ="America/Los_Angeles" 09:00 next Fri'
Fri Oct 20 09:00:00 GMT 2023

Taking a variation:

69 mak:> TZ='America/Los_Angeles' date
Mon Oct 16 16:03:03 GMT 2023

Except as you can see, in both cases, date(1) reported out using 'GMT'. In my case, I ran these commands at 11:03am in America/Chicago.

What I expected was times expressed via the TZ:

108 mak:> date --date='TZ="America/Los_Angeles" 09:00 next Fri'
Fri Oct 20 11:00:00 CDT 2023

And:

109 mak:>  TZ='America/Los_Angeles' date
Mon Oct 16 09:46:44 PDT 2023`
1

There are 1 best solutions below

0
Bob Makowski On

So even though the TZ was set, date(1) ran home to GMT, because on gitbash the default directory for the zoneinfo data was empty.

to solve this, I grabbed the data from a zoneinfo repo on github and copied the zoneinfo directory to /use/share/zoneinfo

112 mak@:> ll -d /usr/share/zoneinfo/*
drwxr-xr-x 1 mak 197121     0 Oct 16 11:30 /usr/share/zoneinfo/Africa/
drwxr-xr-x 1 mak 197121     0 Oct 16 11:30 /usr/share/zoneinfo/America/
drwxr-xr-x 1 mak 197121     0 Oct 16 11:30 /usr/share/zoneinfo/Antarctica/
drwxr-xr-x 1 mak 197121     0 Oct 16 11:30 /usr/share/zoneinfo/Arctic/
drwxr-xr-x 1 mak 197121     0 Oct 16 11:30 /usr/share/zoneinfo/Asia/
drwxr-xr-x 1 mak 197121     0 Oct 16 11:30 /usr/share/zoneinfo/Atlantic/
drwxr-xr-x 1 mak 197121     0 Oct 16 11:30 /usr/share/zoneinfo/Australia/
drwxr-xr-x 1 mak 197121     0 Oct 16 11:30 /usr/share/zoneinfo/Brazil/
...

to double check, I verified that the magic number for these (binary) files was as I expected:

114 mak:> file  /usr/share/zoneinfo/America/Chicago
/usr/share/zoneinfo/America/Chicago: timezone data (fat), version 2, 7 gmt time flags, 7 std time flags, no leap seconds, 236 transition times, 7 local time types, 24 abbreviation chars

and date(1) immediately found the data and employed it to get the correct results.

110 mak:> echo $TZ
America/Chicago
111 mak:> date
Mon Oct 16 12:08:11 CDT 2023

another double check on the --date switch behavior:

119 mak:> TZ=America/New_York date --date='TZ="America/Los_Angeles" 09:00 next Fri'
Fri Oct 20 12:00:00 EDT 2023