2

On AIX

date -u gives date and time in GMT format
date gives date and time in local timezone
date +%s gives local time in epoch time

Is there a way to get GMT time in epoch format?

In the AIX I'm using, there are only -n and -u options available (so I'm unable to use -f option).

My aim is to calculate Offset in hours between localtime and GMT.

I can parse $TZ environment variable which can be tedious. Instead divide epoch time difference between two timezones by 60 should approximately give me the answer.

  • Keep in mind that time zones will change offsets for daylight saving time, and for other anomalies. If you calculate the offset between local and GMT, that is only valid for that particular point in time. At another point in time, it could very well have changed. – Matt Johnson-Pint Jul 6 '15 at 19:43
  • Yes. Agreed. It varies. For my requirement, it's considered to Ok – r.s Jul 7 '15 at 7:34
3

There is no such thing as "local time in epoch format", date +%s always prints the number of seconds since the epoch, namely since 1970-01-01 UTC. It's the same number in all timezones.

However, with most strftime(3) implementations date +%z will print the numeric offset of the specified timezone:

$ TZ=Australia/Darwin date +%z
+0930
$ TZ=America/Toronto date +%z
-0400

This works with GNU date, and with BSD date. I have no idea about AIX though.

Assuming it works, you can calculate the offset in seconds of any timezone. With bash:

#! /bin/bash
offset="$(TZ="$1" date +%z)"
sign=${offset:0:1}1
hours=${offset:1:2}
minutes=${offset:3:2}
if [ x"${hours:0:1}" = x0 ]; then hours="${hours:1}"; fi
if [ x"${minutes:0:1}" = x0 ]; then minutes="${minutes:1}"; fi
printf 'offset of TZ=%s: %d\n' "$1" $(( sign * 3600 * hours + 60 * minutes ))
  • AIX date doesn't do %z. It's a wonder that it does %s which is an annoying omission from POSIX. – Gilles 'SO- stop being evil' Jul 6 '15 at 22:26
  • As @Gilles mentioned, date +%z doesn't give information about offset. It just prints zone: TAIST, CEST, etc,. – r.s Jul 7 '15 at 7:32
1

If your goal is to calculate the difference in hours between UTC/GMT and local time, just ask it this way:

UTC=$(date -u +%H)
LT=$(date +%H)
DIFF=$((UTC - LT))

taking care to note that the result may be negative depending on time of day and relative locations.

  • if absolute value is desired (and you have a newer ksh), then: DIFF=$((abs(UTC-LT))); otherwise, use: DIFF=$((UTC-LC)); DIFF=${DIFF#-} – Jeff Schaller Jul 6 '15 at 13:45
  • That won't work for TZ=Australia/Darwin, which is +0930 off from UTC. :) – lcd047 Jul 7 '15 at 17:11
1

If your AIX has at least Perl 5.9, then you can use the Time::Piece core module to obtain the timezone offset in seconds:

perl -MTime::Piece -le '$t=Time::Piece->new;print $t->tzoffset'

For a particular timezone, do:

TZ=Australia/Darwin perl -MTime::Piece -le '$t=Time::Piece->new;print $t->tzoffset'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.