---------- Post updated at 09:40 PM ---------- Previous update was at 09:18 PM ----------
Looking back at the results of the date command now (with the way I'm using it), I'm finding the answers to be off by +1 or -1 hr depending on the month (not just March and October). It may be a bigger problem.
What is there to work around? There is nothing wrong with the result. The only possible issue is that your date format is ambiguous because it does not include a timezone.
If you prefer to avoid daylight savings time, use UTC.
Adding daylight savings time or any other time zones does not correct this issue. I still see the hour result being incremented by -1 or 1, whether it's in March, October, July, February, November. I'm still befuddled by this...
Thanks!
---------- Post updated at 09:47 AM ---------- Previous update was at 08:15 AM ----------
I wrote an if statement adding or subtracting an hour when I encounter this, so the problem is solved for me. But it's less than ideal that I have to write additional code to work around an important and widely used script function.
I think the question has already been answered, but to reduce confusion, perhaps include the time zone %Z to see what is happening...
$ date --date "1979-10-26" '+%Y%m%d%H %Z'
1979102600 GMTDT
$ date --date "1979-10-26 +54 hours" '+%Y%m%d%H %Z'
1979102805 GMTST
$ date --utc --date "1979-10-26 +54 hours" '+%Y%m%d%H %Z'
1979102806 UTC
$
If you are still "befuddled", then that is to be expected. The first page of info date provides a quote...
File: coreutils.info, Node: Date input formats
28 Date input formats
*********************
First, a quote:
Our units of temporal measurement, from seconds on up to months,
are so complicated, asymmetrical and disjunctive so as to make
coherent mental reckoning in time all but impossible. Indeed, had
some tyrannical god contrived to enslave our minds to time, to
make it all but impossible for us to escape subjection to sodden
routines and unpleasant surprises, he could hardly have done
better than handing down our present system. It is like a set of
trapezoidal building blocks, with no vertical or horizontal
surfaces, like a language in which the simplest thought demands
ornate constructions, useless particles and lengthy
circumlocutions. Unlike the more successful patterns of language
and science, which enable us to face experience boldly or at least
level-headedly, our system of temporal calculation silently and
persistently encourages our terror of time.
... It is as though architects had to measure length in feet,
width in meters and height in ells; as though basic instruction
manuals demanded a knowledge of five different languages. It is
no wonder then that we often look into our own immediate past or
future, last Tuesday or a week from Sunday, with feelings of
helpless confusion. ...
-- Robert Grudin, `Time and the Art of Living'.
You have yet to demonstrate that there is actually a shortcoming anywhere outside of your own understanding. That is not intended as an insult; it's just an objective analysis of your postings in this thread so far. Consider that the only date example you've posted to demonstrate the "bug" actually returns the correct result.
If you believe that other invocations are returning an erroneous result, then post them. Also, it can't hurt if you list the exact date implementation and version used, and some information about your operating system platform.
And if this is part of a larger script, and if that script in anyway handles the date result before it's displayed, then post that code as well.
Thank you for your help! Yes, this is not a date command bug. The "--utc" option solved the problem. Adding "UTC" in the date string did not work well, which is why I was stuck for a while.
Thank you for reporting back with the solution to your issue.
For future reference, adding a timezone abbreviation to the date format will never affect the output's timezone; it will only cause confusion if the abbreviation does not correspond to the actual timezone in use.
To use UTC, as Ygor demonstrated, you can use the --utc (or -u) option. For UTC or any other timezone, set the TZ environment variable appropriately. This can be done on a per command or login session.