Home > Net >  Converting date in a different timezone with `date`
Converting date in a different timezone with `date`

Time:12-13

I am trying to convert dates from different timezones with UNIX date (I am on Ubuntu 20.04).

With current date, it works well:

$ date
dim. 12 déc. 2021 11:59:16 CET
$ TZ=Pacific/Tahiti date
dim. 12 déc. 2021 00:59:32 -10

But when I am working with a string, it fails:

$ export testdate="2021/10/28 17:47:26"
$ date -d "$test"
jeu. 28 oct. 2021 17:47:26 CEST
$ TZ=Pacific/Tahiti date -d "$test"
jeu. 28 oct. 2021 17:47:26 -10

as I am expecting:

jeu 28 oct. 2021 05:47:26 -10

I don't understand why I don't get the proper shift. And of course if I try with a date and time where the day should also change, it doesn't work either:

$ export test="2021/10/28 7:47:26"
$ date -d "$test"
jeu. 28 oct. 2021 07:47:26 CEST
$ TZ=Pacific/Tahiti date -d "$test" " %F %T %Z"
2021-10-28 07:47:26 -10

while I am expecting:

mer 27 oct. 2021 19:47:26 -10

CodePudding user response:

why I don't get the proper shift

test="2021/10/28 17:47:26"

Is a date in unknown timezone. No one knows what timezone it is in, what the daylight is. GNU date tries to "guess" what offset you meant, it generally traverses TZ database for current timezone and just picks the first offset that matches. Also, specifying timezone is not enough to know what daylight it is, you have to be specific.

Also, because of the daylight time you can "go back" in time, it's now known what the offset to UTC is even when you know the timezone.

Also, you don't have to export it - date does not care about test environment variable.

Converting date in a different timezone with date

If the input is in UTC, tell date that.

$ LC_ALL=C TZ=Pacific/Tahiti date -d "2021/10/28 17:47:26 UTC"
Thu Oct 28 07:47:26 -10 2021

If the input is with any other offset, tell date that.

$ LC_ALL=C TZ=Pacific/Tahiti date -d "2021/10/28 17:47:26 CEST"
Thu Oct 28 05:47:26 -10 2021

Te parsing of GNU date of input format is generally a mystery. The documentation lists 2004-02-29 16:21:42 format as an example input, so I recommend that format. If you want to be exact, I recommend strptime from dateutils (or a real programming language).

CodePudding user response:

I'm not fully confident, but i get the impression you're looking the following syntax:

LC_TIME="es_ES.UTF8" TZ="America/New_York" date --date='TZ="Europe/Amsterdam" 2021/10/28 17:47:26' " %A %F %T %B"

That takes a predefined datetime (interpreted as being local to Amsterdam), adjusts the datetime (based on the time difference) to New York-time at that same moment; Then it prints that result with Spanish names for the months/weekdays (provided that language' locale is present on your system).

CodePudding user response:

One simple way is to convert time first to epoch time :

test="2021/10/28 17:47:26"
TZ=Pacific/Tahiti date -d @$(date -d "$test"  %s)

date -d "$test" %s converts local time to epoch time.

TZ=Pacific/Tahiti date -d @$(date -d "$test" %s) prints Tahiti time from epoch time.

  • Related