datetime review part 2

Michel Fortin michel.fortin at michelf.com
Fri Nov 26 04:38:47 PST 2010


On 2010-11-26 03:03:15 -0500, Jonathan M Davis <jmdavisProg at gmx.com> said:

> Most recent code: http://is.gd/hO85C
> 
> I added code for Mac OS X which should use its monotonic clock functions.
> Unfortunately, as I don't own a Mac, it's completed untested, but in theory,
> what's there should work, and someone with a Mac can fix it later if need be.

Just tested it. There's an error in the definition of 
mach_timebase_info (takes a mach_timebase_info_t* argument, but it 
should be mach_timebase_info_t, no pointer) and the 
mach_timebase_info_t alias is missing. Here's a fixed version of the 
definitions:

	extern(C)
	{
		struct mach_timebase_info_data_t
		{
		    uint numer;
		    uint denom;
		}
		alias mach_timebase_info_data_t* mach_timebase_info_t;

		kern_return_t mach_timebase_info(mach_timebase_info_t);

		ulong mach_absolute_time();
	}

I tested it with this small program:

	extern(C) void sleep(uint sec);

	void main()
	{
	    auto start = TickDuration.currSystemTick;
	    sleep(20);
	    auto end = TickDuration.currSystemTick;
	    writeln(to!string(end-start));
	}

And this is the result (more or less a few thousands on each run):

	TickDuration(20000046269)

So it seems all good.

One thing I wonder about... what is the expected behaviour if you put 
the computer to sleep in the middle of the above program? With 
mach_absolute_time, the clock stops counting while the computer is put 
to sleep. Does TickDuration.currSystemTick works like that on all 
systems? If there's a difference in behaviour perhaps it should be 
documented.


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list