-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Treat endpoints consistently in calculations
Before, a lack of handling of time segment endpoints in calculations consistent with how they are handled elsewhere in the gem (that is, *including* the start time and *excluding* the end time) was causing weird behavior and inconsistencies across both `#for` and `#until` period generation methods, which filtered down to high-level calculations. When looking forward in time, if consumes, or lands at the end of a period, one extra "moment" period should be generated to compensate for the fact that the end time of a time segment is not considered part of that segment. Under similar circumstances when looking backward in time, since the start time of a time segment is considered inclusive to that time segment, calculations that end on or at that moment should not proceed to generate the next moment period. A nice side effect of making this behavior consistent at the lowest level of the engine is that we can remove special-case handling that was added to the high-level calculation object in support of zero-scalar calculations. We can simply use the period and timeline objects normally and get the correct behavior. Woohoo! Running the benchmark suite, we don't see a meaningful delta in performance characteristics with these changes. Before: ``` Biz.configure do |config| config.hours = { mon: {'00:00' => '24:00'}, tue: {'00:00' => '24:00'}, wed: {'00:00' => '24:00'}, thu: {'00:00' => '24:00'}, fri: {'00:00' => '24:00'} } end monday = Time.utc(2019, 1, 14) Biz.time(1, :days).before(monday) #=> 2019-01-11 00:00:00 UTC Biz.time(2, :days).before(monday) #=> 2019-01-10 00:00:00 UTC Biz.time(4, :days).before(monday) #=> 2019-01-08 00:00:00 UTC Biz.time(5, :days).before(monday) #=> 2019-01-05 00:00:00 UTC <- Expected 1/7/19 Biz.time(0, :days).before(monday) #=> 2019-01-12 00:00:00 UTC <- Expected 1/14/19 Biz.time(0, :days).before(monday + 1) #=> 2019-01-14 00:00:01 UTC <- Correct tuesday = Time.utc(2019, 1, 15) Biz.time(0, :days).before(tuesday) #=> 2019-01-15 00:00:00 UTC Biz.time(1, :days).before(tuesday) #=> 2019-01-12 00:00:00 UTC <- Expected 1/14/19 Biz.time(1, :day).before(tuesday + 1) #=> 2019-01-14 00:00:01 UTC <- Correct Biz.time(5, :days).before(tuesday) #=> 2019-01-08 00:00:00 UTC friday = Time.utc(2019, 1, 11) Biz.time(24, :hours).after(friday) #=> 2019-01-12 00:00:00 UTC <- 1/14/19 expected Biz.time(1, :day).after(friday) #=> 2019-01-14 00:00:00 UTC <- Correct ``` After: ``` >> Biz.configure do |config| ?> config.hours = { ?> mon: {'00:00' => '24:00'}, ?> tue: {'00:00' => '24:00'}, ?> wed: {'00:00' => '24:00'}, ?> thu: {'00:00' => '24:00'}, ?> fri: {'00:00' => '24:00'} >> } >> end >> >> monday = Time.utc(2019, 1, 14) => 2019-01-14 00:00:00 UTC >> >> Biz.time(1, :days).before(monday) #=> 2019-01-11 00:00:00 UTC => 2019-01-11 00:00:00 UTC >> Biz.time(2, :days).before(monday) #=> 2019-01-10 00:00:00 UTC => 2019-01-10 00:00:00 UTC >> Biz.time(4, :days).before(monday) #=> 2019-01-08 00:00:00 UTC => 2019-01-08 00:00:00 UTC >> Biz.time(5, :days).before(monday) #=> 2019-01-05 00:00:00 UTC <- Expected 1/7/19 => 2019-01-07 00:00:00 UTC >> Biz.time(0, :days).before(monday) #=> 2019-01-12 00:00:00 UTC <- Expected 1/14/19 => 2019-01-14 00:00:00 UTC >> Biz.time(0, :days).before(monday + 1) #=> 2019-01-14 00:00:01 UTC <- Correct => 2019-01-14 00:00:01 UTC >> >> tuesday = Time.utc(2019, 1, 15) => 2019-01-15 00:00:00 UTC >> >> Biz.time(0, :days).before(tuesday) #=> 2019-01-15 00:00:00 UTC => 2019-01-15 00:00:00 UTC >> Biz.time(1, :days).before(tuesday) #=> 2019-01-12 00:00:00 UTC <- Expected 1/14/19 => 2019-01-14 00:00:00 UTC >> Biz.time(1, :day).before(tuesday + 1) #=> 2019-01-14 00:00:01 UTC <- Correct => 2019-01-14 00:00:01 UTC >> Biz.time(5, :days).before(tuesday) #=> 2019-01-08 00:00:00 UTC => 2019-01-08 00:00:00 UTC >> friday = Time.utc(2019, 1, 11) => 2019-01-11 00:00:00 UTC >> Biz.time(24, :hours).after(friday) #=> 2019-01-12 00:00:00 UTC <- 1/14/19 expected => 2019-01-14 00:00:00 UTC >> Biz.time(1, :day).after(friday) #=> 2019-01-14 00:00:00 UTC <- Correct => 2019-01-14 00:00:00 UTC ``` Fixes #138.
- Loading branch information
1 parent
0affaca
commit 970ac46
Showing
13 changed files
with
124 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,10 +29,6 @@ def weeks | |
) | ||
end | ||
|
||
def relevant?(period) | ||
origin < period.end_time | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.