Example #1
0
 public void CultureTest()
 {
     Assert.AreEqual( new TimeCalendar().Culture, Thread.CurrentThread.CurrentCulture );
     CultureTestData cultures = new CultureTestData();
     foreach ( CultureInfo culture in cultures )
     {
         Assert.AreEqual( TimeCalendar.New( culture ).Culture, culture );
     }
 }
Example #2
0
 public void FirstDayOfWeekTest()
 {
     Assert.AreEqual( new TimeCalendar().FirstDayOfWeek, Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek );
     CultureTestData cultures = new CultureTestData();
     foreach ( CultureInfo culture in cultures )
     {
         Assert.AreEqual( TimeCalendar.New( culture ).FirstDayOfWeek, culture.DateTimeFormat.FirstDayOfWeek );
     }
 }
Example #3
0
 public void GetDayNameTest()
 {
     DateTime now = ClockProxy.Clock.Now;
     CultureTestData cultures = new CultureTestData();
     foreach ( CultureInfo culture in cultures )
     {
         Assert.AreEqual( TimeCalendar.New( culture ).GetDayName( now.DayOfWeek ), culture.DateTimeFormat.GetDayName( now.DayOfWeek ) );
         Assert.AreEqual( TimeCalendar.New( culture ).GetDayName( testDate.DayOfWeek ), culture.DateTimeFormat.GetDayName( testDate.DayOfWeek ) );
     }
 }
Example #4
0
        public void CurrentMonthTest()
        {
            DateTime now = ClockProxy.Clock.Now;

            CultureTestData cultures = new CultureTestData();
            foreach ( CultureInfo culture in cultures )
            {
                DateTime currentYearMonthStart = new DateTime( now.Year, now.Month, 1 );
                DateTime currentYearMonthEnd =
                    currentYearMonthStart.AddDays( culture.Calendar.GetDaysInMonth( currentYearMonthStart.Year,
                                                                                                                                                    currentYearMonthStart.Month ) );
                Month month = new Month( currentYearMonthStart, TimeCalendar.New( culture, TimeSpan.Zero, TimeSpan.Zero ) );

                Assert.AreEqual( month.Year, now.Year );
                Assert.AreEqual( month.YearMonth, (YearMonth)now.Month );
                Assert.AreEqual( month.Start, currentYearMonthStart );
                Assert.AreEqual( month.End, currentYearMonthEnd );
            }
        }
Example #5
0
        public void AddMonthsTest()
        {
            DateTime now = ClockProxy.Clock.Now;

            CultureTestData cultures = new CultureTestData();
            foreach ( CultureInfo culture in cultures )
            {

                DateTime currentYearMonthStart = new DateTime( now.Year, now.Month, 1 );
                DateTime currentYearMonthEnd =
                    currentYearMonthStart.AddDays( culture.Calendar.GetDaysInMonth( currentYearMonthStart.Year,
                                                                                                                                                    currentYearMonthStart.Month ) );
                Month currentYearMonth = new Month( currentYearMonthStart,
                                                                                        TimeCalendar.New( culture, TimeSpan.Zero, TimeSpan.Zero ) );
                Assert.AreEqual( currentYearMonth.Start, currentYearMonthStart );
                Assert.AreEqual( currentYearMonth.End, currentYearMonthEnd );

                Assert.AreEqual( currentYearMonth.AddMonths( 0 ), currentYearMonth );

                DateTime previousYearMonthStart = new DateTime( now.Year - 1, now.Month, 1 );
                DateTime previousYearMonthEnd =
                    previousYearMonthStart.AddDays( culture.Calendar.GetDaysInMonth( previousYearMonthStart.Year,
                                                                                                                                                     previousYearMonthStart.Month ) );
                Month previousYearMonth = currentYearMonth.AddMonths( TimeSpec.MonthsPerYear * -1 );
                Assert.AreEqual( previousYearMonth.Start, previousYearMonthStart );
                Assert.AreEqual( previousYearMonth.End, previousYearMonthEnd );
                Assert.AreEqual( previousYearMonth.YearMonth, currentYearMonth.YearMonth );
                Assert.AreEqual( previousYearMonth.Start.Year, currentYearMonth.Start.Year - 1 );
                Assert.AreEqual( previousYearMonth.End.Year, currentYearMonth.End.Year - 1 );

                DateTime nextYearMonthStart = new DateTime( now.Year + 1, now.Month, 1 );
                DateTime nextYearMonthEnd =
                    nextYearMonthStart.AddDays( culture.Calendar.GetDaysInMonth( nextYearMonthStart.Year, nextYearMonthStart.Month ) );
                Month nextYearMonth = currentYearMonth.AddMonths( TimeSpec.MonthsPerYear );
                Assert.AreEqual( nextYearMonth.Start, nextYearMonthStart );
                Assert.AreEqual( nextYearMonth.End, nextYearMonthEnd );
                Assert.AreEqual( nextYearMonth.YearMonth, currentYearMonth.YearMonth );
                Assert.AreEqual( nextYearMonth.Start.Year, currentYearMonth.Start.Year + 1 );
                Assert.AreEqual( nextYearMonth.End.Year, currentYearMonth.End.Year + 1 );
            }
        }
Example #6
0
        public void DefaultCalendarTest()
        {
            const int startWeek = 1;
            int currentYear = ClockProxy.Clock.Now.Year;
            CultureTestData cultures = new CultureTestData();
            foreach ( CultureInfo culture in cultures )
            {
                foreach ( YearWeekType yearWeekType in Enum.GetValues( typeof( YearWeekType ) ) )
                {
                    int weeksOfYear = TimeTool.GetWeeksOfYear( currentYear, culture, yearWeekType );
                    for ( int weekOfYear = startWeek; weekOfYear < weeksOfYear; weekOfYear++ )
                    {
                        Week week = new Week( currentYear, weekOfYear, TimeCalendar.New( culture, YearMonth.January, yearWeekType ) );
                        Assert.AreEqual( week.Year, currentYear );

                        DateTime weekStart = TimeTool.GetStartOfYearWeek( currentYear, weekOfYear, culture, yearWeekType );
                        DateTime weekEnd = weekStart.AddDays( TimeSpec.DaysPerWeek );
                        Assert.AreEqual( week.Start, weekStart.Add( week.Calendar.StartOffset ) );
                        Assert.AreEqual( week.End, weekEnd.Add( week.Calendar.EndOffset ) );
                    }
                }
            }
        }
Example #7
0
 public void GetDayOfMonthTest()
 {
     DateTime now = ClockProxy.Clock.Now;
     CultureTestData cultures = new CultureTestData();
     foreach ( CultureInfo culture in cultures )
     {
         Assert.AreEqual( TimeCalendar.New( culture ).GetDayOfMonth( now ), culture.Calendar.GetDayOfMonth( now ) );
         Assert.AreEqual( TimeCalendar.New( culture ).GetDayOfMonth( testDate ), culture.Calendar.GetDayOfMonth( testDate ) );
     }
 }
Example #8
0
        public void UnmapStartTest()
        {
            DateTime now = ClockProxy.Clock.Now;
            TimeSpan offset = Duration.Second;
            CultureTestData cultures = new CultureTestData();
            foreach ( CultureInfo culture in cultures )
            {
                Assert.AreEqual( TimeCalendar.New( culture, TimeSpan.Zero, TimeSpan.Zero ).UnmapStart( now ), now );
                Assert.AreEqual( TimeCalendar.New( culture, offset, TimeSpan.Zero ).UnmapStart( now ), now.Subtract( offset ) );

                Assert.AreEqual( TimeCalendar.New( culture, TimeSpan.Zero, TimeSpan.Zero ).UnmapStart( testDate ), testDate );
                Assert.AreEqual( TimeCalendar.New( culture, offset, TimeSpan.Zero ).UnmapStart( testDate ), testDate.Subtract( offset ) );
            }
        }
Example #9
0
        public void MapEndTest()
        {
            DateTime now = ClockProxy.Clock.Now;
            TimeSpan offset = Duration.Second.Negate();
            CultureTestData cultures = new CultureTestData();
            foreach ( CultureInfo culture in cultures )
            {
                Assert.AreEqual( TimeCalendar.New( culture, TimeSpan.Zero, TimeSpan.Zero ).MapEnd( now ), now );
                Assert.AreEqual( TimeCalendar.New( culture, TimeSpan.Zero, offset ).MapEnd( now ), now.Add( offset ) );

                Assert.AreEqual( TimeCalendar.New( culture, TimeSpan.Zero, TimeSpan.Zero ).MapEnd( testDate ), testDate );
                Assert.AreEqual( TimeCalendar.New( culture, TimeSpan.Zero, offset ).MapEnd( testDate ), testDate.Add( offset ) );
            }
        }
Example #10
0
        public void GetWeekOfYearTest()
        {
            DateTime now = ClockProxy.Clock.Now;
            CultureTestData cultures = new CultureTestData();
            foreach ( CultureInfo culture in cultures )
            {
                foreach ( CalendarWeekRule weekRule in Enum.GetValues( typeof( CalendarWeekRule ) ) )
                {
                    culture.DateTimeFormat.CalendarWeekRule = weekRule;

                    int year;
                    int weekOfYear;

                    // calendar week
                    TimeCalendar timeCalendar = TimeCalendar.New( culture );
                    TimeTool.GetWeekOfYear( now, culture, weekRule, culture.DateTimeFormat.FirstDayOfWeek, YearWeekType.Calendar,
                                                                    out year, out weekOfYear );
                    Assert.AreEqual( timeCalendar.GetWeekOfYear( now ), weekOfYear );

                    // iso 8601 calendar week
                    TimeCalendar timeCalendarIso8601 = TimeCalendar.New( culture, YearMonth.January, YearWeekType.Iso8601 );
                    TimeTool.GetWeekOfYear( now, culture, weekRule, culture.DateTimeFormat.FirstDayOfWeek, YearWeekType.Iso8601,
                                                                    out year, out weekOfYear );
                    Assert.AreEqual( timeCalendarIso8601.GetWeekOfYear( now ), weekOfYear );
                }
            }
        }
