4

Is there a way to use unix date to print the number of seconds since epoch?

I'm open to using other standard shell commands if there is a way

(I'm using Solaris, so there isn't date "+"%s")

Thanks

Michael Neale
  • 19,248
  • 19
  • 77
  • 109
Mike
  • 58,961
  • 76
  • 175
  • 221
  • 1
    Is perl (perl -e 'print localtime' ) not an acceptable answer? I can't think of how to do it with the date command. – MJB Apr 07 '10 at 12:56
  • @MJB rather than 'print localtime', the comparable perl expression to "date +%s" would be "perl -e 'print time'" – michael Jul 13 '12 at 07:45
  • probably should be considered a duplicate of http://stackoverflow.com/questions/2445198/get-seconds-since-epoch-in-any-posix-compliant-shell (but there are many duplicates of this question) – michael Jul 13 '12 at 07:46

4 Answers4

6
perl -e 'print time'
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
3

Well, since this is a programming site, I would just compile something like:

#include <stdio.h>
#include <time.h>
int main (void) {
    printf ("%d\n",time(0));
    return 0;
}

:-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Try this

/usr/bin/truss /usr/bin/date 2>&1 | grep ^time | awk '{print $3}'

Abhijeet Kasurde
  • 3,937
  • 1
  • 24
  • 33
0

you can try nawk,(but try to double confirm with the perl commands and see if they give the same results)

nawk "BEGIN{print srand}"
ghostdog74
  • 327,991
  • 56
  • 259
  • 343