Home > Blockchain >  Ruby Subtracting of Times, Timezones, Dates, or DateTimes appears broken
Ruby Subtracting of Times, Timezones, Dates, or DateTimes appears broken

Time:07-14

a.time_at
 => Mon, 12 Jul 2021 13:40:35 UTC  00:00 
b.time_at
 => Mon, 12 Jul 2021 13:52:22 UTC  00:00 

b - a
 => 707.0

However it should be 11 minutes ( and some seconds )

a.time_at.class
 => ActiveSupport::TimeWithZone 
b.time_at.class
 => ActiveSupport::TimeWithZone 

DateTime.parse(b.time_at.to_s) - DateTime.parse(a.time_at.to_s)
 => (707/86400)

Any idea why this may be?

CodePudding user response:

Subtracting Time class objects in ruby returns a difference in seconds. 707 / 60 = 11.73, which is 11 minutes and 46.8 seconds. You can also read about this in the documentation.

  • Related