cakephp / chronos

A standalone DateTime library originally based off of Carbon
http://book.cakephp.org/chronos
MIT License
1.35k stars 63 forks source link

Convenience Functions for choosing/setting precision #152

Closed ravage84 closed 4 years ago

ravage84 commented 7 years ago

After reading a recent article about time precision, I was wondering if it would make sense to include convenience functions for choosing/setting precision into Chronos?

I could imagine setting the precision of a date or datetime object to:

Creating two datetime objects, setting their precision and comparing them could become quite easy... Some of that might be possible already through one wary or another.

Alternatively, instead of functions, this could be implemented through different precision classes.

May be all this is just a brain fart. Let me know.

markstory commented 7 years ago

We have a number of comparison methods that would fall under 'solution 3' from that article. How do you see these precision methods being used?

ravage84 commented 7 years ago
// With methods
$now = Chronos::now();
$nowPreciseToHour = $now->setPrecisionToHour();
$someOtherDate = Chronos::create(2017, 12, 25, 4, 32, 58);
$otherDatePreciseToHour = $someOtherDate->setPrecisionToHour();

// With different types
$nowPreciseToHour = ChronosHour::now();
$otherDatePreciseToHour = ChronosHour:parse($someStringToParse);

Instead of working with one of the diffInXyz() methods, you can compare apple with apple.

ravage84 commented 6 years ago

Eric Evans proposes an interesting solution to problems like this in this talk "Eploring Time": Allen's interval algebra

But a solution like the one proposed would probably go way too far and would likely be implemented on a programming language level at best.