TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

gettimeofday() should never be used to measure time

95 pointsby aethertapover 10 years ago

11 comments

MichaelGGover 10 years ago
Except when doing a pcap, I actually want the actual time it is. Maybe in some cases you&#x27;d explicitly want an offset, but not in general. It&#x27;s really a plea to get your clocks synced up, so you aren&#x27;t forced to choose between reporting an incorrect time or an incorrect duration. If I&#x27;m running a pcap and the system time changes over a day by several seconds, I&#x27;d prefer each packet to report the closest concept of the right time instead of being way off as time goes by.<p>Not to mention: if the monotonic clock can keep such accurate timing, then everyone would just use that and NTP would not be so necessary.<p>Really: Under what conditions do you have a usefully functioning system when the clock is so off you need to do multi minute jumps? Even HyperV, with the utterly atrocious w32time manages to keep it with a minute or two (and a Linux guest can easily have ~ms accuracy).<p>The leap second point is valid, but that&#x27;s an argument against leap seconds which serve no use in today&#x27;s society other than to introduce unnecessary problems. Even Google just gives up and purposely introduces inaccuracies in their clocks for a day so that when the leap second comes around they&#x27;re synced again. A leap hour would be a far better solution, as it&#x27;s something many people are (unfortunately) used to from DST, and it wouldn&#x27;t bother us for a dozen centuries.
评论 #8909863 未加载
评论 #8911675 未加载
teddyhover 10 years ago
I wish that HN would use the Public Suffix List (<a href="https://publicsuffix.org/" rel="nofollow">https:&#x2F;&#x2F;publicsuffix.org&#x2F;</a>) in its algorithm to display domain names of submissions. That way, we wouldn’t get things like this, where the domain given (pp.se) does not say anything about what the actual site is.
评论 #8911161 未加载
JoeAltmaierover 10 years ago
OS time handling is feeble across the industry. Its inherited from 20-year-old ideas about APIs. Not just the antique time structures that are useful for rendering but not much else. Also the abominable Sleep() and such.<p>Imagine you want to do something every second. You Sleep(1000) or some such. But it takes time to do the thing, so its actually a bit longer between loops. Maybe it doesn&#x27;t matter; maybe it does. But you&#x27;re stuck doing stuff like that.<p>Why not Wait(timetowaitfor). Not a duration; the actual time you want to be woken up. Now it still takes time to wake up and run. And it takes time to make the call. But now, your stuff actually runs say 60 times per minute (e.g. if you wait for successive seconds), hour after hour and day after day.<p>Also, what&#x27;s with limited resolution on the time? Its due to the common implementation of timers as a counter of ticks, where a tick is whatever regular interval some hardware timer is set to interrupt. Why not instead interrogate a free-running counter? And if I want to wait 1 second plus 150 nanoseconds, then I Wait for that time to arrive, and the library (or OS) set a real timer interrupt to go off when that time has arrived? Sure there&#x27;s latency in calling me back; that&#x27;s inevitable. What&#x27;s not inevitable is some limited multi-millisecond tick resolution.<p>Anyway, whenever I&#x27;m in charge of designing an OS or application environment, I provide real timers like this. It&#x27;s about time the big OS providers catch up to the 21st century.
评论 #8911873 未加载
评论 #8911099 未加载
评论 #8910736 未加载
vojfoxover 10 years ago
It&#x27;s a similar situation on iOS, where new developers sometimes use (in Objective-C) `[[NSDate date] timeIntervalSince1970]` which is natural, but wrong. NSDate draws from the network synchronized clock and will occasionally hiccup when re-synching it against the network, among other reasons.<p>If you&#x27;re looking at measuring relative timing (for example for games or animation), you should instead use `double currentTime = CACurrentMediaTime();` That&#x27;s the correct way.
conradkover 10 years ago
Ironic that the &quot;What to use instead&quot; part doesn&#x27;t even feel the need to check return values of functions.<p>Am I missing something ?
评论 #8911654 未加载
评论 #8910784 未加载
sargunover 10 years ago
Let&#x27;s talk about the sad state of clocks today. There exists a few ways to query NTP time on Linux. (1) Directly through NTP (2) the adjtimex syscall, (3) the ntp_gettime call. I found it hard to find many codebases using the proper NTP. In fact codebases that need reliable time, like Cassandra and OpenLDAP. don&#x27;t use NTP time APIs to check whether the system clock is in sync, or to get accurate time. Even if we were to make PTP accessible to the world, it would be some time before its usage actually became ubiquitous. The understandability of time keeping, and clock yielding in our community is a sore point.
评论 #8910729 未加载
dbrowerover 10 years ago
I think the article and much of the discussion misses a larger point -- time is hard, and very very hard when there are multiple systems with different clocks. The APIs are the way they are because there just aren&#x27;t solutions especially since all systems ultimately have unreliable connections to good time sources.<p>The miserable APIs are New Jersey&#x2F;Worse-is-better answers to intractable problems.
评论 #8911327 未加载
评论 #8911683 未加载
dsjoergover 10 years ago
the semantics you&#x27;d like the OS + standard library to provide would be some kind of gettime() call that returns a time thingie, and a secondsbetween(a,b) call that reliably tells you the time between the two time thingies.<p>the fact that it doesn&#x27;t already work this way is a design fail.<p>all the nonsense about NTP and clock slew and monotonicity are implementation details that should be hidden below this layer.
shanwangover 10 years ago
the last time I tested them on redhat 6, clock_gettime(REALTIME) and gettimeofday are slightly faster than clock_gettime(CLOCK_MONOTONIC), and gettimeofday is much faster than any clock_gettime on older platforms.
pstratemanover 10 years ago
This isn&#x27;t even right....<p>The correct call is to clock_gettime(CLOCK_MONOTONIC_RAW...
评论 #8909716 未加载
评论 #8909727 未加载
评论 #8909658 未加载
shared4youover 10 years ago
OP or mods: please add [2010] tag to the title.
评论 #8909536 未加载