Home > database >  bash command to get current time in given time zone accounting for daylight savings
bash command to get current time in given time zone accounting for daylight savings

Time:07-09

Is there a bash command that will tell me the current time in a given time zone while accounting for daylight savings? For example, I'm thinking of something like this:

$ getDateTime --region Seattle
2021-01-01-13-30-00

Importantly, I would like it to give me the standard time during the winter and daylight time during the summer, and switch over on the correct days of the year in accordance with the region. Does something like this exist in bash? If not, what about another language?

CodePudding user response:

Set the TZ environment variable to the desired time zone and use the date command.

TZ=US/Pacific date

There are some city names in the timezone database, such as America/New_York and America/Los_Angeles, but it's not very complete and doesn't include Seattle. See https://data.iana.org/time-zones/tzdb-2019c/zone.tab for the master list.

CodePudding user response:

Only with bash >= version 4.2:

TZ=US/Pacific printf -v unixtime "%(%Y-%m-%d-%H-%M-%S)T\n" -1
echo "$unixtime"

Output (e.g.)

2022-07-08-15-18-51

CodePudding user response:

Building on Barmar's answer, here's a bash function you can use:

getDateTime() {
    TZ="$1" date ' %Y-%m-%d-%H-%M-%S %Z %z'
}

Sample usage:

getDateTime America/Los_Angeles
getDateTime America/New_York
getDateTime Pacific/Honolulu
getDateTime Asia/Hong_Kong

2022-07-08-18-06-50 PDT -0700
2022-07-08-21-06-50 EDT -0400
2022-07-08-15-06-50 HST -1000
2022-07-09-09-06-50 HKT  0800

Daylight savings times are handled automatically by the system, since the timezone names are timezone-aware.

Your system most likely uses the most up-to-date timezone information from IANA:

https://data.iana.org/time-zones/tzdb/zone.tab

https://ftp.iana.org/tz/tzdb/zone.tab

You can find your system's file of timezone names in:

/usr/share/zoneinfo/zoneinfo.tab

It seems like a really nice idea to map individual country/state/city names or latitude/longitude pairs to IANA timezone TZ names, there are probably third-party software out there that can do this.

CodePudding user response:

To demonstrate how the other answers work with DST:

"spring forward"

date:

$ TZ=America/Los_Angeles date -d '2022-03-13 01:59:59'
Sun Mar 13 01:59:59 PST 2022

$ TZ=America/Los_Angeles date -d '2022-03-13   1 second 01:59:59'
Sun Mar 13 03:00:00 PDT 2022

bash builtin printf

$ epoch=$(TZ=America/Los_Angeles date -d '2022-03-13 01:59:59'  %s)

$ TZ=America/Los_Angeles printf '%(%F %T %Z)T\n' $epoch
2022-03-13 01:59:59 PST

$ TZ=America/Los_Angeles printf '%(%F %T %Z)T\n' $((epoch   1))
2022-03-13 03:00:00 PDT

"fall behind"

$ TZ=America/Los_Angeles date -d '2022-11-06 01:59:59'
Sun Nov  6 01:59:59 PDT 2022

$ TZ=America/Los_Angeles date -d '2022-11-06   1 second 01:59:59'
Sun Nov  6 01:00:00 PST 2022
$ epoch=$(TZ=America/Los_Angeles date -d '2022-11-06 01:59:59'  %s)

$ TZ=America/Los_Angeles printf '%(%F %T %Z)T\n' $epoch
2022-11-06 01:59:59 PDT

$ TZ=America/Los_Angeles printf '%(%F %T %Z)T\n' $((epoch   1))
2022-11-06 01:00:00 PST
  • Related