0

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?

1

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

1
  • Thank you @JdeBP , sorry for troubling again but I want the offset in comparison with local time. :( Aug 1 '20 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.