Skip to content

Inconsistent Behavior for Calendars between macOS on Windows #7331

@vcsjones

Description

@vcsjones

This (bad) code here behaves differently for Windows and macOS:

using System;
using System.Globalization;

class Program
{
    private static CultureInfo _parsingCulture;
    
    static void Main(string[] args)
    {
        _parsingCulture = (CultureInfo)CultureInfo.InvariantCulture.Clone();
        _parsingCulture.DateTimeFormat.Calendar = new AsnUTCTimeCalendar();
        
    }

        private class AsnUTCTimeCalendar : GregorianCalendar
        {
            public override int ToFourDigitYear(int year) => (year < 50 ? 2000 : 1900) + year;
        }
}

On Windows, this throws an exception:

System.InvalidOperationException: Cannot set sub-classed Program+UTCTimeCalendar object to System.Globalization.DateTimeFormatInfo object.

On macOS, an exception is not thrown and it "works".

I understand why the Windows version is throwing, but I was surprised that it did not on macOS. I think the correct behavior is that all platforms should throw the exception.

Runtime Environment:
OS Name: Windows
OS Version: 10.0.14393
OS Platform: Windows
RID: win10-x64

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions