Skip to content

Commit

Permalink
support for strptime second based formats
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuacronemeyer committed Feb 7, 2021
1 parent eb04805 commit ff1abf6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/timecop/time_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def strptime_with_mock_date(str = '-4712-01-01', fmt = '%F', start = Date::ITALY
else
Date.commercial(d[:cwyear], d[:cweek])
end
elsif d[:seconds]
Time.at(d[:seconds]).to_date
else
Date.new(year, mon)
end
Expand Down
8 changes: 8 additions & 0 deletions test/date_strptime_scenarios.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def test_date_strptime_with_iso_8601_week_date
assert_equal Date.strptime('1984-W09-1', '%G-W%V-%u'), Date.new(1984, 2, 27)
end

def test_date_strptime_with_seconds_since_epoch
assert_equal Date.strptime('446731200', '%s'), Date.new(1984, 2, 27)
end

def test_date_strptime_with_miliseconds_since_epoch
assert_equal Date.strptime('446731200000', '%Q'), Date.new(1984, 2, 27)
end

def test_date_strptime_with_invalid_date
assert_raises(ArgumentError) { Date.strptime('', '%Y-%m-%d') }
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def local_offset(time = Time.now)
Time.at(time.to_i).to_datetime.offset
end

TIMEZONES = ["Europe/Paris", "UTC", "America/Chicago"]
TIMEZONES = ["Pacific/Midway", "Europe/Paris", "UTC", "America/Chicago"]

def each_timezone
old_tz = ENV["TZ"]
Expand Down

0 comments on commit ff1abf6

Please sign in to comment.