Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: get date and time from another time zone


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
like image 866
alex_pc89 Avatar asked Nov 07 '14 13:11

alex_pc89


4 Answers

You could use

TZ=America/New_York date

or if you want to do date arithmetic you could use

date -d "+5 hours"
like image 57
SMA Avatar answered Sep 22 '22 17:09

SMA


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
like image 41
anubhava Avatar answered Sep 19 '22 17:09

anubhava


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
like image 39
mivk Avatar answered Sep 20 '22 17:09

mivk


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

like image 33
rubo77 Avatar answered Sep 20 '22 17:09

rubo77