Bash: get date and time from another time zone
Asked Answered
D

5

25


I would want to get the date and time from another time zone (UTC-4) with a bash command, but I don't want to configure it as the default TZ for the system.
Is there a simple way to do it?
E.g 1 (current):

$ date
fri nov  7 13:15:35 UTC 2014

E.g 2 (what I need):

$ date (+ some option for UTC-4)
fri nov  7 09:15:35 UTC 2014
Darton answered 7/11, 2014 at 13:19 Comment(0)
S
42

You could use

TZ=America/New_York date

or if you want to do date arithmetic you could use

date -d "+5 hours"
Skirr answered 7/11, 2014 at 13:26 Comment(6)
Thank you @almas shaikh, the first option is the most accurate response for my case.Darton
But it requires you to know the proper name of the time zone you want, which can be tricky to find out sometimes. You can also use a numeric offset, though it's by no means obvious how. The timezone(3) manual page has the gory details.Neap
Also maybe check Wikipedia to find e.g. that TZ=Asia/Kolkata is one way to get IST (which is tricky because TZ=UTC-5:30 isn't entirely obvious; e.g. UTC-5.5 doesn't work).Neap
Any way this could be achieved for macOS? -d argument is for daylight saving offset in the date tool and there is no argument for date string.Misdemean
Doesn't work on Mac OS. This TZ=UTC date -R does work though.Ozonize
For time zones with hours and minutes + or - UTC, eg for India: date -d "+5 hours 30 minutes"Alasteir
D
15

You can use offset value in TZ to get the date for a different timezone:

TZ=UTC date -R
Fri, 07 Nov 2014 13:55:07 +0000

TZ=UTC+4 date -R
Fri, 07 Nov 2014 09:54:52 -0400
Dismal answered 7/11, 2014 at 13:24 Comment(5)
Yes that is how TZ works with offset. I would suggest using offset since it doesn't require you to remember long & valid timezone names.Dismal
You don't need a subshell. TZ=UTC date sets TZ, runs date, and reverts the value of TZ to its previous value (unset, or different value, or the same value as the one you specified, as the case may be).Neap
Also, TZ=UTC-4 date confusingly prints the time zone as "UTC" even though it's not. (This is visible in your example output, too.)Neap
Also better to use date -R for --rfc-2822 format which displays correct offset as well.Dismal
The TZ=UTC date -R is the only solution I could get to work that works on Mac OS for a redis set key insert I am doing and wanted UTC: redis-cli set starttime "$(TZ=UTC date -R)"Ozonize
A
9

I use a little script for that, so I don't have to know or remember the exact name of the timezone I'm looking for. The script takes a search string as argument, and gives the date and time for any timezone matching the search. I named it wdate.

#!/bin/bash

# Show date and time in other time zones

search=$1

format='%a %F %T %z'
zoneinfo=/usr/share/zoneinfo/posix/

if command -v timedatectl >/dev/null; then
    tzlist=$(timedatectl list-timezones)
else
    tzlist=$(find -L $zoneinfo -type f -printf "%P\n")
fi

grep -i "$search" <<< "$tzlist" \
| while read z
  do
      d=$(TZ=$z date +"$format")
      printf "%-32s %s\n" "$z" "$d"
  done

Example output:

$ wdate fax
America/Halifax                  Fri 2022-03-25 09:59:02 -0300

or

$ wdate canad
Canada/Atlantic                  Fri 2022-03-25 10:00:04 -0300
Canada/Central                   Fri 2022-03-25 08:00:04 -0500
Canada/Eastern                   Fri 2022-03-25 09:00:04 -0400
Canada/Mountain                  Fri 2022-03-25 07:00:04 -0600
Canada/Newfoundland              Fri 2022-03-25 10:30:04 -0230
Canada/Pacific                   Fri 2022-03-25 06:00:04 -0700
Canada/Saskatchewan              Fri 2022-03-25 07:00:04 -0600
Canada/Yukon                     Fri 2022-03-25 06:00:04 -0700
Andrews answered 15/1, 2020 at 12:4 Comment(1)
I'm getting this error: find: ‘/usr/share/zoneinfo/posix/’: No such file or directory. Maybe because I'm in a Windows 11 machine?Ashaashamed
F
4

If the other solutioons don't work, use

date --date='TZ="UTC+4" 2016-08-22 10:37:44' "+%Y-%m-%d %H:%M:%S"

2016-08-22 16:37:44

Fuel answered 22/8, 2016 at 10:19 Comment(0)
C
1

You may also add minutes using the following for Time Zones with half an hour difference:

date -d "-210 mins"
Celebrated answered 28/12, 2023 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.