brick / date-time

Date and time library for PHP
MIT License
337 stars 30 forks source link

Add `DefaultClock::travelForwards(Duration)` #91

Closed francislavoie closed 7 months ago

francislavoie commented 9 months ago

Currently, I have to write code like this to travel forwards by some nanos (to mimic usleep):

DefaultClock::travel(Instant::now()->plus(Duration::ofNanos($delayNanos)));

It would be nicer if I could write:

DefaultClock::travelForwards(Duration::ofNanos($delayNanos));

Would this be an acceptable addition?

BenMorel commented 9 months ago

Hi @francislavoie,

Yes, I think this is a reasonable addition, together with its travelBackwards() counterpart. What about travelForward vs travelForwards though? As I understand this is an AE vs BE issue, what's your take on this?

francislavoie commented 9 months ago

👍 sounds good to me, you're right I think Forward is probably more correct.

Somewhat off topic, I was also missing a function on Instant that would return seconds with millis as a float; there's only seconds int or millis int but no seconds float which is typically nicer for presentation in a log message or whatever. I'm not sure what that method should be called though. Admittedly this can lose precision because of course floats aren't perfect but I think it's good enough for many situations. -- I should probably open a new issue for this 😅

BenMorel commented 9 months ago

Yes, please open a new issue to discuss this :wink:

francislavoie commented 9 months ago

Regarding that last bit about seconds float, I just found ->toDecimal() which does what I wanted. So scratch that :sweat_smile:

I opened #92 to solve this