Example #11
0
        public void IsSameWeekTest()
        {
            DateTime previousWeek = testDate.AddDays( TimeSpec.DaysPerWeek + 1 );
            DateTime nextWeek = testDate.AddDays( TimeSpec.DaysPerWeek + 1 );

            CultureTestData cultures = new CultureTestData();
            foreach ( CultureInfo culture in cultures )
            {
                foreach ( CalendarWeekRule weekRule in Enum.GetValues( typeof( CalendarWeekRule ) ) )
                {
                    culture.DateTimeFormat.CalendarWeekRule = weekRule;

                    foreach ( YearWeekType yearWeekType in Enum.GetValues( typeof( YearWeekType ) ) )
                    {
                        int year;
                        int weekOfYear;

                        TimeTool.GetWeekOfYear( testDate, culture, yearWeekType, out year, out weekOfYear );
                        DateTime startOfWeek = TimeTool.GetStartOfYearWeek( year, weekOfYear, culture, yearWeekType );

                        Assert.IsTrue( TimeCompare.IsSameWeek( testDate, startOfWeek, culture, yearWeekType ) );
                        Assert.IsTrue( TimeCompare.IsSameWeek( testDate, testDate, culture, yearWeekType ) );
                        Assert.IsTrue( TimeCompare.IsSameWeek( testDiffDate, testDiffDate, culture, yearWeekType ) );
                        Assert.IsFalse( TimeCompare.IsSameWeek( testDate, testDiffDate, culture, yearWeekType ) );
                        Assert.IsFalse( TimeCompare.IsSameWeek( testDate, previousWeek, culture, yearWeekType ) );
                        Assert.IsFalse( TimeCompare.IsSameWeek( testDate, nextWeek, culture, yearWeekType ) );

                        foreach ( DayOfWeek dayOfWeek in Enum.GetValues( typeof( DayOfWeek ) ) )
                        {
                            culture.DateTimeFormat.FirstDayOfWeek = dayOfWeek;
                            TimeTool.GetWeekOfYear( testDate, culture, weekRule, dayOfWeek, yearWeekType, out year, out weekOfYear );
                            startOfWeek = TimeTool.GetStartOfYearWeek( year, weekOfYear, culture, weekRule, dayOfWeek, yearWeekType );

                            Assert.IsTrue( TimeCompare.IsSameWeek( testDate, startOfWeek, culture, yearWeekType ) );
                            Assert.IsTrue( TimeCompare.IsSameWeek( testDate, testDate, culture, yearWeekType ) );
                            Assert.IsTrue( TimeCompare.IsSameWeek( testDiffDate, testDiffDate, culture, yearWeekType ) );
                            Assert.IsFalse( TimeCompare.IsSameWeek( testDate, testDiffDate, culture, yearWeekType ) );
                            Assert.IsFalse( TimeCompare.IsSameWeek( testDate, previousWeek, culture, yearWeekType ) );
                            Assert.IsFalse( TimeCompare.IsSameWeek( testDate, nextWeek, culture, yearWeekType ) );
                        }

                    }

                }
            }
        }
Example #12
0
 public void WeeekOfYearIsoTest()
 {
     DateTime moment = new DateTime( 2007, 12, 31 );
     CultureTestData cultures = new CultureTestData();
     foreach ( CultureInfo culture in cultures )
     {
         if ( culture.DateTimeFormat.CalendarWeekRule != CalendarWeekRule.FirstFourDayWeek ||
              culture.DateTimeFormat.FirstDayOfWeek != DayOfWeek.Monday )
         {
             continue;
         }
         int year;
         int weekOfYear;
         TimeTool.GetWeekOfYear( moment, culture, YearWeekType.Iso8601, out year, out weekOfYear );
         Assert.AreEqual( weekOfYear, 1 );
     }
 }
Example #13
0
 public void WeeekOfYearCalendarTest()
 {
     DateTime moment = new DateTime( 2007, 12, 31 );
     CultureTestData cultures = new CultureTestData();
     foreach ( CultureInfo culture in cultures )
     {
         int calendarWeekOfYear = culture.Calendar.GetWeekOfYear(
             moment,
             culture.DateTimeFormat.CalendarWeekRule,
             culture.DateTimeFormat.FirstDayOfWeek );
         int year;
         int weekOfYear;
         TimeTool.GetWeekOfYear( moment, culture, YearWeekType.Calendar, out year, out weekOfYear );
         Assert.AreEqual( weekOfYear, calendarWeekOfYear );
     }
 }