3

Apple has extremely comprehensive documentation, but I can't find any documentation for the function AbsoluteToNanoseconds? I was to find the difference between AbsoluteToNanoseconds and AbsoluteToDuration.

Note

I am beginning to think that the Apple Docs only cover Objective-C functions? Is this the case?

I found the following by using Apple-double-click:

Duration 32-bit millisecond timer for drivers AbsoluteTime 64-bit clock

Kara
  • 6,115
  • 16
  • 50
  • 57
Casebash
  • 114,675
  • 90
  • 247
  • 350
  • 3
    “I am beginning to think that the Apple Docs only cover Objective-C functions? Is this the case?” No. Many C-based APIs are documented. You should file a bug report asking for AbsoluteToNanoseconds and friends to be documented: https://bugreport.apple.com/ – Peter Hosey Jan 28 '10 at 04:51

2 Answers2

3

I'm not sure why it isn't documented anywhere, but here is an example of how it is used, if that helps:

static float HowLong(
    AbsoluteTime endTime,
    AbsoluteTime bgnTime
    )
{
    AbsoluteTime absTime;
    Nanoseconds  nanosec;

    absTime = SubAbsoluteFromAbsolute(endTime, bgnTime);
    nanosec = AbsoluteToNanoseconds(absTime);
    return (float) UnsignedWideToUInt64( nanosec ) / 1000.0;
}

UPDATE:

"The main reason I am interested in the docs is to find out how it differs from AbsoluteToDuration"

That's easier. AbsoluteToNanoseconds() returns a value of type Nanoseconds, which is really an UnsignedWide struct.

struct UnsignedWide {
  UInt32              hi;
  UInt32              lo;
};

In contrast, AbsoluteToDuration() returns a value of type Duration, which is actually an SInt32 or signed long:

typedef SInt32 Duration;

Durations use a smaller, signed type because they are intended to hold relative times. Nanoseconds, on the other hand, only make sense as positive values, and they can be very large, since computers can stay running for years at a time.

e.James
  • 116,942
  • 41
  • 177
  • 214
  • Thanks. Actually, the main reason I am interested in the docs is to find out how it differs from AbsoluteToDuration – Casebash Jan 28 '10 at 04:01
  • Is there any reason why I have to use SubAbsoluteFromAbsolute rather than plain subtraction? – Casebash Jan 28 '10 at 04:07
  • 2
    You bet! `SubAbsoluteFromAbsolute()` returns 0 instead of a negative number if the time has passed. http://lists.apple.com/archives/darwin-dev/2008/Mar/msg00130.html – e.James Jan 28 '10 at 04:38
  • I have responded to your first comment via an update to my answer. I hope it makes sense. – e.James Jan 28 '10 at 04:51
2

According to https://developer.apple.com/library/prerelease/mac/releasenotes/General/APIDiffsMacOSX10_9/Kernel.html,

SubAbsoluteFromAbsolute(), along with apparently all the other *Absolute* functions, have been removed from Mavericks. I have confirmed this.

These functions are no longer necessary since at least in Mavericks and Mountain Lion (the two I tested), mach_absolute_time() already returns time in nanoseconds, and not in absolute form (which used to be the number of bus cycles), making a conversion no longer necessary. Thus, the conversion shown in clock_gettime alternative in Mac OS X and similar code presented in several places on the web, is no longer necessary. This can be confirmed on your system by checking that both the numerator and denominator returned by mach_timebase_info() are 1.

Here is my test code with lots of output to check if you need to do the conversion on your system (I have to perform a check since my code might run on older Macs, although I do the check at program initiation and set a function pointer to call a different routine):

#include <CoreServices/CoreServices.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <time.h>
#include <iostream>

using namespace std;

int main()
{
   uint64_t now, then;
   uint64_t abs, nano;
   mach_timebase_info_data_t timebase_info = {0,0};

   then = mach_absolute_time();
   sleep(1);
   now = mach_absolute_time();
   abs = now - then;

   mach_timebase_info(&timebase_info);

   cout << "numerator " << timebase_info.numer << " denominator "
        << timebase_info.denom << endl;

   if ((timebase_info.numer != 1) || (timebase_info.denom != 1))
   {
      nano = (abs * timebase_info.numer) / timebase_info.denom;
      cout << "Have a real conversion value" << endl;
   }
   else
   {
      nano = abs;
      cout << "Both numerator and denominator are 1" << endl;
   }
   cout << "milliseconds = " << nano/1000000LL << endl;
}
Community
  • 1
  • 1
Jon Spencer
  • 569
  • 5
  • 8