Example #1
0
 /// <summary>
 /// ��������ʱ���
 /// </summary>
 /// <param name="interval">ָ�����</param>
 /// <param name="StartDate">��ʼʱ��</param>
 /// <param name="EndDate">����ʱ��</param>
 /// <returns></returns>
 public static long DateDiff(DateTimeInterval interval, DateTime StartDate,DateTime EndDate)
 {
     long lngDateDiffValue = 0;
     TimeSpan TS = new System.TimeSpan(EndDate.Ticks - StartDate.Ticks);
     switch (interval)
     {
         case DateTimeInterval.Day:
             lngDateDiffValue = (long)TS.Days;
             break;
         case DateTimeInterval.Hour:
             lngDateDiffValue = (long)TS.TotalHours;
             break;
         case DateTimeInterval.Minute:
             lngDateDiffValue = (long)TS.TotalMinutes;
             break;
         case DateTimeInterval.Month:
             lngDateDiffValue = (long)(TS.Days / 30);
             break;
         case DateTimeInterval.Quarter:
             lngDateDiffValue = (long)((TS.Days / 30) / 3);
             break;
         case DateTimeInterval.Second:
             lngDateDiffValue = (long)TS.TotalSeconds;
             break;
         case DateTimeInterval.Week:
             lngDateDiffValue = (long)(TS.Days / 7);
             break;
         case DateTimeInterval.Year:
             lngDateDiffValue = (long)(TS.Days / 365);
             break;
     }
     return (lngDateDiffValue);
 }
        public void UpdateDateTimeInterval(DateTimeInterval dateTimeImterval)
        {
            // TODO refactor me

            var updatingItem = db.DateTimeIntervals.Single(interval => interval.Id == dateTimeImterval.Id);
            updatingItem.StartDate = dateTimeImterval.StartDate;
            updatingItem.EndDate = dateTimeImterval.EndDate;
            updatingItem.HeatSupplier = dateTimeImterval.HeatSupplier;
        }
Example #3
0
        /// <summary>
        /// same common params similar to the VBScript DateDiff: 
        /// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsfctdatediff.asp
        ///   /*
        ///    * Sample Code:
        ///    * System.DateTime dt1 = new System.DateTime(1974,12,16);
        ///    * System.DateTime dt2 = new System.DateTime(1973,12,16);
        ///    * double diff = DateDiff(DateTimeInterval.Day, dt1, dt2);
        ///    * 
        ///    */
        /// </summary>
        /// <param name="howtocompare"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        //adapted from http://authors.aspalliance.com/nothingmn/default.aspx?whichpoll=nothingmn_117&aid=117
        public static double DateDiff(DateTimeInterval howtocompare, System.DateTime startDate, System.DateTime endDate)
        {
            double diff=0;
            try
            {
                System.TimeSpan TS = new System.TimeSpan(startDate.Ticks-endDate.Ticks);
                switch (howtocompare)
                {
                    case DateTimeInterval.Tick:
                        diff = Convert.ToDouble(TS.Ticks);
                        break;
                    case DateTimeInterval.Millisecond:
                        diff = Convert.ToDouble(TS.TotalMilliseconds);
                        break;
                    case DateTimeInterval.Second:
                        diff = Convert.ToDouble(TS.TotalSeconds);
                        break;
                    case DateTimeInterval.Minute:
                        diff = Convert.ToDouble(TS.TotalMinutes);
                        break;
                    case DateTimeInterval.Hour:
                        diff = Convert.ToDouble(TS.TotalHours);
                        break;
                    case DateTimeInterval.Day:
                        diff = Convert.ToDouble(TS.TotalDays);
                        break;
                    case DateTimeInterval.Week:
                        diff = Convert.ToDouble(TS.TotalDays/7);
                        break;
                    case DateTimeInterval.Fortnight:
                        diff = Convert.ToDouble(TS.TotalDays/15);
                        break;
                    case DateTimeInterval.Month:
                        diff = Convert.ToDouble((TS.TotalDays/365)/12);
                        break;
                    case DateTimeInterval.Quarter:
                        diff = Convert.ToDouble((TS.TotalDays/365)/4);
                        break;
                    case DateTimeInterval.Year:
                        diff = Convert.ToDouble(TS.TotalDays/365);
                        break;
                }

            }
            catch
            {
                diff = -1;
            }
            return diff;
        }
Example #4
0
        /// <summary>
        /// Gets the time.
        /// </summary>
        /// <param name="date">The date.</param>
        /// <param name="calendar">The calendar.</param>
        /// <returns>System.Nullable{DateTime}.</returns>
        private static DateTime? GetTime(DateTime date, ICalendar calendar)
        {
            if (calendar == null || calendar.WorkingIntervals == null || !calendar.WorkingIntervals.Any())
            {
                return date;
            }

            var day = date.DayOfWeek;
            var skipInterval = false;

            foreach (var item in calendar.WorkingIntervals.OrderBy(x => x.StartDate).Where(x => x.Weekday == day.ToString()))
            {
                if (!item.StartDate.HasValue) continue;

                if (calendar.WorkingExceptions != null)
                {
                    var startDate = new DateTime(date.Year, date.Month, date.Day, item.StartDate.Value.Hour, item.StartDate.Value.Minute, 0);

                    var finishDateHour = 0;
                    var finishDateMinute = 0;
                    if (item.FinishDate != null)
                    {
                        finishDateHour = item.FinishDate.Value.Hour;
                        finishDateMinute = item.FinishDate.Value.Minute;
                    }
                    var finishDate = new DateTime(date.Date.Year, date.Date.Month, date.Date.Day, finishDateHour, finishDateMinute, 0);
                    var workingInterval = new DateTimeInterval(startDate, finishDate);
                    var intersectedExceptions = new List<DateTimeInterval>();

                    foreach (var exception in calendar.WorkingExceptions)
                    {
                        if (exception.StartDate.HasValue && exception.FinishDate.HasValue)
                        {
                            var compareResult = workingInterval.Compare(exception.StartDate.Value, exception.FinishDate.Value);
                            switch (compareResult)
                            {
                                case Interposition.Inside:
                                    skipInterval = true;
                                    break;
                                case Interposition.IntersectRight:
                                case Interposition.IntersectLeft:
                                case Interposition.Contains:
                                    intersectedExceptions.Add(new DateTimeInterval(exception.StartDate.Value, exception.FinishDate.Value));
                                    break;
                            }
                        }

                        if (skipInterval)
                            break;
                    }

                    if (intersectedExceptions.Count > 0)
                    {
                        for (var i = 0; i < intersectedExceptions.Count; i++)
                        {
                            //объеденить пересекающиеся между собой исключения
                            for (var j = i + 1; j < intersectedExceptions.Count; j++)
                            {
                                var uniteResult = intersectedExceptions[i].Unite(intersectedExceptions[j]);
                                if (uniteResult != null)
                                {
                                    //remove united items
                                    intersectedExceptions.Remove(intersectedExceptions[j]);
                                    intersectedExceptions.Remove(intersectedExceptions[i]);
                                    //insert united result instead
                                    intersectedExceptions.Insert(i, uniteResult);
                                }
                            }
                        }

                        // единственное исключение с которым осталось сравнить рабочее время.
                        var resultException = intersectedExceptions.OrderBy(exc => exc.StartDate).First();

                        switch (workingInterval.Compare(resultException))
                        {
                            case Interposition.Inside:
                                skipInterval = true;
                                break;
                            case Interposition.IntersectRight:
                                return resultException.FinishDate;
                            default:
                                return workingInterval.StartDate;
                        }
                    }
                }

                if (skipInterval == false)
                    return date.Date + item.StartDate.Value.TimeOfDay;

                skipInterval = false;
            }

            return null;
        }
Example #5
0
        private static ICalendarWorkingException GetWorkingException(DateTime date, ICalendar calendar, List<ICalendarWorkingInterval> workingIntervals, FrequencyPatternType pattern = FrequencyPatternType.Monthly)
        {
            if (calendar == null || !calendar.WorkingExceptions.Any() || workingIntervals == null || !workingIntervals.Any())
                return null;

            foreach (var item in workingIntervals)
            {
                if (!item.StartDate.HasValue) continue;

                var startDate = new DateTime(date.Year, date.Month, date.Day, item.StartDate.Value.Hour, item.StartDate.Value.Minute, 0);

                var finishDateHour = 0;
                var finishDateMinute = 0;
                if (item.FinishDate.HasValue)
                {
                    finishDateHour = item.FinishDate.Value.Hour;
                    finishDateMinute = item.FinishDate.Value.Minute;
                }
                var finishDate = new DateTime(date.Date.Year, date.Date.Month, date.Date.Day, finishDateHour, finishDateMinute, 0);
                var workingInterval = new DateTimeInterval(startDate, finishDate);

                foreach (var exception in calendar.WorkingExceptions.Where(x => x.StartDate.HasValue && x.FinishDate.HasValue && (x.FinishDate.Value > x.StartDate.Value)))
                {
                    var compareResult = workingInterval.Compare(exception.StartDate.Value, exception.FinishDate.Value);
                    switch (compareResult)
                    {
                        case Interposition.Inside:
                        case Interposition.IntersectRight:
                        case Interposition.IntersectLeft:
                            return pattern == FrequencyPatternType.Daily ? null : exception;
                        case Interposition.Contains:
                            return exception;
                    }
                }
            }

            return null;
        }
        DateTime DateTimeFloorCeiling(DateTime dt, DateTimeInterval interval, int increment, bool isCeiling)
        {
            DateTime dtNew = dt;

            switch (interval)
            {
                case DateTimeInterval.Second:
                    if (dt.Millisecond != 0 || dt.Second % increment != 0)
                    {
                        dtNew = dtNew.AddMilliseconds(-dt.Millisecond);
                        dtNew = dtNew.AddSeconds(-dt.Second % increment);

                        if (isCeiling)
                            dtNew = dtNew.AddSeconds(increment);
                    }
                    break;

                case DateTimeInterval.Minute:
                    if (dt.Millisecond != 0 || dt.Second != 0 || dt.Minute % increment != 0)
                    {
                        dtNew = dtNew.AddMilliseconds(-dt.Millisecond);
                        dtNew = dtNew.AddSeconds(-dt.Second);
                        dtNew = dtNew.AddMinutes(-dt.Minute % increment);

                        if (isCeiling)
                            dtNew = dtNew.AddMinutes(increment);
                    }
                    break;

                case DateTimeInterval.Hour:
                    if (dt.Millisecond != 0 || dt.Second != 0 || dt.Minute != 0 || dt.Hour % increment != 0)
                    {
                        dtNew = dtNew.AddMilliseconds(-dt.Millisecond);
                        dtNew = dtNew.AddSeconds(-dt.Second);
                        dtNew = dtNew.AddMinutes(-dt.Minute);
                        dtNew = dtNew.AddHours(-dt.Hour % increment);

                        if (isCeiling)
                            dtNew = dtNew.AddHours(increment);
                    }
                    break;

                case DateTimeInterval.Day:
                    if (dt.Millisecond != 0 || dt.Second != 0 || dt.Minute != 0 || dt.Hour != 0 || (dt.Day - 1) % increment != 0)
                    {
                        dtNew = dtNew.AddMilliseconds(-dt.Millisecond);
                        dtNew = dtNew.AddSeconds(-dt.Second);
                        dtNew = dtNew.AddMinutes(-dt.Minute);
                        dtNew = dtNew.AddHours(-dt.Hour);
                        dtNew = dtNew.AddDays(-((dt.Day - 1) % increment));

                        if (isCeiling)
                            dtNew = dtNew.AddDays(increment);
                    }
                    break;

                case DateTimeInterval.Month:
                    if (dt.Millisecond != 0 || dt.Second != 0 || dt.Minute != 0 || dt.Hour != 0 || dt.Day != 1 || (dt.Month - 1)% increment != 0)
                    {
                        dtNew = dtNew.AddMilliseconds(-dt.Millisecond);
                        dtNew = dtNew.AddSeconds(-dt.Second);
                        dtNew = dtNew.AddMinutes(-dt.Minute);
                        dtNew = dtNew.AddHours(-dt.Hour);
                        dtNew = dtNew.AddDays(-dt.Day + 1);
                        dtNew = dtNew.AddMonths(-((dt.Month - 1) % increment));

                        if (isCeiling)
                            dtNew = dtNew.AddMonths(increment);
                    }
                    break;

                case DateTimeInterval.Year:
                    if (dt.Millisecond != 0 || dt.Second != 0 || dt.Minute != 0 || dt.Hour != 0 || dt.Day != 1 || dt.Month != 1 || dt.Year % increment != 0)
                    {
                        dtNew = dtNew.AddMilliseconds(-dt.Millisecond);
                        dtNew = dtNew.AddSeconds(-dt.Second);
                        dtNew = dtNew.AddMinutes(-dt.Minute);
                        dtNew = dtNew.AddHours(-dt.Hour);
                        dtNew = dtNew.AddDays(-dt.Day + 1);
                        dtNew = dtNew.AddMonths(-dt.Month + 1);
                        dtNew = dtNew.AddYears(-(dt.Year % increment));

                        if (isCeiling)
                            dtNew = dtNew.AddYears(increment);
                    }
                    break;
            }
            return dtNew;
        }
 DateTime DateTimeCeiling(DateTime dt, DateTimeInterval interval, int increment)
 {
     return DateTimeFloorCeiling(dt, interval, increment, true);
 }
 DateTime AddIncrement(DateTime dt, DateTimeInterval interval, int increment)
 {
     DateTime dtNew = dt.AddSeconds(1);
     return DateTimeCeiling(dt.AddMilliseconds(500), interval, increment);
 }
        protected override void CalculateAxisItems(Type propertyType, ref double minValue, ref double maxValue)
        {
            if (propertyType != typeof(DateTime))
                throw new NotImplementedException("AdaptableDateTimeAxis is only for a DateTime axis.");

            TestIncrementCollection(SecondIncrements, "Year");
            TestIncrementCollection(MinuteIncrements, "Minute");
            TestIncrementCollection(HourIncrements, "Hour");
            TestIncrementCollection(DayIncrements, "Day");
            TestIncrementCollection(MonthIncrements, "Month");
            TestIncrementCollection(YearIncrements, "Year");

            if (MaximumItems < 2)
                throw new ArgumentException("MaximumItems must be at least 2");

            if (minValue == maxValue)
            {
                minValue -= ConvertToDouble(TimeSpan.FromSeconds(1));
                maxValue += ConvertToDouble(TimeSpan.FromSeconds(1));
            }

            DateTime minDateTime = (DateTime)ConvertFromDouble(minValue, propertyType);
            DateTime maxDateTime = (DateTime)ConvertFromDouble(maxValue, propertyType);

            Int32Collection[] incrementsArray = { SecondIncrements, MinuteIncrements, HourIncrements, 
                                                  DayIncrements, MonthIncrements, YearIncrements };
            int incrementsIndex = 0;
            int incrementMultiplier = 1;
            DateTimeInterval interval = DateTimeInterval.Second;

            while (true)
            {
                Int32Collection increments = incrementsArray[(int)interval];
                int increment = incrementMultiplier * increments[incrementsIndex];

                DateTime minAxisDateTime = DateTimeFloor(minDateTime, interval, increment); 
                DateTime maxAxisDateTime = DateTimeCeiling(maxDateTime, interval, increment);
                DateTime dtTick = minAxisDateTime;
                int count = 1;

                while (true)
                {
                    dtTick = AddIncrement(dtTick, interval, increment);
                    count++;

                    if (dtTick >= maxAxisDateTime)
                        break;

                    if (count > MaximumItems)
                        break;
                }

                if (count <= MaximumItems)
                {
                    minValue = ConvertToDouble(minAxisDateTime);
                    maxValue = ConvertToDouble(maxAxisDateTime);
                    dtTick = minAxisDateTime;

                    for (int i = 0; i < count; i++)
                    {
                        AxisItem axisItem = new AxisItem()
                        {
                            Item = dtTick,
                            Offset = Length * (ConvertToDouble(dtTick) - minValue) / (maxValue - minValue)
                        };

                        AxisItems.Add(axisItem);
                        dtTick = AddIncrement(dtTick, interval, increment);
                    }
                    DateTimeInterval = interval;
                    break;
                }

                if (incrementsIndex < increments.Count - 1)
                {
                    incrementsIndex++;
                }
                else
                {
                    incrementsIndex = 0;

                    if (interval != DateTimeInterval.Year)
                        interval++;
                    else
                        incrementMultiplier *= 10;
                }
            }
        }
Example #10
0
        public static DataTable SampleDataByInterval(DataView sourceView, string[] columns, string samplingField, int intervalValue, DateTimeInterval intervalType)
        {
            DataTable newTable = new DataTable();
            for (int i = 0; i < columns.Length; i++)
                newTable.Columns.Add(new DataColumn(columns[i], sourceView.Table.Columns[columns[i]].DataType));

            DateTime lastValue;
            if (sourceView.Count > 0)
            {
                //Add the first row
                lastValue = (DateTime)sourceView[0][samplingField];
                DataRow newRow = newTable.NewRow();
                for (int i = 0; i < columns.Length; i++)
                    newRow[columns[i]] = sourceView[0][columns[i]];
                newTable.Rows.Add(newRow);

                //Add all rows
                for (int i = 1; i < sourceView.Count; i++)
                {
                    DataRow dr = sourceView[i].Row;
                    DateTime currentValue = (DateTime)dr[samplingField];
                    int currentInterval;

                    if (intervalType == DateTimeInterval.Months)
                        currentInterval = DateTimeHelper.MonthsDifference(currentValue, lastValue);
                    else
                        throw new ApplicationException("Interval not supported");

                    if (currentInterval >= intervalValue || (i + 1) == sourceView.Count)
                    {
                        if (intervalType == DateTimeInterval.Months)
                            lastValue = lastValue.AddMonths(intervalValue);
                        else
                            throw new ApplicationException("Interval not supported");

                        newRow = newTable.NewRow();
                        for (int j = 0; j < columns.Length; j++)
                            newRow[columns[j]] = dr[columns[j]];
                        newTable.Rows.Add(newRow);
                    }
                }
            }

            return newTable;
        }
 public void InsertDateTimeInterval(DateTimeInterval dateTimeImterval)
 {
     db.DateTimeIntervals.AddObject(dateTimeImterval);
 }