yesterday=$(perl -e 'use Date::Format;print time2str("%y%m%d%H%M",time-1800)."\n";')The above example is a simple perl one-liner which will give you a timestamp from ½ hour ago.
i.e. ran at 12:51 on 01/02/2010 it will return 201002011221 i.e. 12:21
This perl example and this date/timestamp YYYYMMDDHHMM format is useful because you can pass this format to the touch command, and I've used this in scripts in the past to create a temp file with a timestamp of half hour ago and then passed this file to the find command to pickup files that were either modified before or after this time, e.g. for files that haven't been modified in last 10 minutes
tStamp=$(perl -e 'use Date::Format;print time2str("%y%m%d%H%M",time-600)."\n";)`
touch -t ${tStamp} /tmp/my_timestamp
find /path/to/dir -name "File_Glob_You_Want*" -type f ! -newer /tmp/my_timestamp ! -size 0`
The server this script was to run on wasn't configured correctly as it had no nameservers in resolv.conf or dns entires in nsswitch.conf. I added these but it still wouldn't resolve domain names so I looked for a way to find yesterday's date without using Perl. Turns out you can do this simply in Solaris by specifying the Timezone just before the date command.
TZ=NZ date +'%d-%b-%Y %H:%M'
will take current time and add 12 hours as New Zealand is 12 hours ahead ot UTC/GMT
Problem is I needed yesterdays date and it had to be run at approx 4am so you needed a timezone that was 5 hours behind to be safe. If you want to see what Timezones are available just have a look in /usr/share/lib/zoneinfo/ directory, you can then pick any of these and use it in your date command but perhaps the simplest to use are the GMT+n and GMT-n
So running at 4am you could use any of the following to get yesterdays date in oracle DD-MM-YYYY format.
TZ=US/Alaska date +'%d-%b-%Y %H:%M' TZ=GMT+5 date +'%d-%b-%Y %H:%M' TZ=Chile/EasterIsland date +'%d-%b-%Y %H:%M'
What I can't figure out is why the GMT+?? & GMT-?? are intuitively the wrong way round. In above example i had to use GMT+5 to give me the current time in Lima, Peru despite Lima being GMT-5. I thought at 1st this might be a Solaris 9 bug but i tested on Ubuntu and it has the exact same behaviour.
TZ=Etc/GMT-12 date # (gives current time + 12 hours)
TZ=Etc/GMT+12 date # (gives current time - 12 hours)
For more into on Timezones have a look at wikipedia
No comments:
Post a Comment