Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8702203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:35:40+00:00 2026-06-13T02:35:40+00:00

I found recently this strange behaviour concerning instanciating a Time object (like for instance,

  • 0

I found recently this strange behaviour concerning instanciating a Time object (like for instance, using Time.now) and then using its value for arithmetic operations that in the end will return an Integer. My use case is the following:

I instantiate a Time:

time = 3.days.from_now

I then use it to know the number of seconds from now til that time:

now = Time.now
diff = time - now #=> returns an Integer

Now I store that time in the DB (lets assume a is an ActiveRecord-like instance which will store time in a DATETIME column):

a.date = time
a.save

Now it comes to the trickiest part:

a.reload
time2 = a.date
diff2 = time2 - now #=> returns an Integer
diff == diff2

The last assertion will sometimes be false, others be true. Thing is, When one instantiates the Time object, its value used for the Time arithmetic operations contains precision up to nanoseconds and microseconds, which will be taken into account in the resulting value of a “-” operation. But as it is stored, the “microprecision” is left behind, and when the time stored in the db is loaded, it comes with no nanoseconds or microseconds. Therefore, it is inherently different from the time instance I previously assigned to that object’s attribute. And that messes my difference calculations.

So, the question would be: How can I instantiate from Time so that the instance will precision only up to seconds and will therefore be equivalent to a time fetched from db if that time instance would be eventually stored? I would prefer an ActiveSupport-independent solution, even though AS-suggestions are also welcome. The goal is: diff must be equal to diff2 ALL the time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:35:41+00:00Added an answer on June 13, 2026 at 2:35 am

    If you only need to truncate a Time to a second, you can use this, for example:

    t = Time.at(Time.now.to_i)
    

    instead of Time.now

    Edit: Just noticed Time#round method:

    t = Time.now.round
    

    should do the trick as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

recently I found a function like this in a generic JSR245 portlet class: public
I recently found this article: http://coderjournal.com/2010/10/timing-the-execution-time-of-your-mvc-actions/ Which shows a nice little attribute that you
I have recently found a strange behaviour inside of the ASP.NET DropDownList that I
I recently found this which is great as its the API but it doesn't
I recently found this post . It basically says that Eclipse has a modified
I've recently found this blog entry on a tool that writes XSS attacks directly
So, i recently found this term related somehow to neural networks, but I don't
I'm a PHP novice, and I recently found this database-free pagination script on a
I found this typo recently: if (name.find('/' != string::npos)) Obviously the dev meant to
I just recently found myself writing this line of code, which i did not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.