views:

150

answers:

1

I'd like to know if there's a way to get the number of seconds since the UNIX epoch in any POSIX compliant shell, without resorting to non-POSIX languages like perl, or using non-POSIX extensions like GNU awk's strftime function.

Here are some solutions I've already ruled out...

date +%s // Doesn't work on Solaris

I've seen some shell scripts suggested before, which parse the output of date then derive seconds from the formatted gregorian calendar date, but they don't seem to take details like leap seconds into account.

GNU awk has the strftime function, but this isn't available in standard awk.

I could write a small C program which calls the time function, but the binary would be specific to a particular architecture.

Is there a cross platform way to do this using only POSIX compliant tools?

I'm tempted to give up and accept a dependency on perl, which is at least widely deployed.

perl -e 'print time' // Cheating (non-POSIX), but should work on most platforms

+2  A: 

I've done the binary in the past; I'd be sorely tempted to go with Perl these days. There isn't a standard way that I know of. (NB: date +%s is not POSIX compliant.) Also note that POSIX does not take leap seconds into account. Period.

Jonathan Leffler
I do the same thing, usually putting the source of the 'helper' in the script as a here doc, then compiling it if it does not yet exist. NB, this is only when I have to deal with a 'date' that does not support +s
Tim Post