time-nuts@lists.febo.com

Discussion of precise time and frequency measurement

View all threads

Anyone know a portable way of getting seconds since epoch?

DK
David Kirkby
Tue, Oct 13, 2009 2:03 PM

I've asked this on comp.unix.shell, but never got a 100% satsifactory
answer. Perhaps someone here might know.

Does anyone know how to get the number of seconds since 1/1/1970 on a
Unix system using the shell - not compiling C code. I can't assume the
computer has perl, python or a C compiler.

http://shell.cfajohnson.com/cus-faq.html#Q6
says:


  • GNU date has the %s format option which returns the epoch
    time.

    • More portably, use awk.

      awk 'BEGIN {srand(); printf("%d\n", srand())}'

      This works because srand() sets its seed value with the
      current epoch time if not given an argument. It also returns
      the previous seed value, so the second call gives the epoch
      time.

      Note that this doesn't work with older versions of awk. This
      requires a version supporting the POSIX spec for srand(). For
      example, on Solaris this will not work with /usr/bin/awk, but
      will with nawk or /usr/xpg4/bin/awk.


The problem is, from what I gather, POSIX only says the random number
generator has to be seeded from the time, but not what time. Most awks
use seconds since the epoch, but apparently OpenBSD does not. Someone
has proposed that microseconds since midnight would be better. There
appears to be no standard.

The awk/srand method has worked for me on AIX, HP-UX, Linux, OS X and Solaris.

For the purpose I have for this, leapseconds would be best ignored,
though it is not essential they are.

Dave

I've asked this on comp.unix.shell, but never got a 100% satsifactory answer. Perhaps someone here might know. Does anyone know how to get the number of seconds since 1/1/1970 on a Unix system using the shell - not compiling C code. I can't assume the computer has perl, python or a C compiler. http://shell.cfajohnson.com/cus-faq.html#Q6 says: ------------------------------------------------------ - GNU date has the %s format option which returns the epoch time. - More portably, use awk. awk 'BEGIN {srand(); printf("%d\n", srand())}' This works because srand() sets its seed value with the current epoch time if not given an argument. It also returns the previous seed value, so the second call gives the epoch time. Note that this doesn't work with older versions of awk. This requires a version supporting the POSIX spec for srand(). For example, on Solaris this will not work with /usr/bin/awk, but will with nawk or /usr/xpg4/bin/awk. ------------------------------------------------------------- The problem is, from what I gather, POSIX only says the random number generator has to be seeded from the time, but not what time. Most awks use seconds since the epoch, but apparently OpenBSD does not. Someone has proposed that microseconds since midnight would be better. There appears to be no standard. The awk/srand method has worked for me on AIX, HP-UX, Linux, OS X and Solaris. For the purpose I have for this, leapseconds would be best ignored, though it is not essential they are. Dave
PK
Poul-Henning Kamp
Tue, Oct 13, 2009 2:10 PM

I've asked this on comp.unix.shell, but never got a 100% satsifactory
answer. Perhaps someone here might know.

Does anyone know how to get the number of seconds since 1/1/1970 on a
Unix system using the shell - not compiling C code. I can't assume the
computer has perl, python or a C compiler.

On FreeBSD you can use the strftime facility in date(1):

$ date +%s
1255442977

Poul-Henning

--
Poul-Henning Kamp      | UNIX since Zilog Zeus 3.20
phk@FreeBSD.ORG        | TCP/IP since RFC 956
FreeBSD committer      | BSD since 4.3-tahoe
Never attribute to malice what can adequately be explained by incompetence.

In message <286f7bad0910130703v6680affbx95905a440000099f@mail.gmail.com>, David Kirkby writes: >I've asked this on comp.unix.shell, but never got a 100% satsifactory >answer. Perhaps someone here might know. > >Does anyone know how to get the number of seconds since 1/1/1970 on a >Unix system using the shell - not compiling C code. I can't assume the >computer has perl, python or a C compiler. On FreeBSD you can use the strftime facility in date(1): $ date +%s 1255442977 Poul-Henning -- Poul-Henning Kamp | UNIX since Zilog Zeus 3.20 phk@FreeBSD.ORG | TCP/IP since RFC 956 FreeBSD committer | BSD since 4.3-tahoe Never attribute to malice what can adequately be explained by incompetence.
JH
Javier Herrero
Tue, Oct 13, 2009 8:20 PM

Poul-Henning Kamp escribió:

I've asked this on comp.unix.shell, but never got a 100% satsifactory
answer. Perhaps someone here might know.

Does anyone know how to get the number of seconds since 1/1/1970 on a
Unix system using the shell - not compiling C code. I can't assume the
computer has perl, python or a C compiler.

On FreeBSD you can use the strftime facility in date(1):

$ date +%s
1255442977

