How to get the hour difference between two time zones in bash

Bob ubuntu-qygzanxc at listemail.net
Mon Mar 28 20:41:01 UTC 2022


** Reply to message from Bo Berglund <bo.berglund at gmail.com> on Mon, 28 Mar
2022 16:34:37 +0200

> I have a scriot that runs every day and it operates on data from both my
> location and New York. It sets up at jobs at certain times in New York but doing
> it from Stockholm.
> 
> I would like to automatically handle the time difference between the two sites
> in the script.
> The normal difference is 6 hours but for a couple of weeks in March it is 5
> hours and in the fall it is 7 hours because the US and Sweden switches to/from
> DST at different dates in March and October.
> 
> I just ran into the problem and could almost fix it by setting the TZ variable
> in the script, but it only worked during the diff time and then it screwed up
> again.
> 
> So I need to calculate on the start of the script which is the current number of
> hours difference between Stockholm and New York.
> 
> How can this be done?

I have done that in the past.  You need to know the TZ parameters for each time
zone.  That will give you the start and end of daylight time and the hours
difference from UTC.  Compute UTC from local time for each zone and subtract
the difference and if needed convert back to local time from UTC.

-- 
Robert Blair


We need leaders not in love with money but in love with justice.  Not in love with publicty but in love with humanity  -- Martin Luther King




More information about the ubuntu-users mailing list