I am taking input for TZ and I want to convert it into a (+/-)HHMM format. but not compared with UTC, I want it to be compared with the local time. How can I do it?
Asked
Active
Viewed 1,786 times
1 Answers
2
This isn't done using bash
.
This is a fairly trivial exercise in the use of the date
command and the (widely available but ironically not standardized for date
even though it is for strftime()
) %z
format specifier.
TZ="Asia/Tokyo" date +"%z"
This obtains the current UTC offset, accounting for daylight savings time switches, of course.
Various implementations of date
allow one to display arbitrary timestamps instead of the current time, without attempting to set the system clock, and so obtain the UTC offset for other times of the year.
For example: FreeBSD date
has a -j
option and takes input timestamps (by default, if -f
is not used) in a strictly descending form.
% TZ="Europe/Lisbon" date -j +"%z" "202401060000" +0000 % TZ="Europe/Lisbon" date -j +"%z" "202407060000" +0100 %
There are also tools such as zdump
, but the date
command is the obvious one here.
Further reading
date
. 2018-08-04. BSD General Commands Manual. FreeBSD.- How can I have `date` output the time from a different timezone?
- Why does TZ=UTC-8 produce dates that are UTC+8?
- Need to get the Timezone details
- "Utilities: date". Shell Command Language. Single UNIX Specification. Issue 7. IEEE 1003.1. 2016. The Open Group.

JdeBP
- 68,745
-
Thank you @JdeBP , sorry for troubling again but I want the offset in comparison with local time. :( – theSwapnilSaste Aug 01 '20 at 12:29