misc Unix time stuff.
#
ISO8601_TIMESTAMP=$(date +%Y-%m-%d);export ISO8601_TIMESTAMP
THEDATE=$(perl -e ‘@d = localtime((stat(shift))[9]); printf(”%04d-%02d-%02dT%02d:%02d:%02d”, @d[5]+1900,@d[4]+1,@d[3],@d[2],@d[1],@d[0]);’ mypw); echo ${THEDATE}
SECONDS=$(perl -e ‘require “ctime.pl”;use Time::Local;local($_) = shift;(($yr, $mon, $day, $hr, $min, $sec) = /^(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/x);printf “%d”, timelocal (ctime((stat(shift))))’ ${THEDATE}); echo ${SECONDS}
SECONDS=$(perl -e ‘use Time::Local;local($_) = shift;(($yr, $mon, $day, $hr, $min, $sec) = /^(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/x);printf “%d”, timelocal($sec,$min,$hr,$day,$mon - 1,$yr - 1900)’ ${THEDATE}); echo ${SECONDS}
perl -e ‘printf “%d\n”,((stat(shift))[9]);’ myfile
echo $(perl -e ‘use Time::Local;my($day, $mon, $yr, $hr, $min, $sec);local($_) = shift;(($yr, $mon, $day, $hr, $min, $sec) = /^(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/x);printf “%d %d %d %d %d %d\n”, $sec,$min,$hr,$day,$mon - 1,$yr - 1900′ ${THEDATE})
export THEDATE=’2005-02-02T12:12:12′
SECONDS=$(perl -e ‘use Time::Local;local($_) = shift;(($yr, $mon, $day, $hr, $min, $sec) = /^(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/x);printf “%d”, timegm($sec,$min,$hr,$day,$mon - 1,$yr - 1900)’ ${THEDATE})
need to figure out a function to convert ISO8609 to the input format “touch” likes.
“time will be a decimal number of the form:
[[CC]YY]MMDDhhmm [.SS]
where each two digits represent the following:
MM The month of the year [01-12].
DD The day of the month [01-31].
hh The hour of the day [00-23].
mm The minute of the hour [00-59].
CC The first two digits of the year.
YY The second two digits of the year.
SS The second of the minute [00-61]
perl -e ‘require “ctime.pl”; print &ctime($(^T - 60*60), “\n”‘
perl -e ‘print time(), “\n”‘
1083335420
$time = 999523563;
@d = localtime($time); # time as a 9 element array
$d[4] ++; # adjust month
$d[5] += 1900; # adjust year to 4 digits
$date = sprintf( “%04d-%02d-%02d %02d:%02d:%02d”, reverse @d[0..5] );
perl -e ‘@d = localtime(time() - 60*60*5); printf(”%04d%02d%02d”, @d[5]+1900,@d[4],@d[3]);’
perl -e ‘@d = localtime(1083246096); printf(”%04d%02d%02d”, @d[5]+1900,@d[4],@d[3]);’
TIME_SECONDS=$(perl -e ‘@d = localtime(1083246096); printf(”%04d%02d%02d%02d%02d%02d”, @d[5]+1900,@d[4],@d[3],@d[2],@d[1],@d[0]);’)
touch -t ${TIME_SECONDS} mydated.txt
touch -t 200410261200 2004-10-26.txt
perl -e ‘@d = localtime(1083246096); printf(”%04d-%02d-%02dT%02d:%02d:%02d”, @d[5]+1900,@d[4],@d[3],@d[2],@d[1],@d[0]);’
Time in Seconds, NOW! (for later comparision)
With perl you can
perl -e ‘print time(), “\n”‘
also with perl, you can munge it a lot and get the date for five hours ago:
perl -e ‘@d = localtime(time() - 60*60*5); printf(”%04d%02d%02d”, @d[5]+1900,@d[4],@d[3]);’
————
in any arbitrary format: (http://iis1.cps.unizar.es/Oreilly/perl/cookbook/ch03_09.htm)
perl -e ‘use POSIX qw(strftime);print strftime( “%a %b %e %H:%M:%S %z %Y\n”,localtime(time() - 60*60*24));’
Wed Jun 2 11:48:44 2004
========
$time = 999523563;
@d = localtime($time); # time as a 9 element array
$d[4] ++; # adjust month
$d[5] += 1900; # adjust year to 4 digits
$date = sprintf( “%04d-%02d-%02d %02d:%02d:%02d”, reverse @d[0..5] );
perl -e ‘require “ctime.pl”; print &ctime((stat(shift))[9]),”\n”;’ \
oldfile