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

Time.change doesn't support changing nanoseconds #16392

Closed
lukeredpath opened this issue Aug 4, 2014 · 7 comments · Fixed by #16758
Closed

Time.change doesn't support changing nanoseconds #16392

lukeredpath opened this issue Aug 4, 2014 · 7 comments · Fixed by #16758
Assignees
Milestone

Comments

@lukeredpath
Copy link
Contributor

I previously encountered an issue in my tests where I would be comparing a Ruby date with a date returned by ActiveRecord and the comparison would fail because the date returned from the database would lose its nanosecond precision.

My workaround was to use Timecop and the Rails #change method to freeze to a given time with truncated nanoseconds, using this small monkey patch:

class Timecop
  def self.freeze_without_nanoseconds(time = Time.now, &block)
    freeze(time.change(nsec: 0), &block)
  end
end

This worked in Rails 3 and no longer works in Rails 4. On further investigation, it appears that Time#change doesn't actually support the nsec key (hence this issue) - my fix only worked by accident because Time#change truncated nanoseconds.

This was reported as #9591 and fixed in Rails 4, so Time#change now preserves nanoseconds but there is still no support for changing the nanoseconds.

@lukeredpath
Copy link
Contributor Author

Note: use of Time#change in the above monkey patch is just an implementation detail, I believe I originally implemented this differently before I discovered the #change method and updated my implementation to the above as it was cleaner. This is just an example that demonstrates a use case for being able to pass an nsec key.

@pixeltrix
Copy link
Contributor

Whilst we should probably add support for a :nsec key you can use a Rational with the :usec key to update the nanoseconds, e.g:

>> t = Time.now
=> 2014-08-04 21:31:42 +0100
>> t.nsec
=> 773083000
>> t.change(:usec => Rational(1,1000)).nsec
=> 1

@lukeredpath
Copy link
Contributor Author

That's a useful workaround, thanks.

@chancancode
Copy link
Member

Tangentially related to your original problem, time travel helpers now always truncate the usec for the same reason: 9f6e82e

@pixeltrix do you still plan to add this in 4.2? It is currently listed in the github milestone, so it would block 4.2.0.rc1.

@pixeltrix
Copy link
Contributor

@chancancode just back from holiday so I've got a few tickets to go through over the next few days

@chancancode
Copy link
Member

@pixeltrix no worries, but I just want to know if we want/need to hold the release for this 😄

@chancancode chancancode modified the milestones: 4.2.0, 5.0.0 Aug 31, 2014
@chancancode
Copy link
Member

@pixeltrix I removed the milestone since this doesn't seem super critical. If it ends up making it into 4.2, great, otherwise it's not the end of the world. Feel free to change it back if you feel otherwise :)

trungpham pushed a commit to trungpham/rails that referenced this issue Sep 18, 2014
@rafaelfranca rafaelfranca modified the milestones: 5.0.0 [temp], 5.0.0 Dec 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants