Skip to content

Commit

Permalink
Add tests for daylight savings time switches
Browse files Browse the repository at this point in the history
  • Loading branch information
wishdev committed Jan 10, 2025
1 parent c6c64b0 commit 3ac8b17
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 additions & 0 deletions test/logger/test_logperiod.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,72 @@ def test_next_rotate_time
assert_raise(ArgumentError) { Logger::Period.next_rotate_time(time, "invalid") }
end

def test_next_rotate_time_dst_begin
tz = ENV['TZ']
ENV['TZ'] = 'America/New_York' # 1 hour shift
time = Time.new("2025-03-09 00:52:02")

assert_next_rotate_time_words(time, "2025-03-10 00:00:00", ["daily", :daily])
assert_next_rotate_time_words(time, "2025-03-16 00:00:00", ["weekly", :weekly])
assert_next_rotate_time_words(time, "2025-04-01 00:00:00", ["monthly", :monthly])

ENV['TZ'] = 'Antarctica/Troll' # 2 hour shift
time = Time.new("2025-03-30 00:52:02")

assert_next_rotate_time_words(time, "2025-03-31 00:00:00", ["daily", :daily])
assert_next_rotate_time_words(time, "2025-04-06 00:00:00", ["weekly", :weekly])
assert_next_rotate_time_words(time, "2025-04-01 00:00:00", ["monthly", :monthly])

ENV['TZ'] = 'Australia/Lord_Howe' # 30 minute shift
time = Time.new("2025-04-06 00:52:02")

assert_next_rotate_time_words(time, "2025-04-07 00:00:00", ["daily", :daily])
assert_next_rotate_time_words(time, "2025-04-13 00:00:00", ["weekly", :weekly])
assert_next_rotate_time_words(time, "2025-05-01 00:00:00", ["monthly", :monthly])

ENV['TZ'] = 'Asia/Gaza' # 1 hour shift on Saturday
time = Time.new("2025-04-12 00:52:02")

assert_next_rotate_time_words(time, "2025-04-13 00:00:00", ["daily", :daily])
assert_next_rotate_time_words(time, "2025-04-13 00:00:00", ["weekly", :weekly])
assert_next_rotate_time_words(time, "2025-05-01 00:00:00", ["monthly", :monthly])

ENV['TZ'] = tz
end

def test_next_rotate_time_dst_end
tz = ENV['TZ']
ENV['TZ'] = 'America/New_York' # 1 hour shift
time = Time.parse("2025-11-02 13:52:02")

assert_next_rotate_time_words(time, "2025-11-03 00:00:00", ["daily", :daily])
assert_next_rotate_time_words(time, "2025-11-09 00:00:00", ["weekly", :weekly])
assert_next_rotate_time_words(time, "2025-12-01 00:00:00", ["monthly", :monthly])

ENV['TZ'] = 'Antarctica/Troll' # 2 hour shift
time = Time.parse("2025-10-26 13:52:02")

assert_next_rotate_time_words(time, "2025-10-27 00:00:00", ["daily", :daily])
assert_next_rotate_time_words(time, "2025-11-02 00:00:00", ["weekly", :weekly])
assert_next_rotate_time_words(time, "2025-11-01 00:00:00", ["monthly", :monthly])

ENV['TZ'] = 'Australia/Lord_Howe' # 30 minute shift
time = Time.parse("2025-10-05 13:52:02")

assert_next_rotate_time_words(time, "2025-10-06 00:00:00", ["daily", :daily])
assert_next_rotate_time_words(time, "2025-10-12 00:00:00", ["weekly", :weekly])
assert_next_rotate_time_words(time, "2025-11-01 00:00:00", ["monthly", :monthly])

ENV['TZ'] = 'Asia/Gaza' # 1 hour shift on Saturday
time = Time.new("2025-10-25 13:52:02")

assert_next_rotate_time_words(time, "2025-10-26 00:00:00", ["daily", :daily])
assert_next_rotate_time_words(time, "2025-10-26 00:00:00", ["weekly", :weekly])
assert_next_rotate_time_words(time, "2025-11-01 00:00:00", ["monthly", :monthly])

ENV['TZ'] = tz
end

def test_next_rotate_time_extreme_cases
# First day of Month and Saturday
time = Time.parse("2018-07-01 00:00:00")
Expand All @@ -35,6 +101,72 @@ def test_previous_period_end
assert_raise(ArgumentError) { Logger::Period.previous_period_end(time, "invalid") }
end

def test_previous_period_end_dst_begin
tz = ENV['TZ']
ENV['TZ'] = 'America/New_York' # 1 hour shift
time = Time.new("2025-03-09 00:52:02")

assert_previous_period_end_words(time, "2025-03-08 23:59:59", ["daily", :daily])
assert_previous_period_end_words(time, "2025-03-08 23:59:59", ["weekly", :weekly])
assert_previous_period_end_words(time, "2025-02-28 23:59:59", ["monthly", :monthly])

ENV['TZ'] = 'Antarctica/Troll' # 2 hour shift
time = Time.new("2025-03-30 00:52:02")

assert_previous_period_end_words(time, "2025-03-29 23:59:59", ["daily", :daily])
assert_previous_period_end_words(time, "2025-03-29 23:59:59", ["weekly", :weekly])
assert_previous_period_end_words(time, "2025-02-28 23:59:59", ["monthly", :monthly])

ENV['TZ'] = 'Australia/Lord_Howe' # 30 minute shift
time = Time.new("2025-04-06 00:52:02")

assert_previous_period_end_words(time, "2025-04-05 23:59:59", ["daily", :daily])
assert_previous_period_end_words(time, "2025-04-05 23:59:59", ["weekly", :weekly])
assert_previous_period_end_words(time, "2025-03-31 23:59:59", ["monthly", :monthly])

ENV['TZ'] = 'Asia/Gaza' # 1 hour shift on Saturday
time = Time.new("2025-04-12 00:52:02")

assert_previous_period_end_words(time, "2025-04-11 23:59:59", ["daily", :daily])
assert_previous_period_end_words(time, "2025-04-05 23:59:59", ["weekly", :weekly])
assert_previous_period_end_words(time, "2025-03-31 23:59:59", ["monthly", :monthly])

ENV['TZ'] = tz
end

def test_previous_period_end_dst_end
tz = ENV['TZ']
ENV['TZ'] = 'America/New_York' # 1 hour shift
time = Time.parse("2025-11-02 13:52:02")

assert_previous_period_end_words(time, "2025-11-01 23:59:59", ["daily", :daily])
assert_previous_period_end_words(time, "2025-11-01 23:59:59", ["weekly", :weekly])
assert_previous_period_end_words(time, "2025-10-31 23:59:59", ["monthly", :monthly])

ENV['TZ'] = 'Antarctica/Troll' # 2 hour shift
time = Time.parse("2025-10-26 13:52:02")

assert_previous_period_end_words(time, "2025-10-25 23:59:59", ["daily", :daily])
assert_previous_period_end_words(time, "2025-10-25 23:59:59", ["weekly", :weekly])
assert_previous_period_end_words(time, "2025-09-30 23:59:59", ["monthly", :monthly])

ENV['TZ'] = 'Australia/Lord_Howe' # 30 minute shift
time = Time.parse("2025-10-05 13:52:02")

assert_previous_period_end_words(time, "2025-10-04 23:59:59", ["daily", :daily])
assert_previous_period_end_words(time, "2025-10-04 23:59:59", ["weekly", :weekly])
assert_previous_period_end_words(time, "2025-09-30 23:59:59", ["monthly", :monthly])


ENV['TZ'] = 'Asia/Gaza' # 1 hour shift on Saturday
time = Time.new("2025-10-25 13:52:02")

assert_previous_period_end_words(time, "2025-10-24 23:59:59", ["daily", :daily])
assert_previous_period_end_words(time, "2025-10-18 23:59:59", ["weekly", :weekly])
assert_previous_period_end_words(time, "2025-09-30 23:59:59", ["monthly", :monthly])
ENV['TZ'] = tz
end

def test_previous_period_end_extreme_cases
# First day of Month and Saturday
time = Time.parse("2018-07-01 00:00:00")
Expand Down

0 comments on commit 3ac8b17

Please sign in to comment.