Beispiel #1
0
 public void DayOfWeek_BothWaysValid(DayOfWeek bcl, IsoDayOfWeek noda)
 {
     Assert.AreEqual(bcl, BclConversions.ToDayOfWeek(noda));
     Assert.AreEqual(noda, BclConversions.ToIsoDayOfWeek(bcl));
     Assert.AreEqual(bcl, noda.ToDayOfWeek());
     Assert.AreEqual(noda, bcl.ToIsoDayOfWeek());
 }
Beispiel #2
0
            // Converts a TimeZoneInfo "TransitionTime" to a "ZoneYearOffset" - the two correspond pretty closely.
            private static ZoneYearOffset ConvertTransition(TimeZoneInfo.TransitionTime transitionTime)
            {
                // Used for both fixed and non-fixed transitions.
                LocalTime timeOfDay = LocalDateTime.FromDateTime(transitionTime.TimeOfDay).TimeOfDay;

                // Easy case - fixed day of the month.
                if (transitionTime.IsFixedDateRule)
                {
                    return(new ZoneYearOffset(TransitionMode.Wall, transitionTime.Month, transitionTime.Day, 0, false, timeOfDay));
                }

                // Floating: 1st Sunday in March etc.
                int  dayOfWeek = (int)BclConversions.ToIsoDayOfWeek(transitionTime.DayOfWeek);
                int  dayOfMonth;
                bool advance;

                // "Last"
                if (transitionTime.Week == 5)
                {
                    advance    = false;
                    dayOfMonth = -1;
                }
                else
                {
                    advance = true;
                    // Week 1 corresponds to ">=1"
                    // Week 2 corresponds to ">=8" etc
                    dayOfMonth = (transitionTime.Week * 7) - 6;
                }
                return(new ZoneYearOffset(TransitionMode.Wall, transitionTime.Month, dayOfMonth, dayOfWeek, advance, timeOfDay));
            }
Beispiel #3
0
        public void ToDayOfWeek_InvalidValues(IsoDayOfWeek noda)
        {
            Assert.Throws <ArgumentOutOfRangeException>(() => BclConversions.ToDayOfWeek(noda));
            Assert.Throws <ArgumentOutOfRangeException>(() => noda.ToDayOfWeek());
#pragma warning disable CS0618 // Type or member is obsolete
            Assert.Throws <ArgumentOutOfRangeException>(() => noda.ToIsoDayOfWeek());
#pragma warning restore CS0618 // Type or member is obsolete
        }
Beispiel #4
0
        public void DayOfWeek_BothWaysValid(DayOfWeek bcl, IsoDayOfWeek noda)
        {
            Assert.AreEqual(bcl, BclConversions.ToDayOfWeek(noda));
            Assert.AreEqual(noda, BclConversions.ToIsoDayOfWeek(bcl));
            Assert.AreEqual(bcl, noda.ToDayOfWeek());
#pragma warning disable CS0618 // Type or member is obsolete
            Assert.AreEqual(bcl, noda.ToIsoDayOfWeek());
#pragma warning restore CS0618 // Type or member is obsolete
            Assert.AreEqual(noda, bcl.ToIsoDayOfWeek());
        }
        public void DayOfWeek_AroundEpoch()
        {
            // Test about couple of months around the Unix epoch. If that works, I'm confident the rest will.
            LocalDate date = new LocalDate(1969, 12, 1);

            for (int i = 0; i < 60; i++)
            {
                Assert.AreEqual(
                    BclConversions.ToIsoDayOfWeek(date.AtMidnight().ToDateTimeUnspecified().DayOfWeek),
                    date.DayOfWeek);
                date = date.PlusDays(1);
            }
        }
Beispiel #6
0
        public void DayOfWeek_AroundEpoch()
        {
            // Test about couple of months around the Unix epoch. If that works, I'm confident the rest will.
            LocalDateTime dateTime = new LocalDateTime(1969, 12, 1, 0, 0);

            for (int i = 0; i < 60; i++)
            {
                // Check once per hour of the day, just in case something's messed up based on the time of day.
                for (int hour = 0; hour < 24; hour++)
                {
                    Assert.AreEqual(BclConversions.ToIsoDayOfWeek(dateTime.ToDateTimeUnspecified().DayOfWeek),
                                    dateTime.DayOfWeek);
                    dateTime = dateTime.PlusHours(1);
                }
            }
        }
Beispiel #7
0
            // Converts a TimeZoneInfo "TransitionTime" to a "ZoneYearOffset" - the two correspond pretty closely.
            private static ZoneYearOffset ConvertTransition(TimeZoneInfo.TransitionTime transitionTime)
            {
                // Used for both fixed and non-fixed transitions.
                LocalTime timeOfDay = LocalDateTime.FromDateTime(transitionTime.TimeOfDay).TimeOfDay;

                // Transitions at midnight are represented in the Windows database by a transition one millisecond early.
                // See BclDateTimeZoneTest.TransitionAtMidnight for a concrete example.
                // We adjust to midnight to represent the correct data - it's clear this is just a data fudge.
                // It's probably done like this to allow the rule to represent "Saturday 24:00" instead of "Sunday 00:00".
                bool addDay = false;

                if (timeOfDay == OneMillisecondBeforeMidnight)
                {
                    timeOfDay = LocalTime.Midnight;
                    addDay    = true;
                }

                // Easy case - fixed day of the month.
                if (transitionTime.IsFixedDateRule)
                {
                    return(new ZoneYearOffset(TransitionMode.Wall, transitionTime.Month, transitionTime.Day, 0, false, timeOfDay, addDay));
                }

                // Floating: 1st Sunday in March etc.
                int  dayOfWeek = (int)BclConversions.ToIsoDayOfWeek(transitionTime.DayOfWeek);
                int  dayOfMonth;
                bool advance;

                // "Last"
                if (transitionTime.Week == 5)
                {
                    advance    = false;
                    dayOfMonth = -1;
                }
                else
                {
                    advance = true;
                    // Week 1 corresponds to ">=1"
                    // Week 2 corresponds to ">=8" etc
                    dayOfMonth = (transitionTime.Week * 7) - 6;
                }
                return(new ZoneYearOffset(TransitionMode.Wall, transitionTime.Month, dayOfMonth, dayOfWeek, advance, timeOfDay, addDay));
            }
 /// <summary>
 /// Converts a <see cref="IsoDayOfWeek"/> into the corresponding <see cref="DayOfWeek"/>.
 /// </summary>
 /// <remarks>This is a convenience method which calls <see cref="BclConversions.ToDayOfWeek"/>.</remarks>
 /// <param name="isoDayOfWeek">The <c>IsoDayOfWeek</c> to convert.</param>
 /// <returns>The <c>DayOfWeek</c> equivalent to <paramref name="isoDayOfWeek"/></returns>
 public static DayOfWeek ToDayOfWeek(this IsoDayOfWeek isoDayOfWeek) => BclConversions.ToDayOfWeek(isoDayOfWeek);
Beispiel #9
0
 /// <summary>
 /// Converts a <see cref="DayOfWeek"/> into the corresponding <see cref="IsoDayOfWeek"/>.
 /// </summary>
 /// <remarks>This is a convenience method which calls <see cref="BclConversions.ToIsoDayOfWeek"/>.</remarks>
 /// <param name="dayOfWeek">The <c>DayOfWeek</c> to convert.</param>
 /// <returns>The <c>IsoDayOfWeek</c> equivalent to <paramref name="dayOfWeek"/></returns>
 public static IsoDayOfWeek ToIsoDayOfWeek(this DayOfWeek dayOfWeek) => BclConversions.ToIsoDayOfWeek(dayOfWeek);
Beispiel #10
0
 public void ToIsoDayOfWeek_InvalidValues(DayOfWeek bcl)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => BclConversions.ToIsoDayOfWeek(bcl));
 }
Beispiel #11
0
 public void ToDayOfWeek_InvalidValues(IsoDayOfWeek noda)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => BclConversions.ToDayOfWeek(noda));
     Assert.Throws <ArgumentOutOfRangeException>(() => noda.ToDayOfWeek());
 }