how can I get the current standard epoch time (seconds from 1970) in a shell script?
I know I could do this with a bit of perl of even c++ but i want to do it in Bourne shell.....
how can I get the current standard epoch time (seconds from 1970) in a shell script?
I know I could do this with a bit of perl of even c++ but i want to do it in Bourne shell.....
date +%s should give you that.
only with GNU date ,which we dont run on solaris systems
Recent Solaris versions have perl.
perl -e 'print time(), "\n" '
And when perl is missing there is still nawk:
nawk 'BEGIN{print srand()}'
very cool, thanks
perl -e 'print time(), "\n" ' prints only epoch of system time
How to get epoch of some other time than current system time?
for example I want to know the epoch of Jan 1 2000, 00hrs 00 min 00 seconds
If your date version support these options you can try this:
date +%s -d "01/01/2000 00:00:00"
Regards
date +%s, doesn't work, I tried with %S (not %s), date +%S , this gives the seconds but not epoch
Do we have any other options with perl or nawk.
Btw, I am on Solarid
Not too pretty, but pretty quick...
$ touch -t 200001010000.00 somefile
$ perl -e 'printf "%d\n", ((stat(shift))[9]); ' somefile
946702800
$ perl -e 'print scalar localtime(shift),"\n"' 946702800
Sat Jan 1 00:00:00 2000
$
1) touch a file to get the desired timestamp
2) use perl to display that timestamp in seconds
3) to verify, I use perl to convert unix internal time to human readable
/usr/bin/truss /usr/bin/date 2>&1 | /usr/bin/awk '/^time/ {print $NF}'
vgersh99 - that is a very cool solution!
perl -e 'use Time::Local; print timelocal("00","00","00","01","01","2000"),"\n";'
The Solaris dtrace facility can also display the seconds since Epoch.
#!/usr/sbin/dtrace -qs
tick-1sec
{
printf("%d seconds since the epoch\n", `time);
exit(0)
}
Example , To translate Tuesday may 12 2009 15:47:04 to epoch seconds . in PST zone
TZ=PST date -d "20090512 15:47:04" +%s
This is GNUism - not applicable for all platforms.