Time zone issues in UNIX flavors

Hello All,

I am in process of migrating all my scripts from AIX box to Linux box. In one of my script I calculate my last week date with the below command

$ TZ=EDT+172 date +%F
2012-12-13

$ uname -a
AIX 1 7 000B29AAD400

Now when I tried running the same in Linux, it gives a false output

$ uname -a
Linux 2.6.18-308.11.1.el5 x86_64 x86_64 x86_64 GNU/Linux

$ TZ=EDT+172 date +%F
2012-12-19

I know in Linux we can use

date -d

command to obtain the desired date, since my code contains huge number of these, I don't like to get it changed.

Can some one please help on how to get the Timezone concept work in Linux in the similar fashion as in AIX, thanks in advance.

The offset in TZ for GNU date (or libc?) seemed can't be bigger than 24. You need to convert the day part into -d argument as you said. Either override your date command with a function:

function date
{
  local t="${TZ%%[-0-9+]*}"
  local n="${TZ#$t}"
  [ -z "$t" ] && { command date "$@"; return; }
  #TZ=$t$((n%24)) /bin/date -d "$((-n/24)) days" "$@"
  TZ=$t$((n%24)) command date -d "$((-n/24)) days" "$@"
}

or convert all your scripts with:

awk '{
  if (match($0, /TZ=[A-Z]+[-+0-9 ]+date/)) {
    n = s = substr($0, RSTART, RLENGTH)
    gsub(/[^-+0-9]/, "", n)
    sub(n, sprintf("%+d", n%24), s)
    x = sprintf("%+d days", -int(n/24))
    $0 = substr($0,1,RSTART-1) s " -d \"" x "\"" substr($0,RSTART+RLENGTH)
  }
  print
}'
1 Like

I don't have permissions to touch my date function. I will get my script amended with

date -d

:frowning:

Thanks for your time