Exemplo n.º 1
0
        } // Halfyear

        // ----------------------------------------------------------------------
        public static TimeSpan Halfyear(Calendar calendar, int year, YearHalfyear yearHalfyear)
        {
            YearMonth[] halfyearMonths = TimeTool.GetMonthsOfHalfyear(yearHalfyear);
            TimeSpan    duration       = TimeSpec.NoDuration;

            foreach (YearMonth halfyearMonth in halfyearMonths)
            {
                duration = duration.Add(Month(calendar, year, halfyearMonth));
            }
            return(duration);
        } // Halfyear
Exemplo n.º 2
0
        } // Quarter

        // ----------------------------------------------------------------------
        public static TimeSpan Quarter(Calendar calendar, int year, YearQuarter yearQuarter)
        {
            YearMonth[] quarterMonths = TimeTool.GetMonthsOfQuarter(yearQuarter);
            TimeSpan    duration      = TimeSpec.NoDuration;

            foreach (YearMonth quarterMonth in quarterMonths)
            {
                duration = duration.Add(Month(calendar, year, quarterMonth));
            }
            return(duration);
        } // Quarter
Exemplo n.º 3
0
        } // IsSameQuarter

        // ----------------------------------------------------------------------
        public static bool IsSameQuarter(YearMonth yearStartMonth, DateTime left, DateTime right)
        {
            int leftYear  = TimeTool.GetYearOf(yearStartMonth, left);
            int rightYear = TimeTool.GetYearOf(yearStartMonth, right);

            if (leftYear != rightYear)
            {
                return(false);
            }

            return(TimeTool.GetQuarterOfMonth(yearStartMonth, (YearMonth)left.Month) == TimeTool.GetQuarterOfMonth(yearStartMonth, (YearMonth)right.Month));
        } // IsSameQuarter
Exemplo n.º 4
0
        } // GetPeriodOf

        // ----------------------------------------------------------------------
        private static TimeRange GetPeriodOf(int year, int weekOfYear, int weekCount, ITimeCalendar calendar)
        {
            if (weekCount < 1)
            {
                throw new ArgumentOutOfRangeException("weekCount");
            }

            DateTime start = TimeTool.GetStartOfYearWeek(year, weekOfYear, calendar.Culture, calendar.YearWeekType);
            DateTime end   = start.AddDays(weekCount * TimeSpec.DaysPerWeek);

            return(new TimeRange(start, end));
        } // GetPeriodOf
Exemplo n.º 5
0
        } // GetStartOfWeek

        // ----------------------------------------------------------------------
        public ITimePeriodCollection GetDays()
        {
            TimePeriodCollection days      = new TimePeriodCollection();
            DateTime             startDate = TimeTool.GetStartOfYearWeek(year, startWeek, Calendar.Culture, Calendar.YearWeekType);
            int dayCount = weekCount * TimeSpec.DaysPerWeek;

            for (int i = 0; i < dayCount; i++)
            {
                days.Add(new Day(startDate.AddDays(i), Calendar));
            }
            return(days);
        } // GetDays
Exemplo n.º 6
0
        } // Halfyears

        // ----------------------------------------------------------------------
        public ITimePeriodCollection GetHalfyears()
        {
            TimePeriodCollection halfyears = new TimePeriodCollection();

            for (int i = 0; i < HalfyearCount; i++)
            {
                int          year;
                YearHalfyear halfyear;
                TimeTool.AddHalfyear(BaseYear, StartHalfyear, i, out year, out halfyear);
                halfyears.Add(new Halfyear(year, halfyear, Calendar));
            }
            return(halfyears);
        } // GetHalfyears
Exemplo n.º 7
0
        } // Months

        // ----------------------------------------------------------------------
        public ITimePeriodCollection GetMonths()
        {
            TimePeriodCollection months = new TimePeriodCollection();

            for (int i = 0; i < MonthCount; i++)
            {
                int       year;
                YearMonth month;
                TimeTool.AddMonth(StartYear, StartMonth, i, out year, out month);
                months.Add(new Month(year, month, Calendar));
            }
            return(months);
        } // GetMonths
Exemplo n.º 8
0
        } // Quarters

        // ----------------------------------------------------------------------
        public ITimePeriodCollection GetQuarters()
        {
            TimePeriodCollection quarters = new TimePeriodCollection();

            for (int i = 0; i < QuarterCount; i++)
            {
                int         year;
                YearQuarter quarter;
                TimeTool.AddQuarter(BaseYear, StartQuarter, i, out year, out quarter);
                quarters.Add(new Quarter(year, quarter, Calendar));
            }
            return(quarters);
        } // GetQuarters
Exemplo n.º 9
0
        } // CalendarQuarter

        // ----------------------------------------------------------------------
        public static DateTime Quarter(YearMonth yearStartMonth)
        {
            DateTime now  = ClockProxy.Clock.Now;
            int      year = now.Year;

            if (now.Month - (int)yearStartMonth < 0)
            {
                year--;
            }
            YearQuarter quarter = TimeTool.GetQuarterOfMonth(yearStartMonth, (YearMonth)now.Month);
            int         months  = ((int)quarter - 1) * TimeSpec.MonthsPerQuarter;

            return(new DateTime(year, (int)yearStartMonth, 1).AddMonths(months));
        } // Quarter
Exemplo n.º 10
0
        } // GetHalfyears

        // ----------------------------------------------------------------------
        public ITimePeriodCollection GetQuarters()
        {
            TimePeriodCollection quarters = new TimePeriodCollection();

            for (int i = 0; i < yearCount; i++)
            {
                for (int quarter = 0; quarter < TimeSpec.QuartersPerYear; quarter++)
                {
                    int         year;
                    YearQuarter yearQuarter;
                    TimeTool.AddQuarter(startYear, YearQuarter.First, (i * TimeSpec.QuartersPerYear) + quarter, out year, out yearQuarter);
                    quarters.Add(new Quarter(year, yearQuarter, Calendar));
                }
            }
            return(quarters);
        } // GetQuarters
Exemplo n.º 11
0
        } // EndYearName

        // ----------------------------------------------------------------------
        public ITimePeriodCollection GetHalfyears()
        {
            TimePeriodCollection halfyears = new TimePeriodCollection();

            for (int i = 0; i < yearCount; i++)
            {
                for (int halfyear = 0; halfyear < TimeSpec.HalfyearsPerYear; halfyear++)
                {
                    int          year;
                    YearHalfyear yearHalfyear;
                    TimeTool.AddHalfyear(startYear, YearHalfyear.First, (i * TimeSpec.HalfyearsPerYear) + halfyear, out year, out yearHalfyear);
                    halfyears.Add(new Halfyear(year, yearHalfyear, Calendar));
                }
            }
            return(halfyears);
        } // GetHalfyears
Exemplo n.º 12
0
        } // GetStartOfQuarter

        // ----------------------------------------------------------------------
        private static TimeRange GetPeriodOf(ITimeCalendar calendar, int startYear, YearQuarter startQuarter, int quarterCount)
        {
            if (quarterCount < 1)
            {
                throw new ArgumentOutOfRangeException("quarterCount");
            }

            DateTime    start = GetStartOfQuarter(calendar, startYear, startQuarter);
            int         endYear;
            YearQuarter endQuarter;

            TimeTool.AddQuarter(startYear, startQuarter, quarterCount, out endYear, out endQuarter);
            DateTime end = GetStartOfQuarter(calendar, endYear, endQuarter);

            return(new TimeRange(start, end));
        } // GetPeriodOf
Exemplo n.º 13
0
        } // GetStartOfHalfyear

        // ----------------------------------------------------------------------
        private static TimeRange GetPeriodOf(ITimeCalendar calendar, int startYear, YearHalfyear startHalfyear, int halfyearCount)
        {
            if (halfyearCount < 1)
            {
                throw new ArgumentOutOfRangeException("halfyearCount");
            }

            DateTime     start = GetStartOfHalfyear(calendar, startYear, startHalfyear);
            int          endYear;
            YearHalfyear endHalfyear;

            TimeTool.AddHalfyear(startYear, startHalfyear, halfyearCount, out endYear, out endHalfyear);
            DateTime end = GetStartOfHalfyear(calendar, endYear, endHalfyear);

            return(new TimeRange(start, end));
        } // GetPeriodOf
Exemplo n.º 14
0
        } // GetQuarters

        // ----------------------------------------------------------------------
        public ITimePeriodCollection GetMonths()
        {
            TimePeriodCollection months = new TimePeriodCollection();

            for (int i = 0; i < halfyearCount; i++)
            {
                for (int month = 0; month < TimeSpec.MonthsPerHalfyear; month++)
                {
                    int       year;
                    YearMonth yearMonth;
                    TimeTool.AddMonth(startYear, YearBaseMonth, (i * TimeSpec.MonthsPerHalfyear) + month, out year, out yearMonth);
                    months.Add(new Month(year, yearMonth, Calendar));
                }
            }
            return(months);
        } // GetMonths
Exemplo n.º 15
0
        } // EndMonthOfYearName

        // ----------------------------------------------------------------------
        public ITimePeriodCollection GetDays()
        {
            TimePeriodCollection days      = new TimePeriodCollection();
            DateTime             startDate = GetStartOfMonth(Calendar, startYear, startMonth);

            for (int month = 0; month < monthCount; month++)
            {
                DateTime monthStart  = startDate.AddMonths(month);
                int      daysOfMonth = TimeTool.GetDaysInMonth(monthStart.Year, monthStart.Month);
                for (int day = 0; day < daysOfMonth; day++)
                {
                    days.Add(new Day(monthStart.AddDays(day), Calendar));
                }
            }
            return(days);
        } // GetDays
Exemplo n.º 16
0
        } // CalcYears

        // ----------------------------------------------------------------------
        private int CalcQuarters()
        {
            if (TimeCompare.IsSameMonth(date1, date2))
            {
                return(0);
            }

            int         year1    = TimeTool.GetYearOf(yearBaseMonth, Year1, Month1);
            YearQuarter quarter1 = TimeTool.GetQuarterOfMonth(yearBaseMonth, (YearMonth)Month1);

            int         year2    = TimeTool.GetYearOf(yearBaseMonth, Year2, Month2);
            YearQuarter quarter2 = TimeTool.GetQuarterOfMonth(yearBaseMonth, (YearMonth)Month2);

            return
                (((year2 * TimeSpec.QuartersPerYear) + quarter2) -
                 ((year1 * TimeSpec.QuartersPerYear) + quarter1));
        } // CalcQuarters
Exemplo n.º 17
0
        } // CalcMonths

        // ----------------------------------------------------------------------
        private int CalcWeeks()
        {
            if (TimeCompare.IsSameDay(date1, date2))
            {
                return(0);
            }

            DateTime week1 = TimeTool.GetStartOfWeek(date1, firstDayOfWeek);
            DateTime week2 = TimeTool.GetStartOfWeek(date2, firstDayOfWeek);

            if (week1.Equals(week2))
            {
                return(0);
            }

            return((int)(week2.Subtract(week1).TotalDays / TimeSpec.DaysPerWeek));
        } // CalcWeeks
Exemplo n.º 18
0
        } // IsSameWeek

        // ----------------------------------------------------------------------
        public static bool IsSameWeek(DateTime left, DateTime right, CultureInfo culture,
                                      CalendarWeekRule weekRule, DayOfWeek firstDayOfWeek, YearWeekType weekType)
        {
            if (culture == null)
            {
                throw new ArgumentNullException("culture");
            }

            // left
            int leftWeekOfYear;
            int leftYear;

            TimeTool.GetWeekOfYear(left, culture, weekRule, firstDayOfWeek, weekType, out leftYear, out leftWeekOfYear);

            // rught
            int rightWeekOfYear;
            int rightYear;

            TimeTool.GetWeekOfYear(right, culture, weekRule, firstDayOfWeek, weekType, out rightYear, out rightWeekOfYear);

            return(leftYear == rightYear && leftWeekOfYear == rightWeekOfYear);
        } // IsSameWeek
Exemplo n.º 19
0
        } // GetStartOfMonth

        // ----------------------------------------------------------------------
        private static TimeRange GetPeriodOf(ITimeCalendar calendar, int startYear, YearMonth startMonth, int monthCount)
        {
            if (monthCount < 1)
            {
                throw new ArgumentOutOfRangeException("monthCount");
            }

            DateTime start = GetStartOfMonth(calendar, startYear, startMonth);
            DateTime end;

            if (calendar.YearType == YearType.FiscalYear)
            {
                int       endYear;
                YearMonth endMonth;
                TimeTool.AddMonth(startYear, startMonth, monthCount, out endYear, out endMonth);
                end = GetStartOfMonth(calendar, endYear, endMonth);
            }
            else
            {
                end = start.AddMonths(monthCount);
            }
            return(new TimeRange(start, end));
        } // GetPeriodOf
Exemplo n.º 20
0
        } // GetWeekOfYear

        // ----------------------------------------------------------------------
        public virtual DateTime GetStartOfYearWeek(int year, int weekOfYear)
        {
            return(TimeTool.GetStartOfYearWeek(year, weekOfYear, culture, yearWeekType));
        } // GetStartOfYearWeek
Exemplo n.º 21
0
        } // WeekTimeRange

        // ----------------------------------------------------------------------
        protected WeekTimeRange(DateTime moment, int weekCount, ITimeCalendar calendar) :
            base(GetPeriodOf(moment, weekCount, calendar), calendar)
        {
            TimeTool.GetWeekOfYear(moment, calendar.Culture, calendar.YearWeekType, out year, out startWeek);
            this.weekCount = weekCount;
        } // WeekTimeRange
Exemplo n.º 22
0
        } // GetCalendarPeriod

        #endregion

        #region Interval

        // ----------------------------------------------------------------------
        public string GetInterval(DateTime start, DateTime end,
                                  IntervalEdge startEdge, IntervalEdge endEdge, TimeSpan duration)
        {
            if (end < start)
            {
                throw new ArgumentOutOfRangeException("end");
            }

            StringBuilder sb = new StringBuilder();

            // interval start
            switch (startEdge)
            {
            case IntervalEdge.Closed:
                sb.Append(IntervalStartClosed);
                break;

            case IntervalEdge.Open:
                sb.Append(UseIsoIntervalNotation ? intervalStartOpenIso : intervalStartOpen);
                break;
            }

            bool addDuration       = true;
            bool startHasTimeOfDay = TimeTool.HasTimeOfDay(start);

            // no duration - schow start date (optionally with the time part)
            if (duration == TimeSpec.MinPeriodDuration)
            {
                sb.Append(startHasTimeOfDay ? GetDateTime(start) : GetShortDate(start));
                addDuration = false;
            }
            // within one day: show full start, end time and suration
            else if (TimeCompare.IsSameDay(start, end))
            {
                sb.Append(GetDateTime(start));
                sb.Append(startEndSeparator);
                sb.Append(GetLongTime(end));
            }
            else
            {
                bool endHasTimeOfDay = TimeTool.HasTimeOfDay(start);
                bool hasTimeOfDays   = startHasTimeOfDay || endHasTimeOfDay;
                if (hasTimeOfDays)
                {
                    sb.Append(GetDateTime(start));
                    sb.Append(startEndSeparator);
                    sb.Append(GetDateTime(end));
                }
                else
                {
                    sb.Append(GetShortDate(start));
                    sb.Append(startEndSeparator);
                    sb.Append(GetShortDate(end));
                }
            }

            // interval end
            switch (endEdge)
            {
            case IntervalEdge.Closed:
                sb.Append(IntervalEndClosed);
                break;

            case IntervalEdge.Open:
                sb.Append(UseIsoIntervalNotation ? IntervalEndOpenIso : IntervalEndOpen);
                break;
            }

            // duration
            if (addDuration)
            {
                sb.Append(durationSeparator);
                sb.Append(GetDuration(duration));
            }

            return(sb.ToString());
        } // GetInterval
Exemplo n.º 23
0
        } // GetNextWeek

        // ----------------------------------------------------------------------
        public Week AddWeeks(int weeks)
        {
            DateTime startDate = TimeTool.GetStartOfYearWeek(Year, StartWeek, Calendar.Culture, Calendar.YearWeekType);

            return(new Week(startDate.AddDays(weeks * TimeSpec.DaysPerWeek), Calendar));
        } // AddWeeks
Exemplo n.º 24
0
        } // IsSameYear

        // ----------------------------------------------------------------------
        public static bool IsSameYear(YearMonth yearStartMonth, DateTime left, DateTime right)
        {
            return(TimeTool.GetYearOf(yearStartMonth, left) == TimeTool.GetYearOf(yearStartMonth, right));
        } // IsSameYear
Exemplo n.º 25
0
        } // Quarter

        // ----------------------------------------------------------------------
        public Quarter(DateTime moment, ITimeCalendar calendar) :
            this(TimeTool.GetYearOf(calendar.YearBaseMonth, calendar.GetYear(moment), calendar.GetMonth(moment)),
                 TimeTool.GetQuarterOfMonth(calendar.YearBaseMonth, (YearMonth)moment.Month), calendar)
        {
        } // Quarter
Exemplo n.º 26
0
        } // Years

        // ----------------------------------------------------------------------
        public Years(DateTime moment, int count, ITimeCalendar calendar) :
            this(TimeTool.GetYearOf(calendar.YearBaseMonth, calendar.GetYear(moment), calendar.GetMonth(moment)),
                 count, calendar)
        {
        } // Years
Exemplo n.º 27
0
        } // Halfyear

        // ----------------------------------------------------------------------
        public Halfyear(DateTime moment, ITimeCalendar calendar) :
            this(TimeTool.GetYearOf(calendar.YearBaseMonth, calendar.GetYear(moment), calendar.GetMonth(moment)),
                 TimeTool.GetHalfyearOfMonth(calendar.YearBaseMonth, (YearMonth)calendar.GetMonth(moment)), calendar)
        {
        } // Halfyear
Exemplo n.º 28
0
        } // Halfyears

        // ----------------------------------------------------------------------
        public Halfyears(DateTime moment, YearHalfyear startHalfyear, int count, ITimeCalendar calendar) :
            this(TimeTool.GetYearOf(calendar.YearBaseMonth, calendar.GetYear(moment), calendar.GetMonth(moment)),
                 startHalfyear, count, calendar)
        {
        } // Halfyears
Exemplo n.º 29
0
        } // Quarters

        // ----------------------------------------------------------------------
        public Quarters(DateTime moment, YearQuarter startYearQuarter, int count, ITimeCalendar calendar) :
            this(TimeTool.GetYearOf(calendar.YearBaseMonth, calendar.GetYear(moment), calendar.GetMonth(moment)),
                 startYearQuarter, count, calendar)
        {
        } // Quarters