Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

gettimeofday() vs. systime()

38 views
Skip to first unread message

Kenny McCormack

unread,
Jan 20, 2018, 11:03:01 AM1/20/18
to
I was looking at the "time" extension lib (part of the gawk distro).

A couple questions about the gettimeofday() function arose:

1) Why this function? Isn't it the same as the built-in systime() function?
2) Except that it isn't quite the same. Observe:

$ gawk4 -l time 'BEGIN {print gettimeofday(), systime()}'
1.51646e+09 1516463840
$ gawk4 -l time 'BEGIN {printf("%20d%20d\n", gettimeofday(), systime())}'
1516463882 1516463882

Just curious. Why does it display in exponential notation the first time
(but systime() doesn't) ?

--
Just for a change of pace, this sig is *not* an obscure reference to
comp.lang.c...

Luuk

unread,
Jan 20, 2018, 11:28:03 AM1/20/18
to
On 20-01-18 17:02, Kenny McCormack wrote:
> I was looking at the "time" extension lib (part of the gawk distro).
>
> A couple questions about the gettimeofday() function arose:
>
> 1) Why this function? Isn't it the same as the built-in systime() function?
> 2) Except that it isn't quite the same. Observe:
>
> $ gawk4 -l time 'BEGIN {print gettimeofday(), systime()}'
> 1.51646e+09 1516463840
> $ gawk4 -l time 'BEGIN {printf("%20d%20d\n", gettimeofday(), systime())}'
> 1516463882 1516463882
>
> Just curious. Why does it display in exponential notation the first time
> (but systime() doesn't) ?
>


man gawk says:
systime() Return the current time of day as the number of seconds since
the Epoch

here is says:
gettimeofday() Return the time in seconds that has elapsed since
1970-01-01 UTC as a floating-point value.

Andrew Schorr

unread,
Jan 20, 2018, 9:24:46 PM1/20/18
to
On Saturday, January 20, 2018 at 11:03:01 AM UTC-5, Kenny McCormack wrote:
> 1) Why this function? Isn't it the same as the built-in systime() function?

It is not the same. The gettimeofday function has subsecond accuracy.

> 2) Except that it isn't quite the same. Observe:
>
> $ gawk4 -l time 'BEGIN {print gettimeofday(), systime()}'
> 1.51646e+09 1516463840
> $ gawk4 -l time 'BEGIN {printf("%20d%20d\n", gettimeofday(), systime())}'
> 1516463882 1516463882

Here's a better example:

bash-4.2$ gawk -l time 'BEGIN {printf "%.6f %.6f\n", systime(), gettimeofday(); sleep(.1); printf "%.6f %.6f\n", systime(), gettimeofday()}'
1516501463.000000 1516501463.549218
1516501463.000000 1516501463.649364

Regards,
Andy

Kenny McCormack

unread,
Jan 21, 2018, 1:18:42 AM1/21/18
to
In article <01600c56-0c5a-4d13...@googlegroups.com>,
Andrew Schorr <asc...@telemetry-investments.com> wrote:
...
>Here's a better example:
>
>bash-4.2$ gawk -l time 'BEGIN {printf "%.6f %.6f\n", systime(), gettimeofday();
>sleep(.1); printf "%.6f %.6f\n", systime(), gettimeofday()}'
>1516501463.000000 1516501463.549218
>1516501463.000000 1516501463.649364
>
>Regards,
>Andy

Interesting. Thanks!

--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain
in compliance with said RFCs, the actual sig can be found at the following URL:
http://user.xmission.com/~gazelle/Sigs/InsaneParty

Andrew Schorr

unread,
Jan 21, 2018, 9:55:34 PM1/21/18
to
On Sunday, January 21, 2018 at 1:18:42 AM UTC-5, Kenny McCormack wrote:
> Interesting. Thanks!

And by the way, the man page does clearly state:

gettimeofday()
This function returns the number of seconds since the Epoch as a
floating-point value. It should have subsecond precision. It
returns -1 upon error and sets ERRNO to indicate the problem.

Regards,
Andy
0 new messages