At the time of running following commands my Unix system's time for eastern day light savings was --> 201104210003(yyyymmddHHSS)
# This seem to be more accurate..
$> echo $(TZ=$(date +%Z)+28 date +%Y%m%d%H%M)
201104200003
# This was constructed from old posts from this forum to find Yesterday's date.
$> echo $(TZ=$(date +%Z)+24 date +%Y%m%d%H%M)
201104200403
$> echo $(date +%Z)
EDT
Can someone tell what would be the 100% accurate solution that takes daylight savings also into account using ksh.
Preferrably still using one line solution.
I also do not fully understand what exactly above code solution is doing, if at all it can be still used!? I allways thought +24 is good, why do I need to use +28 !?
/usr/ahamed-> date
Mon Dec 20 02:51:14 CST 2010
/usr/ahamed-> date +%G%m%d%H%M
201012200251
/usr/ahamed-> date +%G%m%d%H%M --date="yesterday"
201012190251
I did lot of searching and read lot of postings about this on this site and finally decided to use my own improved syntax
---> $(TZ=$(date +%Z)+24 date +%Y%m%d%H%M)
But I found a bug with this,, apparently this is putting yesterday's datetime 4 hrs ahead of what its supposed to be.
Please be patient and validate what I found. This is really important for scheduling jobs I am sure for many of us using older shells. I think its worth re-validating it.
Unless you print Hours and minutes you will not know what this is giving is true or not. Or test this after 10 PM.
cambridge,
That is incredible. Your solution is not only 100% correct, it also formats it.
I am still looking for a reason why the ksh syntax that almost everyone said is working in so many previous posts puts time 4 hrs ahead with "$(TZ=$(date +%Z)+24 date +%Y%m%d%H%M)" and works with " echo $(TZ=$(date +%Z)+28 date +%Y%m%d%H%M)" syntax.
I personally wouldn't choose to modify the TZ variable in order to do date manipulation, because it's not so easy for others to understand your code in the future. However, you can do more background reading here: TZ Variable - The GNU C Library
That "+24" construct is not portable and that's why it's biting you. If you read that FAQ article, I point out the non-portability of this technique. But the idea that the authors of this technique were shooting for was to add 24 to the offset already used by the timezone software. It appears that you are overriding your value rather than modifying it. Rather than screwing around with this troublesome contruct, why not just use one the reasonable solutions that have been offered?
The main idea of the thread mentionned above is to first calculate your offset to make reliable the calculation of the yesterday date based on the timezone and taking into account that offset.
So that the yesterday date is accurrate.