Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are those two datetimes different?

Why isn't Time.current equal to its parsed equivalent?

current = Time.current
# Wed, 16 Sep 2015 17:10:56 CEST +02:00
parsed = Time.zone.parse('16 Sep 2015 17:10:56')
# Wed, 16 Sep 2015 17:10:56 CEST +02:00
current == parsed
# false <= What ?!
current.to_i == parsed.to_i
# true
Ticket.create(datetime: current)
# ...
Ticket.find_by_datetime(parsed)
# nil  <= Why ?!

I'm actually having trouble with this in a Ruby on Rails application where I try to find a record based on a datetime attribute that has been parsed, like shown on the last lines.

I really don't get it. Time zones are the same, times are the same down to seconds. What's happening here?

Moreover, how should I proceed to find a record based on a parsed datetime?

like image 569
Jeremy F. Avatar asked Sep 16 '15 15:09

Jeremy F.


People also ask

How do I find the difference between two Datetimes in Python?

Using Python datetime module: In order to find the difference between two dates we simply input the two dates with date type and subtract them, which in turn provides us the number of days between the two dates.

How do you find the difference between two timestamps in seconds in Python?

Time Difference between two timestamps in Python Next, use the fromtimestamp() method to convert both start and end timestamps to datetime objects. We convert these timestamps to datetime because we want to subtract one timestamp from another. Next, use the total_seconds() method to get the difference in seconds.

How can I find the difference between two timestamps in C#?

The difference between two dates can be calculated in C# by using the substraction operator - or the DateTime. Subtract() method. The following example demonstrates getting the time interval between two dates using the - operator.


1 Answers

They should not be the same:

current.to_f #=> 1442417032.6567826
parsed.to_f  #=> 1442417032.0

When parsing, you miss milliseconds.

like image 191
dimakura Avatar answered Sep 27 '22 22:09

dimakura