i336_
i336_

Reputation: 2011

Converting time across timezones, while preserving DST + other adjustments, using bash/sh

Given any two or more timezones, I would like to convert the local time in any timezone to any other such that the time representation accurately represents any local alterations (DST, etc) that each timezone has in effect.

Specifically, if some of the timezones I provide happen to deal with DST considerations, I need to detect and catch this such that the appropriate recalculations will automatically be made - for example, that the output for those timezones is automatically adjusted forward or backward by one hour.

I want to do all of this from the shell, if possible, preferably using portable techniques; I was considering handing off the heavy lifting to date, but I'm not sure how to have it perform relative timezone-accurate calculation, especially in a way that's crossplatform to at least Linux and BSD.

Upvotes: 0

Views: 46

Answers (1)

Diego Torres Milano
Diego Torres Milano

Reputation: 69378

Convert to UTC first and then convert to the target time zone (using CET as an example)

TZ=CET date --date=@$(TZ=UTC date +%s)

DST will be taken automatically.

Upvotes: 2

Related Questions