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?
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.
)
while
scope are not visible to the while
condition - you need to move the nowTime
declaration outside of the do ... while
block.dur!"minutes"
, not "minute"
.With these fixes, the code compiles fine for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With