2

I was trying to find older dates using TZ="GMT+NoOfHours" date. To my surprise, it was absolutely working as expected when NoOfHours >= 0 to NoOfHours <=167(returning date older by 1-7 days). But, when NoOfHours went greater than 167 it returned current date. I'm curious to know the reason for this behavior rather than other possible solutions.

Current date

-bash-5.0$ TZ="GMT" date
Thu Mar  4 14:59:42 GMT 2021

NoOfHours = 144(24*6)

-bash-5.0$ TZ="GMT+144" date
Fri Feb 26 14:59:47 GMT 2021

NoOfHours = 168(24*7)

-bash-5.0$ TZ="GMT+168" date
Thu Mar  4 14:59:50 GMT 2021

And, this left me confused!

-bash-5.0$ TZ="GMT+167" date
Thu Feb 25 15:59:54 GMT 2021
3
  • 3
    Why the drive-by downvote? It's a reasonable question that seems to be on-topic Mar 4, 2021 at 17:57
  • 1
    @roaima Always this undying hope that the down-voter would return to explain ... ;)
    – AdminBee
    Mar 5, 2021 at 8:31
  • 2
    More an attempt at reassurance to a relatively new user that their question is good Mar 5, 2021 at 8:33

1 Answer 1

3

The TZ variable is defined under POSIX to range from -24 to +24:

The hour shall be between zero and 24, and the minutes (and seconds)-if present-between zero and 59. The result of using values outside of this range is unspecified. If preceded by a '-', the timezone shall be east of the Prime Meridian; otherwise, it shall be west (which may be indicated by an optional preceding '+' ).

GNU and others have extended this range to ±167 hours:

The hours part of the time fields can range from -167 through 167; this is an extension to POSIX.1, which allows only the range 0 through 24

Solaris 11.4 appears to be strictly POSIX in regard to its documentation, but GNU-like in regard to its implementation (or perhaps you are using the GNU date variant in your testing)

3
  • BTW: POSIX requires at least -25h < off < +26h since there is an overlap in the date limit. It may be that someone just selected the next larger unit > than a day. This is because the offset des not only apply to GMT but to any valid timezone.
    – schily
    Mar 4, 2021 at 21:40
  • @schily it's an hh[:mm[.ss]] offset from UTC Mar 4, 2021 at 22:32
  • Sorry, the name before the offset is the name to be used ad the offset is an inverse GMT offset. I was mistaken.
    – schily
    Mar 5, 2021 at 0:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .