public void DayOfWeekTest() { DateTime now = ClockProxy.Clock.Now; TimeCalendar calendar = new TimeCalendar(); Assert.AreEqual( new Day( now, calendar ).DayOfWeek, calendar.Culture.Calendar.GetDayOfWeek( now ) ); }
public void EnAuCultureTest() { CultureInfo cultureInfo = new CultureInfo( "en-AU" ); // cultureInfo.DateTimeFormat.CalendarWeekRule = CalendarWeekRule.FirstFourDayWeek; TimeCalendar calendar = new TimeCalendar( new TimeCalendarConfig { Culture = cultureInfo } ); Week week = new Week( new DateTime( 2011, 4, 1, 9, 0, 0 ), calendar ); Assert.AreEqual( week.Start, new DateTime( 2011, 3, 28 ) ); }
// ---------------------------------------------------------------------- public DateTime AddBusinessDays3( DateTime startDate, int noOfBusinessDays ) { TimeCalendar timeCalendar = new TimeCalendar( new TimeCalendarConfig { EndOffset = TimeSpan.Zero } ); CalendarDateAdd dateAdd = new CalendarDateAdd( timeCalendar ); dateAdd.AddWorkingWeekDays(); // Exclude holidays (simplified from the actual production code, here reduced to one hardcoded day) dateAdd.ExcludePeriods.Add( new Day( 2012, 6, 22, timeCalendar ) ); DateTime? targetDate = dateAdd.Add( startDate.Date.AddTicks( 1 ), new TimeSpan( noOfBusinessDays, 0, 0, 0, 0 ) ); return targetDate.HasValue ? targetDate.Value.Date : startDate.Date; }
public void EndOffsetTest() { Assert.AreEqual( new TimeCalendar().EndOffset, TimeCalendar.DefaultEndOffset ); TimeSpan offset = Duration.Second.Negate(); TimeCalendar timeCalendar = new TimeCalendar( new TimeCalendarConfig { StartOffset = TimeSpan.Zero, EndOffset = offset } ); Assert.AreEqual( timeCalendar.EndOffset, offset ); }
public void CalendarDateAdd1Test() { TimeCalendar timeCalendar = new TimeCalendar( new TimeCalendarConfig { Culture = new CultureInfo( "en-AU" ), EndOffset = TimeSpan.Zero } ); CalendarDateAdd calendarDateAdd = new CalendarDateAdd( timeCalendar ); calendarDateAdd.AddWorkingWeekDays(); calendarDateAdd.ExcludePeriods.Add( new Day( 2011, 4, 4, calendarDateAdd.Calendar ) ); calendarDateAdd.WorkingHours.Add( new HourRange( 8, 18 ) ); DateTime start = new DateTime( 2011, 4, 1, 9, 0, 0 ); Assert.AreEqual( calendarDateAdd.Add( start, new TimeSpan( 22, 0, 0 ) ), new DateTime( 2011, 4, 6, 11, 0, 0 ) ); Assert.AreEqual( calendarDateAdd.Add( start, new TimeSpan( 29, 0, 0 ) ), new DateTime( 2011, 4, 7, 8, 0, 0 ) ); }
} // CalcDayllightDuration // ---------------------------------------------------------------------- protected virtual TimeSpan DoCalcDuration(DateTime start, DateTime end, TimeZoneInfo timeZone = null) { if (start.Equals(end)) { return(TimeSpan.Zero); } // test range TimeRange testRange = new TimeRange(start, end); // search range DateTime searchStart = new Day(testRange.Start).Start; DateTime serachEnd = new Day(testRange.End).GetNextDay().Start; TimeRange searchPeriod = new TimeRange(searchStart, serachEnd); // exclude periods filter.ExcludePeriods.Clear(); filter.ExcludePeriods.AddAll(excludePeriods); // collect hours TimeCalendar calendar = new TimeCalendar(new TimeCalendarConfig { EndOffset = TimeSpan.Zero }); CalendarPeriodCollector collector = new CalendarPeriodCollector(filter, searchPeriod, SeekDirection.Forward, calendar); collector.CollectHours(); TimeSpan duration = TimeSpan.Zero; collector.Periods.AddAll(includePeriods); foreach (ICalendarTimeRange period in collector.Periods) { // get the intersection of the test-range and the day hours ITimePeriod intersection = testRange.GetIntersection(period); if (intersection == null) { continue; } duration = duration.Add(durationProvider.GetDuration(intersection.Start, intersection.End)); } return(start < end ? duration : duration.Negate()); } // DoCalcDuration
// ---------------------------------------------------------------------- private bool HasSameData( TimeCalendar comp ) { return culture.Equals( comp.culture ) && startOffset == comp.startOffset && endOffset == comp.endOffset && yearBaseMonth == comp.yearBaseMonth && yearWeekType == comp.yearWeekType && dayNameType == comp.dayNameType && monthNameType == comp.monthNameType; }
public void CalendarWeekSample() { DateTime testDate = new DateTime( 2007, 12, 31 ); // .NET calendar week TimeCalendar calendar = new TimeCalendar(); Console.WriteLine( "Calendar Week of {0}: {1}", testDate.ToShortDateString(), new Week( testDate, calendar ).WeekOfYear ); // > Calendar Week of 31.12.2007: 53 // ISO 8601 calendar week TimeCalendar calendarIso8601 = new TimeCalendar( new TimeCalendarConfig { YearWeekType = YearWeekType.Iso8601 } ); Console.WriteLine( "ISO 8601 Week of {0}: {1}", testDate.ToShortDateString(), new Week( testDate, calendarIso8601 ).WeekOfYear ); // > ISO 8601 Week of 31.12.2007: 1 }
public void YearStartSample() { TimeCalendar calendar = new TimeCalendar( new TimeCalendarConfig { YearBaseMonth = YearMonth.February } ); Years years = new Years( 2012, 2, calendar ); // 2012-2013 Console.WriteLine( "Quarters of Years (February): {0}", years ); // > Quarters of Years (February): 2012 - 2014; 01.02.2012 - 31.01.2014 | 730.23:59 foreach ( Year year in years.GetYears() ) { foreach ( Quarter quarter in year.GetQuarters() ) { Console.WriteLine( "Quarter: {0}", quarter ); } } // > Quarter: Q1 2012; 01.02.2012 - 30.04.2012 | 89.23:59 // > Quarter: Q2 2012; 01.05.2012 - 31.07.2012 | 91.23:59 // > Quarter: Q3 2012; 01.08.2012 - 31.10.2012 | 91.23:59 // > Quarter: Q4 2012; 01.11.2012 - 31.01.2013 | 91.23:59 // > Quarter: Q1 2013; 01.02.2013 - 30.04.2013 | 88.23:59 // > Quarter: Q2 2013; 01.05.2013 - 31.07.2013 | 91.23:59 // > Quarter: Q3 2013; 01.08.2013 - 31.10.2013 | 91.23:59 // > Quarter: Q4 2013; 01.11.2013 - 31.01.2014 | 91.23:59 }
// ---------------------------------------------------------------------- public TimeSpan CalcWorkingPeriod( DateTime start, DateTime end, ITimePeriodCollection excludePeriods = null) { if ( start.Equals( end ) ) { return TimeSpan.Zero; } // test range TimeRange testRange = new TimeRange( start, end ); // search range DateTime searchStart = new Day( testRange.Start ).Start; DateTime serachEnd = new Day( testRange.End ).GetNextDay().Start; TimeRange searchPeriod = new TimeRange( searchStart, serachEnd ); // search filter CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.AddWorkingWeekDays(); // working days if ( excludePeriods != null ) { filter.ExcludePeriods.AddAll( excludePeriods ); } filter.CollectingHours.Add( new HourRange( 07, 12 ) ); // working hours filter.CollectingHours.Add( new HourRange( 13, 19 ) ); // working hours // collect working hours TimeCalendar calendar = new TimeCalendar( new TimeCalendarConfig { EndOffset = TimeSpan.Zero } ); CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, searchPeriod, SeekDirection.Forward, calendar ); collector.CollectHours(); TimeSpan workingPeriod = new TimeSpan(); foreach ( ICalendarTimeRange period in collector.Periods ) { // get the intersection of the test-range and the day working-hours ITimePeriod intersection = testRange.GetIntersection( period ); if ( intersection == null ) { continue; } workingPeriod = workingPeriod.Add( intersection.Duration ); } return workingPeriod; }
// ---------------------------------------------------------------------- public void GetDaysOfPastQuarter( DateTime moment, out DateTime firstDay, out DateTime lastDay ) { TimeCalendar calendar = new TimeCalendar( new TimeCalendarConfig { YearBaseMonth = YearMonth.October } ); Quarter quarter = new Quarter( moment, calendar ); Quarter pastQuarter = quarter.GetPreviousQuarter(); firstDay = pastQuarter.FirstDayStart; lastDay = pastQuarter.LastDayStart; }
public void FiscalYearWeekCountSample() { TimeCalendar calendar = new TimeCalendar( new TimeCalendarConfig { YearBaseMonth = YearMonth.April, // April year base month //YearWeekType = YearWeekType.Iso8601, // ISO 8601 week numbering YearType = YearType.FiscalYear// treat years as fiscal years } ); bool countOnlyFullWeeks = true; Year year = new Year( 2012, calendar ); foreach ( Month month in year.GetMonths() ) { Month nextMonth = month.GetNextMonth(); Week week = new Week( month.Start.Date ); int weekCount = 0; while ( week.Start.Date < nextMonth.Start.Date ) { if ( countOnlyFullWeeks ) { if ( month.HasInside( week ) ) { weekCount++; } } else { weekCount++; } week = week.GetNextWeek(); } Console.WriteLine( "Month: " + month + ", Tot Weeks : " + weekCount ); } }
public void DaysOfWeekSample() { Week week = new Week( 2012, 13 ); foreach ( Day day in week.GetDays() ) { Console.WriteLine( day ); } Console.WriteLine( "ISO 8601 Week" ); TimeCalendar calendarIso8601 = new TimeCalendar( new TimeCalendarConfig { YearWeekType = YearWeekType.Iso8601 } ); Week isoWeek = new Week( 2012, 13, calendarIso8601 ); foreach ( Day isoDay in isoWeek.GetDays() ) { Console.WriteLine( isoDay ); } }
public void CalendarWeekSample() { DateTime testDate = new DateTime( 2007, 12, 31 ); // .NET calendar week TimeCalendar calendar = new TimeCalendar(); Console.WriteLine( "Calendar Week of {0}: {1}", testDate.ToShortDateString(), new Week( testDate, calendar ).WeekOfYear ); // > Calendar Week of 31.12.2007: 53 // ISO 8601 calendar week CultureInfo culture = new CultureInfo( "EN-us" ); culture.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Monday; culture.DateTimeFormat.CalendarWeekRule = CalendarWeekRule.FirstFourDayWeek; TimeCalendar calendarIso8601 = new TimeCalendar( new TimeCalendarConfig { YearWeekType = YearWeekType.Iso8601, Culture = culture } ); Console.WriteLine( "ISO 8601 Week of {0}: {1}", testDate.ToShortDateString(), new Week( testDate, calendarIso8601 ).WeekOfYear ); // > ISO 8601 Week of 31.12.2007: 1 }
public void TimePeriodMapperSample() { TimeCalendar timeCalendar = new TimeCalendar(); CultureInfo ci = CultureInfo.InvariantCulture; DateTime start = new DateTime( 2011, 3, 1, 13, 0, 0 ); DateTime end = new DateTime( 2011, 3, 1, 14, 0, 0 ); Console.WriteLine( "Original start: {0}", start.ToString( "HH:mm:ss.fffffff", ci ) ); // > Original start: 13:00:00.0000000 Console.WriteLine( "Original end: {0}", end.ToString( "HH:mm:ss.fffffff", ci ) ); // > Original end: 14:00:00.0000000 Console.WriteLine( "Mapping offset start: {0}", timeCalendar.StartOffset ); // > Mapping offset start: 00:00:00 Console.WriteLine( "Mapping offset end: {0}", timeCalendar.EndOffset ); // > Mapping offset end: -00:00:00.0000001 Console.WriteLine( "Mapped start: {0}", timeCalendar.MapStart( start ).ToString( "HH:mm:ss.fffffff", ci ) ); // > Mapped start: 13:00:00.0000000 Console.WriteLine( "Mapped end: {0}", timeCalendar.MapEnd( end ).ToString( "HH:mm:ss.fffffff", ci ) ); // > Mapped end: 13:59:59.9999999 }
public void CalendarTest() { TimeCalendar calendar = new TimeCalendar(); CalendarTimeRange calendarTimeRange = new CalendarTimeRange( TimeRange.Anytime, calendar ); Assert.AreEqual( calendarTimeRange.Calendar, calendar ); }
// ---------------------------------------------------------------------- private double CalculateBusinessHours( ITimePeriod timePeriod ) { double businessHours = 0; // collect filter CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter(); filter.AddWorkingWeekDays(); filter.CollectingHours.Add( new HourRange( 8, 18 ) ); //filter.ExcludePeriods.Add( new Day( 2012, 4, 6 ) ); // mor exclude periods // collect the working hours TimeRange collectorTimeRange = new TimeRange( timePeriod.Start.Date, timePeriod.End.Date.AddDays( 1 ) ); TimeCalendar calendar = new TimeCalendar( new TimeCalendarConfig { EndOffset = TimeSpan.Zero } ); CalendarPeriodCollector collector = new CalendarPeriodCollector( filter, collectorTimeRange, SeekDirection.Forward, calendar ); collector.CollectHours(); if ( collector.Periods.Count > 0 ) { // calculate the business hour periods collector.Periods.Add( timePeriod ); // add source period to find the intersections TimePeriodIntersector<TimeRange> intersector = new TimePeriodIntersector<TimeRange>(); ITimePeriodCollection businessHourPeriods = intersector.IntersectPeriods( collector.Periods ); // collect the business hours foreach ( TimeRange businessHourPeriod in businessHourPeriods ) { businessHours += businessHourPeriod.Duration.TotalHours; } } return businessHours; }