Subject: Convert a time value from one timezone to another in a portable way
On 31.10.2017 06:49, Ian Zimmerman wrote:
> I'll be satisfied if it is portable between GNU/Linux and FreeBSD.
>
> Both systems' date(1) command provides a way to format and print an
> arbitrary time, not just the current time. But unfortunately each of
> them requires very different flags for date(1) to do that.
>
> Here's what I do now on GNU/Linux (simplified):
>
> $ date -d 'TZ="Europe/Prague" 2017-10-31 15:45' +'%H%M %m/%d/%Y'
> 0745 10/31/2017
>
> But on FreeBSD, the -d option does something completely different, and
> instead one is apparently supposed to just pass a bare time value (as if
> setting a new system time), and include the -j flag (which OTOH is
> absent in the GNU version).
>
> If there's no other way, I'll switch on the output of uname -s, but I'd
> really like to avoid that.
You could use another tool to do that; one possibility is GNU awk with
its time functions. But that will probably become yet more bulky than
the uname-switching you have in mind (presuming you don't want to hide
the functionality in a function or so). Something like
TZ='Europe/Prague' awk -v t="$(TZ='Europe/London' date +'%Y %m %d %H %M
%S')" 'BEGIN { print strftime("%H%M %Y-%m-%d", mktime(t)) }'
Janis