Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Timespan

Moderators: larsivi kris

Posted: 03/16/07 15:46:38

hi, how can i calculate the timespan between two time points?

This does't work:

void main(char[][] args)
{
	DateTime t1 = DateTime.now; 
	DateTime t2 = DateTime( 2007, 1, 2, 10, 0, 0, 0 );
	TimeSpan span = t1 - t2; //error! 
	Stdout.format("{0} Seconds\n", span.seconds());

Author Message

Posted: 03/16/07 16:09:51 -- Modified: 03/16/07 16:36:14 by
simas -- Modified 2 Times

I have found one way:

	DateTime t1 = DateTime.now; 
	DateTime t2 = DateTime( 2007, 1, 2, 10, 0, 0, 0 );
	TimeSpan span = t1.ticks - t2.ticks; 
	Stdout.format("{0} Seconds\n", span.seconds()); //this is wrong!
	//seconds() is only a part of the timestamp. I need someting like total_seconds()
	ulong total_seconds = span.ticks/TICKS_PER_SECOND;
	Stdout.format("{0} Seconds\n", total_seconds); //this is correct, but not nice!

Comments?

Posted: 05/02/07 02:59:55

There's a number of ways to do this. The simplest is using tango.util.time.Timer:

Timer timer;
timer.start;
Interval elapsed = timer.stop;

where 'elapsed' represents the elapsed period between start and stop. Another approach is to use tango.util.time.Utc:

Time start = Utc.time;
Time elapsed = Utc.time - start;

in this second case, 'elapsed' represents the period between two Time values, in 100ns ticks. Both of these approaches depend on the accuracy of the underlying OS/platform timer or clock API, but Timer is expected to be more accurate for short periods whereas Time is expected to be used for long durations instead.

Hope this helps?

Posted: 05/08/07 16:58:01

an FYI: Utc.time() has been replaced with Clock.now()