RE: Filling in a struct timespec with the current date?
site_archiver@lists.apple.com Delivered-To: darwin-dev@lists.apple.com Thread-index: AcoPtHsvzHJigHWKSe25Tdk8NQGYPAAC4lLA Thread-topic: Filling in a struct timespec with the current date? Terry Lambert wrote on Tuesday, July 28, 2009 2:51 PM
On Jul 28, 2009, at 10:07 AM, Kevin Van Vechten <kvv@apple.com> wrote:
On Jul 28, 2009, at 9:57 AM, Karan, Cem (Civ, ARL/CISD) wrote:
Jean-Daniel Dupas wrote on Tuesday, July 28, 2009 12:52 PM
Le 28 juil. 09 à 18:42, Karan, Cem (Civ, ARL/CISD) a écrit :
I'm trying to use struct timespec, and I want to initialize it to the current date (sec & nanosec). Is there a function call that I can call that will do so, or do I have to roll my own? Note that I'm looking for an API that will fill in a timespec that I pass in, not create a new one on the heap.
You can wrap your own using gettimeofday() which does what you want but using a timeval instead of a timespec.
Yeah, that is what I'm doing now; I was hoping there was a cleaner way, something along the lines of:
ERROR_CODE initWithCurrentDate(&myTimeSpec);
If there isn't, then such is life; I'll continue using what gettimeofday().
Wrapping gettimeofday(3) is the way to go. There is a POSIX REALTIME interface clock_gettime(3) that returns a struct timespec, but Mac OS X does not implement these optional interfaces.
Or if he needs better resolution, mach_absolute_time(), which is well documented at <http://developer.apple.com>.
Even if we did a clock_gettime(), you'd need to call clock_getres() to get the resolution, and like gettimeofday(), there's no guarantee that just because the structure is capable of representing a resolution that that's the resolution you'd get. I could give you the interface tomorrow, but depending on the resolution I picked, it could be worse than gettimeofday() while still being technically standards compliant. Without the rest of the [TMR] functionality, though, the feature test macro from <unistd.h> that would let you use the interface safely in the knowledge it was compliant would still claim it wasn't there.
The only guaranteed resolution is going to be if you call mach_absolute_time() and then do the numerator/denominator math via the contents of the mach_timebase_info structure (i.e. you can not assume it is in nanoseconds or that the value is 1/1, particularly for hardware clocked at less than 1GHz, for example, an iPhone).
In general, timespec is mostly useful as a parameter to things like pselect to contain delta times, rather than useful for times since the epoch, and for measuring time, for example for benchmarking a new version of your code against the previous version of your code to check for regressions, you probably want better resolution than you'll get from gettimeofday().
Wow! Thank you Terry, I'll recode my code to use that immediately; it's a lot more accurate than what I need at the moment, but having accurate clock code that I can reuse is always useful! Thanks, Cem Karan _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-dev mailing list (Darwin-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-dev/site_archiver%40lists.appl... This email sent to site_archiver@lists.apple.com
participants (1)
-
Karan, Cem (Civ, ARL/CISD)