2

Does the Time#to_i method consider leap years and any other time-related special cases when it converts a given time to seconds?

Reference: http://www.ruby-doc.org/core-2.0.0/Time.html#method-i-to_i

MxLDevs
  • 19,048
  • 36
  • 123
  • 194
  • 2
    ctrl+f on windows/linux, cmd+f on a mac and search for the word leap. – Justin Wood Dec 16 '13 at 21:57
  • 1
    Not accounting for leap years would introduce huge errors, so that's kind of an obvious one. Do you consider leap seconds to be another "time-related special case?" What about [this bundle of joy?](http://stackoverflow.com/a/6841479/139010) – Matt Ball Dec 16 '13 at 22:00
  • @JustinWood ctrl-f on the documentation page? – MxLDevs Dec 16 '13 at 22:10
  • @MattBall I suppose leap years is the more important one, since timezone and location are not relevant for me. Not too sure about those leap seconds. – MxLDevs Dec 16 '13 at 22:10
  • yes, it is how to search in your browser. It accounts for leap years, even leap seconds. – Justin Wood Dec 16 '13 at 22:10
  • @JustinWood Thanks for the tip on that search functionality! I didn't make the connection that if they considered leap seconds, then they would have considered leap years. – MxLDevs Dec 16 '13 at 22:13

1 Answers1

0

Leap years are of course supported. About leap seconds, I have strong doubts. While officially recognizing second range from 0 to 60 (in #sec), it seems that ruby just delegates to underlying operating system (which are in most cases not aware of leap seconds). See this link Furthermore: #to_i refers to unix epoch and does not count leap seconds, otherwise you get heavy interoperability problems between different platforms. In general, the documentation of ruby is not very precise, unfortunately.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • It doesn't seem to account for leap seconds in calculations. For may case this is good. But I wondered if this is defined or an undefined implementation detail. `Time.parse("2017-01-02 00:00:00 UTC") - Time.parse("2016-12-26 00:00:00 UTC") => 604800.0` – akostadinov Apr 07 '23 at 14:38
  • 1
    Ok, from wikipedia i found the spec explaining that "seconds since the Epoch, each and every day shall be accounted for by exactly 86400 seconds", see https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_16 – akostadinov Apr 07 '23 at 15:53