Skip to content

Commit

Permalink
Prevent day skipping on entering DST, gh-53
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Mar 23, 2021
1 parent a881ecb commit 364cc8f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
12 changes: 5 additions & 7 deletions lib/fugit/cron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ def to_i; @t.to_i; end
%w[ year month day wday hour min sec wday_in_month rweek rday ]
.collect(&:to_sym).each { |k| define_method(k) { @t.send(k) } }

def inc(i)
@t = @t + i
self
end
def inc(i); @t = @t + i; self; end
def dec(i); inc(-i); end

def inc_month
Expand All @@ -95,6 +92,7 @@ def inc_month

def inc_day
inc((24 - @t.hour) * 3600 - @t.min * 60 - @t.sec)
inc( - @t.hour * 3600) if @t.hour != 0 # compensate for entering DST
end
def inc_hour
inc((60 - @t.min) * 60 - @t.sec)
Expand All @@ -112,7 +110,7 @@ def inc_sec
end

def dec_month
dec((@t.day - 1) * 24 * 3600 + @t.hour * 3600 + @t.min * 60 + @t.sec + 1)
dec((@t.day - 1) * DAY_S + @t.hour * 3600 + @t.min * 60 + @t.sec + 1)
end

def dec_day
Expand Down Expand Up @@ -332,7 +330,7 @@ def brute_frequency(year=2017)
[ :seconds, 1, 60 ],
[ :minutes, 60, 60 ],
[ :hours, 3600, 24 ],
[ :days, 24 * 3600, 365 ] ].freeze
[ :days, DAY_S, 365 ] ].freeze

def rough_frequency

Expand Down Expand Up @@ -374,7 +372,7 @@ def initialize(deltas, span)

@delta_min = deltas.min; @delta_max = deltas.max
@occurrences = deltas.size
@span_years = span / (365 * 24 * 3600)
@span_years = span / YEAR_S
@yearly_occurrences = @occurrences.to_f / @span_years
end

Expand Down
8 changes: 4 additions & 4 deletions lib/fugit/duration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ def common_rewrite_dur(t)
end

KEYS = {
yea: { a: 'Y', r: 'y', i: 'Y', s: 365 * 24 * 3600, x: 0, l: 'year' },
mon: { a: 'M', r: 'M', i: 'M', s: 30 * 24 * 3600, x: 1, l: 'month' },
wee: { a: 'W', r: 'w', i: 'W', s: 7 * 24 * 3600, I: true, l: 'week' },
day: { a: 'D', r: 'd', i: 'D', s: 24 * 3600, I: true, l: 'day' },
yea: { a: 'Y', r: 'y', i: 'Y', s: YEAR_S, x: 0, l: 'year' },
mon: { a: 'M', r: 'M', i: 'M', s: 30 * DAY_S, x: 1, l: 'month' },
wee: { a: 'W', r: 'w', i: 'W', s: 7 * DAY_S, I: true, l: 'week' },
day: { a: 'D', r: 'd', i: 'D', s: DAY_S, I: true, l: 'day' },
hou: { a: 'h', r: 'h', i: 'H', s: 3600, I: true, l: 'hour' },
min: { a: 'm', r: 'm', i: 'M', s: 60, I: true, l: 'minute' },
sec: { a: 's', r: 's', i: 'S', s: 1, I: true, l: 'second' } }.freeze
Expand Down
3 changes: 3 additions & 0 deletions lib/fugit/misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

module Fugit

DAY_S = (24 * 3600).freeze
YEAR_S = (365 * DAY_S).freeze

class << self

def isostamp(show_date, show_time, show_usec, time)
Expand Down

0 comments on commit 364cc8f

Please sign in to comment.