Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time Comparison in D

Tags:

time

d

I'm trying to build a Pomodoro timer app in D. I used to ruby, and I would like to do basic time comparisons.

I tried using something like

auto startTime = Clock.currTime();

And then comparing by grabbing the current time and comparing:

do{

// bla bla stuff
  auto nowTime = Clock.currTime();
}while(nowTime <= (startTime + dur!"minute"(25));

However, missing method and type errors ensue. Any ideas?

like image 241
RedMage Avatar asked Dec 24 '11 02:12

RedMage


2 Answers

In addition to CyberShadow's answer which does indeed tell you how to fix your code, I would point out that this particular approach is not the best approach for a timer. Aside from the fact that there's a good chance that a condition variable would make more sense (depending on what you're really doing), Clock.currTime is the wrong function to be using.

Clock.currTime returns the time using the real time clock, whereas timing is going to generally be more accurate with a monotonic clock. With clocks other than a monotonic clock, the time can be affected by changes to the clock (e.g. the system clock gets adjusted by a few minutes by the NTP daemon). However, a monotonic clock always moves forward at the same rate, even if the system clock is adjusted. So, it's not very useful for getting the time, but it's perfect for timing stuff. For that, you'd want to do something more like this:

auto endTime = Clock.currSystemTick + to!TickDuration(dur!"minutes"(25));
do
{
    //bla bla stuff
} while(Clock.currSystemTick < endTime);

So, you end up dealing with core.time.TickDuration instead of std.datetime.SysTime. As long as you don't need the actual time of day and are just using this for timing purposes, then this approach is better.

like image 126
Jonathan M Davis Avatar answered Nov 19 '22 06:11

Jonathan M Davis


  1. You're missing a )
  2. Variables declared inside a while scope are not visible to the while condition - you need to move the nowTime declaration outside of the do ... while block.
  3. It should be dur!"minutes", not "minute".

With these fixes, the code compiles fine for me.

like image 25
Vladimir Panteleev Avatar answered Nov 19 '22 06:11

Vladimir Panteleev