Skip to content

Commit c3be26c

Browse files
committed
Fix Time.new with String argument and handle nanoseconds correctly
1 parent d1673f7 commit c3be26c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

core/time/new_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,36 @@ def obj.to_int; 3; end
524524
Time.new("2021-12-25 00:00:00.123456789876 +09:00", precision: 3r).subsec.should == 0.123r
525525
end
526526

527+
it "returns Time with correct subseconds when given seconds fraction is shorted than 6 digits" do
528+
Time.new("2020-12-25T00:56:17.123 +09:00").nsec.should == 123000000
529+
Time.new("2020-12-25T00:56:17.123 +09:00").usec.should == 123000
530+
Time.new("2020-12-25T00:56:17.123 +09:00").subsec.should == 0.123
531+
end
532+
533+
it "returns Time with correct subseconds when given seconds fraction is milliseconds" do
534+
Time.new("2020-12-25T00:56:17.123456 +09:00").nsec.should == 123456000
535+
Time.new("2020-12-25T00:56:17.123456 +09:00").usec.should == 123456
536+
Time.new("2020-12-25T00:56:17.123456 +09:00").subsec.should == 0.123456
537+
end
538+
539+
it "returns Time with correct subseconds when given seconds fraction is longer that 6 digits but shorted than 9 digits" do
540+
Time.new("2020-12-25T00:56:17.12345678 +09:00").nsec.should == 123456780
541+
Time.new("2020-12-25T00:56:17.12345678 +09:00").usec.should == 123456
542+
Time.new("2020-12-25T00:56:17.12345678 +09:00").subsec.should == 0.12345678
543+
end
544+
545+
it "returns Time with correct subseconds when given seconds fraction is nanoseconds" do
546+
Time.new("2020-12-25T00:56:17.123456789 +09:00").nsec.should == 123456789
547+
Time.new("2020-12-25T00:56:17.123456789 +09:00").usec.should == 123456
548+
Time.new("2020-12-25T00:56:17.123456789 +09:00").subsec.should == 0.123456789
549+
end
550+
551+
it "returns Time with correct subseconds when given seconds fraction is longer than 9 digits" do
552+
Time.new("2020-12-25T00:56:17.123456789876 +09:00").nsec.should == 123456789
553+
Time.new("2020-12-25T00:56:17.123456789876 +09:00").usec.should == 123456
554+
Time.new("2020-12-25T00:56:17.123456789876 +09:00").subsec.should == 0.123456789
555+
end
556+
527557
ruby_version_is ""..."3.3" do
528558
it "raise TypeError is can't convert precision keyword argument into Integer" do
529559
-> {

0 commit comments

Comments
 (0)