Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is 1401-01-01 a Saturday in Ruby but a Thursday in MySQL?

In Ruby:

> require 'time'
=> true
> Date.new(1401, 1, 1).saturday?
=> true

With MySQL:

SELECT dayofweek('1401-01-01')

This returns 5 which is Thursday.

In the OSX Calendar, this is also a Thursday.

What is causing this discrepancy?

like image 707
Dean Avatar asked Nov 18 '25 13:11

Dean


1 Answers

I strongly suspect that one of those environments is taking into account the change from the Julian calendar to the Gregorian calendar (at various times since the 16th century depending on place).

Using my Noda Time library, we can see which is which:

using System;
using NodaTime;

class Test
{
    static void Main()
    {
        var julian = new LocalDate(1401, 1, 1, 
                                   CalendarSystem.GetJulianCalendar(4));
        var gregorian = new LocalDate(1401, 1, 1,
                                      CalendarSystem.GetGregorianCalendar(4));

        Console.WriteLine("Julian: {0}", julian.IsoDayOfWeek);
        Console.WriteLine("Gregorian: {1}", gregorian.IsoDayOfWeek);
    }
}

Output:

Saturday
Thursday

So it looks like Ruby has taken the transition into account, but MySQL hasn't.

The big question is: which calendar system were you interested in? (In 1401 you probably want the Julian one - but if you have dates in the 16th century or later, it becomes trickier...)

like image 148
Jon Skeet Avatar answered Nov 21 '25 03:11

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!