Skip to content

Time can be initialized with negative nanoseconds #5370

@til

Description

@til

Environment

jruby 9.2.0.0 (2.5.0) 2018-05-24 81156a8 OpenJDK 64-Bit Server VM 25.181-b13 on 1.8.0_181-b13 [linux-x86_64]
Linux 4.18.12-arch1-1-ARCH #1 SMP PREEMPT Thu Oct 4 01:01:27 UTC 2018 x86_64 GNU/Linux

Expected Behavior

Initializing Time with a negative value for seconds should raise an error.

On MRI it does:

irb(main):001:0> Time.new(2018, 1, 1, 10, 30, -0.1)
Traceback (most recent call last):
        4: from /home/tils/.rubies/ruby-2.5.1/bin/irb:11:in `<main>'
        3: from (irb):1
        2: from (irb):1:in `new'
        1: from (irb):1:in `initialize'
ArgumentError (argument out of range)

On JRuby it does not, when only given fraction of a second:

irb(main):003:0> Time.new(2018, 1, 1, 10, 30, -0.1)
=> 2018-01-01 10:29:59 +0100

When the number is small enough, it will have negative nsec:

irb(main):011:0> Time.new(2018, 1, 1, 10, 30, -0.0009).nsec
=> -900000

which becomes a problem in very specific serializations, e.g. with Sequel and postgres-jdbc it produces this invalid timestamp representation:

[2] pry(main)> DB.literal(Time.new(2018, 1, 1, 10, 1, -0.0009))
=> "'2018-01-01 10:01:00.-00900+0000'"

Actual Behavior

Behave like MRI and always raise an ArgumentError when sec < 0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions