Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert a time value from one timezone to another in a portable way

14 views
Skip to first unread message

Ian Zimmerman

unread,
Oct 31, 2017, 1:49:16 AM10/31/17
to
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.

--
Please don't Cc: me privately on mailing lists and Usenet,
if you also post the followup to the list or newsgroup.
To reply privately _only_ on Usenet, fetch the TXT record for the domain.
You can automate that, Mr. Pointed Ears! :-)

Bit Twister

unread,
Oct 31, 2017, 5:52:26 AM10/31/17
to
On Mon, 30 Oct 2017 22:49:05 -0700, 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,

does FreeBSD's date support the long switch option. --date?

Janis Papanagnou

unread,
Oct 31, 2017, 10:00:28 AM10/31/17
to
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

Ian Zimmerman

unread,
Oct 31, 2017, 11:34:35 AM10/31/17
to
On 2017-10-31 09:52, Bit Twister wrote:

> does FreeBSD's date support the long switch option. --date?

Of course not; they're opposed to that form of options in general.

Thomas 'PointedEars' Lahn

unread,
Nov 1, 2017, 12:31:16 PM11/1/17
to
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).

ACK, see also <https://www.freebsd.org/cgi/man.cgi?date>.

The “-d” and “-j” options/switches (not: flags) are not specified in
POSIX1-2008, which is indicative of non-portability. Unless you are
comfortable with temporarily *setting* the system time, there is
apparently no possibility that is POSIX-compliant:

<http://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html>

> If there's no other way, I'll switch on the output of uname -s, but I'd
> really like to avoid that.

As always, perform a *feature* test, or test for *the command’s* version,
instead.

--
PointedEars

Twitter: @PointedEars2
Please do not cc me. /Bitte keine Kopien per E-Mail.
0 new messages