Guest User

Untitled

a guest
Mar 21st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. # When adding or subtracting DateTime objects, the return type is a Rational expressing the fraction of a day's number of minutes (1440). So 0.5 would mean half a day, or 720 minutes, or 12 hours.
  2. # When adding or subtracting Time objects, the return type is Float expressing the difference in seconds.
  3. #Here's an example showing the difference between two times that are an hour apart:
  4.  
  5. MINUTES_IN_A_DAY = 1440
  6. ref1 = "2018-03-13T08:00:00+00:00"
  7. ref2 = "2018-03-13T09:00:00+00:00"
  8.  
  9. # -3600 = 3600 seconds ago, i.e. 1 hour ago
  10. Time.zone.iso8601(ref1) - Time.zone.iso8601(ref2)
  11. # => -3600.0
  12.  
  13. # You'd get the same result if you did 60.0 / 1440.0
  14. DateTime.iso8601(ref1) - DateTime.iso8601(ref2)
  15. # => (-1/24)
  16.  
  17. # Shown another way, convert to float and multiply by 1440 minutes to get
  18. # difference in minutes
  19. (DateTime.iso8601(ref1) - DateTime.iso8601(ref2)).to_f * MINUTES_IN_A_DAY
  20. # => -60.0
Add Comment
Please, Sign In to add comment