Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Time::Span initializer from big seconds and sleep with big seconds #7221

Merged
merged 2 commits into from
Dec 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions spec/std/time/span_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ describe Time::Span do
t1.to_s.should eq("1.01:00:00")
end

it "initializes with big seconds value" do
t = Time::Span.new 0, 0, 1231231231231
t.total_seconds.should eq(1231231231231)
end

it "days overflows" do
expect_overflow do
days = 106751991167301
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/event.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct Crystal::Event

def add(timeout : Time::Span)
add LibC::Timeval.new(
tv_sec: timeout.total_seconds.to_i,
tv_sec: LibC::TimeT.new(timeout.total_seconds),
tv_usec: timeout.nanoseconds / 1_000
)
end
Expand Down
6 changes: 3 additions & 3 deletions src/time/span.cr
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ struct Time::Span
private def self.compute_seconds(days, hours, minutes, seconds, raise_exception)
# there's no overflow checks for hours, minutes, ...
# so big hours/minutes values can overflow at some point and change expected values
hrssec = hours * 3600 # break point at (Int32::MAX - 596523)
minsec = minutes * 60
s = (hrssec + minsec + seconds).to_i64
hrssec = 3600_i64 * hours # break point at (Int32::MAX - 596523)
minsec = 60_i64 * minutes
s = hrssec + minsec + seconds

result = 0_i64

Expand Down