Get seconds since epoch in any POSIX compliant shell

Posted by mattbh on Stack Overflow See other posts from Stack Overflow or by mattbh
Published on 2010-03-15T05:06:17Z Indexed on 2010/03/15 5:09 UTC
Read the original article Hit count: 511

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about posix

Related posts about shell