Post date: May 9, 2011 6:18:03 AM
Just looking around and found something that might be useful. The GNU date routine has some extensions that allow it to parse dates (you may have known that but) in many free form values. Some of this has just been added so older Linux etc may not yet have all of it.
$ date -d 'last friday 12:00'
$ date -d @1210543200
Using the TZ environment variable you can use any timezone (other ways available, but this is most flexable)
$ TZ=MST7MDT date -d 12:00:00
$ TZ=America/Chicago date -d 12:00:00
There are a couple of formatting extensions
%s = seconds since epoch (Unix epoch)
%N = nanoseconds
You can then convert times by
TZ=America/Chicago date -d $(TZ=Asia/Tokyo date -d 'may 12, 2008 07:00:00' +@%s)
7am tokyo -> central time
note that I added the '@' in the format string to pass unix time to the outer 'date' call Timezone can be in the tzinfo format:
[:]America/Denver
or in the older TZ format
EST+5EDT,M4.1.0/2,M10.5.0/2
5hrs west switching on the first Sunday in April at 2am, last Sunday in October at 2
Some of this is hard to find (not in the man pages) so use the 'info' command
$ info libc
m TZ Variable
'm' takes you to the command line and asks for the name of a menu item
tab completes so you can type 'tz' assuming you have the same info I have.
$ info coreutils
m Date input formats
several menus below here to look at just move the cursor and press return
u
to get back up one level
A shell script to convert YYYYMMDDHHMMSS.sss UTC timestamps to Unix epoch values.
#!/bin/bash
if [[ $1 =~ "(........)(..)(..)(.*)" ]]; then
set "${BASH_REMATCH[@]}" # shorten the command below
date +%s.%N -d "$2 $3:$4:$5Z"
else
echo "0.0"
fi
for C, see getdate(3) and probably better strptime(2). Watch out for getdate if you need performance, I remember that it (use to) open the file with the patterns every time it was called.