Created
November 3, 2016 12:44
-
-
Save maximveksler/5bf9f878da76a93fda2719576c4ba615 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module DurationCalc | |
def self.days_within(start, finish) | |
durations_within(start, finish, 1.day) | |
end | |
# Calculates months between 2 gives dates. If 2 dates are of the same month returns 1 | |
def self.months_within(start, finish) | |
durations_within(start, finish, 1.month) | |
end | |
private | |
def self.durations_within(start, finish, duration) | |
return durations_within(finish, start, duration) if start > finish | |
count = 1 | |
# Yes we really need to loop here, to account for both DST and for leap year edge cases. | |
while ((start += duration) <= finish) | |
raise "Endless loop safegaurd #{count} #{start} #{finish}" if count > 5_000_000 | |
count += 1 | |
end | |
count | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment