# Is it Time bug?

**URL:** <https://rubytalk.org/t/is-it-time-bug/24447>\
**Category:** ruby-talk\
**Created:** [30 January 2006 06:47 UTC](https://rubytalk.org/t/is-it-time-bug/24447 "2006-01-30T06:47:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valerij\_KIR](https://avatars.discourse-cdn.com/v4/letter/v/d26b3c/32.png) [@Valerij\_KIR](https://rubytalk.org/u/Valerij_KIR)\
**Post date:** [30 January 2006 06:47 UTC](https://rubytalk.org/t/is-it-time-bug/24447/1 "2006-01-30T06:47:59Z")

</div>

Hi all! I'm writing program some days ago which calculates every  
Thursday day in interval given by 2 Time values and see interesting  
result described later. Is it my or ruby bug?

Result

> **···**
>
> -------
> 
> Thu Sep 01 00:00:00 MSD 2005  
> Thu Sep 08 00:00:00 MSD 2005  
> Thu Sep 15 00:00:00 MSD 2005  
> Thu Sep 22 00:00:00 MSD 2005  
> Thu Sep 29 00:00:00 MSD 2005  
> Thu Oct 06 00:00:00 MSD 2005  
> Thu Oct 13 00:00:00 MSD 2005  
> Thu Oct 20 00:00:00 MSD 2005  
> Thu Oct 27 00:00:00 MSD 2005  
> Wed Nov 02 23:00:00 MSK 2005  
> Wed Nov 09 23:00:00 MSK 2005  
> Wed Nov 16 23:00:00 MSK 2005  
> Wed Nov 23 23:00:00 MSK 2005  
> Wed Nov 30 23:00:00 MSK 2005  
> Wed Dec 07 23:00:00 MSK 2005
> 
> Code  
> -----
> 
> t1 = Time.mktime(2005, 9, 1)  
> t2 = Time.mktime(2005, 12, 11)  
> while t1.to\_i \< t2.to\_i  
> &nbsp;&nbsp;p t1  
> &nbsp;&nbsp;t1 += 3600\*24\*7  
> end
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [30 January 2006 06:53 UTC](https://rubytalk.org/t/is-it-time-bug/24447/2 "2006-01-30T06:53:41Z")

</div>

Valerij KIR wrote:

> Hi all! I'm writing program some days ago which calculates every Thursday day in interval given by 2 Time values and see interesting result described later. Is it my or ruby bug?
> 
> Result  
> -------
> 
> Thu Sep 01 00:00:00 MSD 2005  
> Thu Sep 08 00:00:00 MSD 2005  
> Thu Sep 15 00:00:00 MSD 2005  
> Thu Sep 22 00:00:00 MSD 2005  
> Thu Sep 29 00:00:00 MSD 2005  
> Thu Oct 06 00:00:00 MSD 2005  
> Thu Oct 13 00:00:00 MSD 2005  
> Thu Oct 20 00:00:00 MSD 2005  
> Thu Oct 27 00:00:00 MSD 2005  
> Wed Nov 02 23:00:00 MSK 2005  
> Wed Nov 09 23:00:00 MSK 2005  
> Wed Nov 16 23:00:00 MSK 2005  
> Wed Nov 23 23:00:00 MSK 2005  
> Wed Nov 30 23:00:00 MSK 2005  
> Wed Dec 07 23:00:00 MSK 2005
> 
> Code  
> -----
> 
> t1 = Time.mktime(2005, 9, 1)  
> t2 = Time.mktime(2005, 12, 11)  
> while t1.to\_i \< t2.to\_i  
> &nbsp;&nbsp;p t1  
> &nbsp;&nbsp;t1 += 3600\*24\*7  
> end

This is what I get. Looks like it is the effect of the PDT-\>PST time change (or in your case MSD-\>MSK).

Thu Sep 01 00:00:00 Pacific Daylight Time 2005  
Thu Sep 08 00:00:00 Pacific Daylight Time 2005  
Thu Sep 15 00:00:00 Pacific Daylight Time 2005  
Thu Sep 22 00:00:00 Pacific Daylight Time 2005  
Thu Sep 29 00:00:00 Pacific Daylight Time 2005  
Thu Oct 06 00:00:00 Pacific Daylight Time 2005  
Thu Oct 13 00:00:00 Pacific Daylight Time 2005  
Thu Oct 20 00:00:00 Pacific Daylight Time 2005  
Thu Oct 27 00:00:00 Pacific Daylight Time 2005  
Wed Nov 02 23:00:00 Pacific Standard Time 2005  
Wed Nov 09 23:00:00 Pacific Standard Time 2005  
Wed Nov 16 23:00:00 Pacific Standard Time 2005  
Wed Nov 23 23:00:00 Pacific Standard Time 2005  
Wed Nov 30 23:00:00 Pacific Standard Time 2005  
Wed Dec 07 23:00:00 Pacific Standard Time 2005

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Pat\_Maddox1](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@Pat\_Maddox1](https://rubytalk.org/u/Pat_Maddox1)\
**Post date:** [30 January 2006 07:10 UTC](https://rubytalk.org/t/is-it-time-bug/24447/3 "2006-01-30T07:10:43Z")

</div>

First of all for doing stuff like this I highly recommend checking out  
Runt [1], it's a temporal expression library. Think regular  
expressions for time.

Anyway I think it has to do with the fact that that the Date lib  
accounts for leap years, but your code doesn't. To explain it a bit..

There are 365.25 days in a year, not 365. This means that there are  
31557600 seconds in a year, rather than the 31536000 that your code  
would generate. That leaves you with 21600 unnoticed seconds. 365 /  
21600 = 0.0168981481, which is how many seconds are lost in each day.  
There are 63 days between your start date of Sep 01 and the "weird"  
date of Nov 02. 63 \* 0.0168981481 = 1.03, so there's one lost second,  
causing the day to not roll over.

Pretty fun, huh? 🙂

Now that I've taken the time to identify the problem, I honestly don't  
feel like solving it..but hopefully you can get started with this new  
found info 🙂 Again, I'd recommend checking out runt.

Pat

[1] [http://runt.rubyforge.org/](http://runt.rubyforge.org/)

> **···**
>
> On 1/29/06, Valerij KIR \<rootkvi@yandex.ru\> wrote:
> 
> > Hi all! I'm writing program some days ago which calculates every  
> > Thursday day in interval given by 2 Time values and see interesting  
> > result described later. Is it my or ruby bug?
> > 
> > Result  
> > -------
> > 
> > Thu Sep 01 00:00:00 MSD 2005  
> > Thu Sep 08 00:00:00 MSD 2005  
> > Thu Sep 15 00:00:00 MSD 2005  
> > Thu Sep 22 00:00:00 MSD 2005  
> > Thu Sep 29 00:00:00 MSD 2005  
> > Thu Oct 06 00:00:00 MSD 2005  
> > Thu Oct 13 00:00:00 MSD 2005  
> > Thu Oct 20 00:00:00 MSD 2005  
> > Thu Oct 27 00:00:00 MSD 2005  
> > Wed Nov 02 23:00:00 MSK 2005  
> > Wed Nov 09 23:00:00 MSK 2005  
> > Wed Nov 16 23:00:00 MSK 2005  
> > Wed Nov 23 23:00:00 MSK 2005  
> > Wed Nov 30 23:00:00 MSK 2005  
> > Wed Dec 07 23:00:00 MSK 2005
> > 
> > Code  
> > -----
> > 
> > t1 = Time.mktime(2005, 9, 1)  
> > t2 = Time.mktime(2005, 12, 11)  
> > while t1.to\_i \< t2.to\_i  
> > &nbsp;&nbsp;p t1  
> > &nbsp;&nbsp;t1 += 3600\*24\*7  
> > end
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Pat\_Maddox1](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@Pat\_Maddox1](https://rubytalk.org/u/Pat_Maddox1)\
**Post date:** [30 January 2006 07:11 UTC](https://rubytalk.org/t/is-it-time-bug/24447/4 "2006-01-30T07:11:49Z")

</div>

Damn, you win. Thought I had found an interesting "gotcha" 🙂

> **···**
>
> On 1/29/06, Joel VanderWerf \<vjoel@path.berkeley.edu\> wrote:
> 
> > Valerij KIR wrote:  
> > \> Hi all! I'm writing program some days ago which calculates every  
> > \> Thursday day in interval given by 2 Time values and see interesting  
> > \> result described later. Is it my or ruby bug?  
> > \>  
> > \> Result  
> > \> -------  
> > \>  
> > \> Thu Sep 01 00:00:00 MSD 2005  
> > \> Thu Sep 08 00:00:00 MSD 2005  
> > \> Thu Sep 15 00:00:00 MSD 2005  
> > \> Thu Sep 22 00:00:00 MSD 2005  
> > \> Thu Sep 29 00:00:00 MSD 2005  
> > \> Thu Oct 06 00:00:00 MSD 2005  
> > \> Thu Oct 13 00:00:00 MSD 2005  
> > \> Thu Oct 20 00:00:00 MSD 2005  
> > \> Thu Oct 27 00:00:00 MSD 2005  
> > \> Wed Nov 02 23:00:00 MSK 2005  
> > \> Wed Nov 09 23:00:00 MSK 2005  
> > \> Wed Nov 16 23:00:00 MSK 2005  
> > \> Wed Nov 23 23:00:00 MSK 2005  
> > \> Wed Nov 30 23:00:00 MSK 2005  
> > \> Wed Dec 07 23:00:00 MSK 2005  
> > \>  
> > \> Code  
> > \> -----  
> > \>  
> > \> t1 = Time.mktime(2005, 9, 1)  
> > \> t2 = Time.mktime(2005, 12, 11)  
> > \> while t1.to\_i \< t2.to\_i  
> > \> p t1  
> > \> t1 += 3600\*24\*7  
> > \> end  
> > \>
> > 
> > This is what I get. Looks like it is the effect of the PDT-\>PST time  
> > change (or in your case MSD-\>MSK).
> > 
> > Thu Sep 01 00:00:00 Pacific Daylight Time 2005  
> > Thu Sep 08 00:00:00 Pacific Daylight Time 2005  
> > Thu Sep 15 00:00:00 Pacific Daylight Time 2005  
> > Thu Sep 22 00:00:00 Pacific Daylight Time 2005  
> > Thu Sep 29 00:00:00 Pacific Daylight Time 2005  
> > Thu Oct 06 00:00:00 Pacific Daylight Time 2005  
> > Thu Oct 13 00:00:00 Pacific Daylight Time 2005  
> > Thu Oct 20 00:00:00 Pacific Daylight Time 2005  
> > Thu Oct 27 00:00:00 Pacific Daylight Time 2005  
> > Wed Nov 02 23:00:00 Pacific Standard Time 2005  
> > Wed Nov 09 23:00:00 Pacific Standard Time 2005  
> > Wed Nov 16 23:00:00 Pacific Standard Time 2005  
> > Wed Nov 23 23:00:00 Pacific Standard Time 2005  
> > Wed Nov 30 23:00:00 Pacific Standard Time 2005  
> > Wed Dec 07 23:00:00 Pacific Standard Time 2005
> > 
> > --  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Lloyd\_Zusman](https://avatars.discourse-cdn.com/v4/letter/l/8baadc/32.png) [@Lloyd\_Zusman](https://rubytalk.org/u/Lloyd_Zusman)\
**Post date:** [30 January 2006 10:03 UTC](https://rubytalk.org/t/is-it-time-bug/24447/5 "2006-01-30T10:03:41Z")

</div>

Pat Maddox \<pergesu@gmail.com\> writes:

> First of all for doing stuff like this I highly recommend checking out  
> Runt [1], it's a temporal expression library. Think regular  
> expressions for time.
> 
> Anyway I think it has to do with the fact that that the Date lib  
> accounts for leap years, but your code doesn't. To explain it a bit..
> 
> There are 365.25 days in a year, not 365. This means that there are  
> 31557600 seconds in a year, rather than the 31536000 that your code  
> would generate. That leaves you with 21600 unnoticed seconds. 365 /  
> 21600 = 0.0168981481, which is how many seconds are lost in each day.  
> There are 63 days between your start date of Sep 01 and the "weird"  
> date of Nov 02. 63 \* 0.0168981481 = 1.03, so there's one lost second,  
> causing the day to not roll over.
> 
> Pretty fun, huh? 🙂

I don't believe that this is the cause of the problem here. Changing  
the dates from 5/1/2005 to 7/11/2005 (just shifting everything back 4  
months) does not cause the same effect. This is a daylight savings time  
issue, not a seconds-per-year issue. In 2005, daylight savings time  
went into effect on April 3, 2005, and terminated on 30 October, 2005  
... at least in the USA.

&nbsp;&nbsp;t1 = Time.mktime(2005, 5, 1)  
&nbsp;&nbsp;t2 = Time.mktime(2005, 7, 11)  
&nbsp;&nbsp;while t1.to\_i \< t2.to\_i  
&nbsp;&nbsp;&nbsp;&nbsp;p t1  
&nbsp;&nbsp;&nbsp;&nbsp;t1 += 3600\*24\*7  
&nbsp;&nbsp;end

&nbsp;&nbsp;Sun May 01 00:00:00 EDT 2005  
&nbsp;&nbsp;Sun May 08 00:00:00 EDT 2005  
&nbsp;&nbsp;Sun May 15 00:00:00 EDT 2005  
&nbsp;&nbsp;Sun May 22 00:00:00 EDT 2005  
&nbsp;&nbsp;Sun May 29 00:00:00 EDT 2005  
&nbsp;&nbsp;Sun Jun 05 00:00:00 EDT 2005  
&nbsp;&nbsp;Sun Jun 12 00:00:00 EDT 2005  
&nbsp;&nbsp;Sun Jun 19 00:00:00 EDT 2005  
&nbsp;&nbsp;Sun Jun 26 00:00:00 EDT 2005  
&nbsp;&nbsp;Sun Jul 03 00:00:00 EDT 2005  
&nbsp;&nbsp;Sun Jul 10 00:00:00 EDT 2005

Every day has exactly 86400 seconds (except every few years when a  
leap-second is added/subtracted). The seconds-per-year issue is covered  
by leap years.

> **···**
>
> --  
> Lloyd Zusman  
> ljz@asfast.com  
> God bless you.

---

<div class="post-metadata">

**Author:** ![Lloyd\_Zusman](https://avatars.discourse-cdn.com/v4/letter/l/8baadc/32.png) [@Lloyd\_Zusman](https://rubytalk.org/u/Lloyd_Zusman)\
**Post date:** [30 January 2006 10:18 UTC](https://rubytalk.org/t/is-it-time-bug/24447/6 "2006-01-30T10:18:45Z")

</div>

Lloyd Zusman \<ljz@asfast.com\> writes:

> Pat Maddox \<pergesu@gmail.com\> writes:
> 
> > First of all for doing stuff like this I highly recommend checking out  
> > Runt [1], it's a temporal expression library. Think regular  
> > expressions for time.
> > 
> > Anyway I think it has to do with the fact that that the Date lib  
> > accounts for leap years, but your code doesn't. To explain it a bit..
> > 
> > There are 365.25 days in a year, not 365. This means that there are  
> > 31557600 seconds in a year, rather than the 31536000 that your code  
> > would generate. That leaves you with 21600 unnoticed seconds. 365 /  
> > 21600 = 0.0168981481, which is how many seconds are lost in each day.  
> > There are 63 days between your start date of Sep 01 and the "weird"  
> > date of Nov 02. 63 \* 0.0168981481 = 1.03, so there's one lost second,  
> > causing the day to not roll over.
> > 
> > Pretty fun, huh? 🙂
> 
> [...]
> 
> Every day has exactly 86400 seconds (except every few years when a  
> leap-second is added/subtracted). The seconds-per-year issue is covered  
> by leap years.

... except on the daylight savings time transitions, of course. In that  
year in the USA, April 3 had 82,800 seconds, and October 30 had 90,000  
seconds, which accounts for the discrepancy in the original date range.

Also, the seconds-lost-per-day calculation above isn't correct. You  
need to divide 21600 by 365, not the other way around. This yields a  
bit less than one minute per day.

> **···**
>
> --  
> Lloyd Zusman  
> ljz@asfast.com  
> God bless you.

---

<div class="post-metadata">

**Author:** ![Pat\_Maddox1](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@Pat\_Maddox1](https://rubytalk.org/u/Pat_Maddox1)\
**Post date:** [30 January 2006 10:26 UTC](https://rubytalk.org/t/is-it-time-bug/24447/7 "2006-01-30T10:26:02Z")

</div>

Yeah, I realized that. To quote my reply to Joel, who uncovered the real issue:  
"Damn, you win. Thought I had found an interesting 'gotcha' :)"

> **···**
>
> On 1/30/06, Lloyd Zusman \<ljz@asfast.com\> wrote:
> 
> > Pat Maddox \<pergesu@gmail.com\> writes:
> > 
> > \> First of all for doing stuff like this I highly recommend checking out  
> > \> Runt [1], it's a temporal expression library. Think regular  
> > \> expressions for time.  
> > \>  
> > \> Anyway I think it has to do with the fact that that the Date lib  
> > \> accounts for leap years, but your code doesn't. To explain it a bit..  
> > \>  
> > \> There are 365.25 days in a year, not 365. This means that there are  
> > \> 31557600 seconds in a year, rather than the 31536000 that your code  
> > \> would generate. That leaves you with 21600 unnoticed seconds. 365 /  
> > \> 21600 = 0.0168981481, which is how many seconds are lost in each day.  
> > \> There are 63 days between your start date of Sep 01 and the "weird"  
> > \> date of Nov 02. 63 \* 0.0168981481 = 1.03, so there's one lost second,  
> > \> causing the day to not roll over.  
> > \>  
> > \> Pretty fun, huh? 🙂
> > 
> > I don't believe that this is the cause of the problem here. Changing  
> > the dates from 5/1/2005 to 7/11/2005 (just shifting everything back 4  
> > months) does not cause the same effect. This is a daylight savings time  
> > issue, not a seconds-per-year issue.
