1

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 Answers1

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

JdeBP
  • 68,745