time_difference
time_difference copied to clipboard
Inclusive dates
Can we use this gem when we want end date as inclusive? and does it handle the time zones and DST changes?? I want to use it as billing duration?
Hi @Zeeshan-Siddhu will you be able to share with me an example with the expected output?
You want to calculate the time difference in a single timezone, UTC for instance. Depending on your use case, a slight margin of error won't be a problem.
Hi @tmlee I'm currently working on the issues raised above too (along with more accurate depiction of months). There's two parts to this request, and I believe that they both stem from the use of Date as the originating part of the requests.
Inclusivity
Time Differences are often used to calculate the period of time that is for what I would term "volumetric" purposes, such as a Billing use case. This is distinct from a relative distance purpose. For example:
Start | Finish | TimeDiff returns | Inclusive would return |
---|---|---|---|
2018-01-01 | 2018-01-07 | 6 days | 7 days |
2018-01-01 | 2018-01-31 | 30 days | 31 days |
2018-01-01 | 2018-01-31 | 0.99 months | 1 month |
2018-01-01 | 2018-02-01 | 1.02 months | 1.0357 months |
To get around this you could pass to the query a start / finish combination that is not a date (which represents a discrete period of time with a rising edge, falling edge and duration) but rather an explicit point in time (a Time object).
Timezone support
When working with time ranges across DST the number of seconds in the range is dependent on the TZ.
# Returns the time in seconds
#
# @return
def time_in_seconds(time)
time.to_time.to_f
end
Has no support to specify the timezone. Further, calling #to_time on a time object passed in will result in it using the locally set timezone. E.g. my timezone is Australia/Sydney
.
2.5.1 :001 > require './lib/time_difference'
=> true
2.5.1 :002 > Time.parse('2018-01-01T00:00:00+01:00')
=> 2018-01-01 00:00:00 +0100
2.5.1 :003 > Time.parse('2018-01-01T00:00:00+01:00').to_time
=> 2018-01-01 10:00:00 +1100