Poul-Henning

And same works for linux, too

Regards,

Javier

--

Javier Herrero                            EMAIL: jherrero@hvsistemas.com
HV Sistemas S.L.                          PHONE:        +34 949 336 806
Los Charcones, 17A                        FAX:          +34 949 336 792
19170 El Casar - Guadalajara - Spain      WEB: http://www.hvsistemas.com

Poul-Henning Kamp escribió: > In message <286f7bad0910130703v6680affbx95905a440000099f@mail.gmail.com>, David > Kirkby writes: >> I've asked this on comp.unix.shell, but never got a 100% satsifactory >> answer. Perhaps someone here might know. >> >> Does anyone know how to get the number of seconds since 1/1/1970 on a >> Unix system using the shell - not compiling C code. I can't assume the >> computer has perl, python or a C compiler. > > On FreeBSD you can use the strftime facility in date(1): > > $ date +%s > 1255442977 > > Poul-Henning > And same works for linux, too Regards, Javier -- ------------------------------------------------------------------------ Javier Herrero EMAIL: jherrero@hvsistemas.com HV Sistemas S.L. PHONE: +34 949 336 806 Los Charcones, 17A FAX: +34 949 336 792 19170 El Casar - Guadalajara - Spain WEB: http://www.hvsistemas.com
DK
David Kirkby
Tue, Oct 13, 2009 9:14 PM

2009/10/13 Poul-Henning Kamp phk@phk.freebsd.dk:

I've asked this on comp.unix.shell, but never got a 100% satsifactory
answer. Perhaps someone here might know.

Does anyone know how to get the number of seconds since 1/1/1970 on a
Unix system using the shell - not compiling C code. I can't assume the
computer has perl, python or a C compiler.

On FreeBSD you can use the strftime facility in date(1):

       $ date +%s
       1255442977

Poul-Henning

But it does not work on Solaris or HP-UX

2009/10/13 Poul-Henning Kamp <phk@phk.freebsd.dk>: > In message <286f7bad0910130703v6680affbx95905a440000099f@mail.gmail.com>, David >  Kirkby writes: >>I've asked this on comp.unix.shell, but never got a 100% satsifactory >>answer. Perhaps someone here might know. >> >>Does anyone know how to get the number of seconds since 1/1/1970 on a >>Unix system using the shell - not compiling C code. I can't assume the >>computer has perl, python or a C compiler. > > On FreeBSD you can use the strftime facility in date(1): > >        $ date +%s >        1255442977 > > Poul-Henning But it does not work on Solaris or HP-UX
DI
David I. Emery
Wed, Oct 14, 2009 4:16 AM

On Tue, Oct 13, 2009 at 10:14:56PM +0100, David Kirkby wrote:

2009/10/13 Poul-Henning Kamp phk@phk.freebsd.dk:

I've asked this on comp.unix.shell, but never got a 100% satsifactory
answer. Perhaps someone here might know.

Does anyone know how to get the number of seconds since 1/1/1970 on a
Unix system using the shell - not compiling C code. I can't assume the
computer has perl, python or a C compiler.

On FreeBSD you can use the strftime facility in date(1):

       $ date +%s
       1255442977

Poul-Henning

But it does not work on Solaris or HP-UX

FWIW it works on Darwin (MacOS X) which is BSD derived...

--
Dave Emery N1PRE/AE, die@dieconsulting.com  DIE Consulting, Weston, Mass 02493
"An empty zombie mind with a forlorn barely readable weatherbeaten
'For Rent' sign still vainly flapping outside on the weed encrusted pole - in
celebration of what could have been, but wasn't and is not to be now either."

On Tue, Oct 13, 2009 at 10:14:56PM +0100, David Kirkby wrote: > 2009/10/13 Poul-Henning Kamp <phk@phk.freebsd.dk>: > > In message <286f7bad0910130703v6680affbx95905a440000099f@mail.gmail.com>, David > >  Kirkby writes: > >>I've asked this on comp.unix.shell, but never got a 100% satsifactory > >>answer. Perhaps someone here might know. > >> > >>Does anyone know how to get the number of seconds since 1/1/1970 on a > >>Unix system using the shell - not compiling C code. I can't assume the > >>computer has perl, python or a C compiler. > > > > On FreeBSD you can use the strftime facility in date(1): > > > >        $ date +%s > >        1255442977 > > > > Poul-Henning > But it does not work on Solaris or HP-UX FWIW it works on Darwin (MacOS X) which is BSD derived... -- Dave Emery N1PRE/AE, die@dieconsulting.com DIE Consulting, Weston, Mass 02493 "An empty zombie mind with a forlorn barely readable weatherbeaten 'For Rent' sign still vainly flapping outside on the weed encrusted pole - in celebration of what could have been, but wasn't and is not to be now either."