コード例 #1
0
        public static string GetDateTimePartToggleButtonName(this DateTimePart dateTimePart)
        {
            switch (dateTimePart)
            {
            case DateTimePart.Day:
                return("ToggleButtonD");

            case DateTimePart.Month:
                return("ToggleButtonMo");

            case DateTimePart.Year:
                return("ToggleButtonY");

            case DateTimePart.Hour:
                return("ToggleButtonH");

            case DateTimePart.Minute:
                return("ToggleButtonM");

            case DateTimePart.Second:
                return("ToggleButtonS");

            default:
                throw new InvalidOperationException();
            }
        }
コード例 #2
0
        public static string ToChinese(DateTime thisDateTime, DateTimePart parts)
        {
            string s1 = string.Empty, s2 = string.Empty;

            if ((parts & DateTimePart.Year) == DateTimePart.Year)
            {
                s1 += thisDateTime.Year.ToString() + "��";
            }
            if ((parts & DateTimePart.Month) == DateTimePart.Month)
            {
                s1 += thisDateTime.Month.ToString() + "��";
            }
            if ((parts & DateTimePart.Day) == DateTimePart.Day)
            {
                s1 += thisDateTime.Day.ToString() + "��";
            }

            if ((parts & DateTimePart.Hour) == DateTimePart.Hour)
            {
                s2 += thisDateTime.Hour.ToString() + "ʱ";
            }
            if ((parts & DateTimePart.Minute) == DateTimePart.Minute)
            {
                s2 += thisDateTime.Minute.ToString() + "��";
            }
            if ((parts & DateTimePart.Second) == DateTimePart.Second)
            {
                s2 += thisDateTime.Second.ToString() + "��";
            }

            return MergeDateAndTime(s1, s2, " ");
        }
コード例 #3
0
 public DateTimePartHelper(DateTime dateTime, DateTimePart dateTimePart, NumericTextBox textBox, ToggleButton activeToggleButton)
 {
     _dateTime = dateTime;
     _textBox = textBox;
     _toggleButton = activeToggleButton;
     _dateTimePart = dateTimePart;
 }
コード例 #4
0
        public static string GetDateTimePartName(this DateTimePart dateTimePart)
        {
            switch (dateTimePart)
            {
            case DateTimePart.Day:
                return("NumericTBDay");

            case DateTimePart.Month:
                return("NumericTBMonth");

            case DateTimePart.Year:
                return("NumericTBYear");

            case DateTimePart.Hour:
                return("NumericTBHour");

            case DateTimePart.Minute:
                return("NumericTBMinute");

            case DateTimePart.Second:
                return("NumericTBSecond");

            default:
                throw new InvalidOperationException();
            }
        }
コード例 #5
0
        public static long Diff(this DateTime t1, DateTime t2, DateTimePart part)
        {
            switch (part)
            {
            case DateTimePart.Year:
                return(t1.Year - t2.Year);

            case DateTimePart.Month:
                return((t1.Year - t2.Year) * 12 + (t1.Month - t2.Month));

            case DateTimePart.Day:
                return(t1.Ticks / TimeSpan.TicksPerDay - t2.Ticks / TimeSpan.TicksPerDay);

            case DateTimePart.Hour:
                return(t1.Ticks / TimeSpan.TicksPerHour - t2.Ticks / TimeSpan.TicksPerHour);

            case DateTimePart.Minute:
                return(t1.Ticks / TimeSpan.TicksPerMinute - t2.Ticks / TimeSpan.TicksPerMinute);

            case DateTimePart.Millisecond:
                return(t1.Ticks / TimeSpan.TicksPerMillisecond - t2.Ticks / TimeSpan.TicksPerMillisecond);

            default:
            case DateTimePart.Tick:
                return(t1.Ticks - t2.Ticks);
            }
        }
        /// <summary>
        ///   Returns part of the <see cref="DateTime" /> object.
        /// </summary>
        /// <param name="source">The <see cref="DateTime" /> to retrieve a part from.</param>
        /// <param name="part">Which part of the <see cref="DateTime" /> to retrieve.</param>
        public static double GetDateTimePart(this DateTime source, DateTimePart part)
        {
            switch (part)
            {
            case DateTimePart.Day:
                return(source.Day);

            case DateTimePart.Hour:
                return(source.Hour);

            case DateTimePart.Millisecond:
                return(source.Millisecond);

            case DateTimePart.Minute:
                return(source.Minute);

            case DateTimePart.Month:
                return(source.Month);

            case DateTimePart.Second:
                return(source.Second);

            case DateTimePart.Year:
                return(source.Year);
            }

            throw new NotSupportedException();
        }
コード例 #7
0
 public DateTimePartHelper(DateTime dateTime, DateTimePart dateTimePart, NumericTextBox textBox, ToggleButton activeToggleButton)
 {
     _dateTime     = dateTime;
     _textBox      = textBox;
     _toggleButton = activeToggleButton;
     _dateTimePart = dateTimePart;
 }
コード例 #8
0
        /// <summary>
        /// Floors the provided date and time to highest step value, which are the same or lower. Flooring is defined as taking a value and quantifying it into
        /// one of the defined step, where the selected step is the one closest, below the value. E.g. if the value PI is given and it should be quantified
        /// into eights (1/8) the steps will be all numbers, which can be divided by 8 without any remainders, hence the floor of PI (3.141) will be 3.125.
        /// The same goes for flooring dates and times, which is what this function supports; however, the you must specify what part of the date time you
        /// would like to quantify, and by which value.
        /// E.g.given the date time 1966-07-14 13:46:15:316. Let’s say we want to quantify it into steps of 10 minutes.This will give the value
        /// 1966-07-1 13:40:00:000, hence this is the closest value that are less or equal to the original date and time and which can be divided by 10 minutes
        /// without any remainder.
        /// To utilize the method, you must therefore provide the source date time value, specify what part of the date time you would like to define the
        /// quantification for, and the step size for that part.
        /// </summary>
        /// <param name="source">The date time to be floored.</param>
        /// <param name="part">The part of the date time, that the <see cref="stepSize"/> is defined on.</param>
        /// <param name="stepSize">The step size of the defined <see cref="part"/>.</param>
        /// <returns>The floored value of <see cref="source"/>.</returns>
        public static DateTime FloorDateTime(DateTime source, DateTimePart part, int stepSize = 1)
        {
            switch (part)
            {
            case DateTimePart.Milliseconds:
                return(new DateTime(source.Year, source.Month, source.Day, source.Hour, source.Minute, source.Second, MathUtilities.Floor(source.Millisecond, stepSize)));

            case DateTimePart.Seconds:
                return(new DateTime(source.Year, source.Month, source.Day, source.Hour, source.Minute, MathUtilities.Floor(source.Second, stepSize), 0));

            case DateTimePart.Minutes:
                return(new DateTime(source.Year, source.Month, source.Day, source.Hour, MathUtilities.Floor(source.Minute, stepSize), 0, 0));

            case DateTimePart.Hours:
                return(new DateTime(source.Year, source.Month, source.Day, MathUtilities.Floor(source.Hour, stepSize), 0, 0, 0));

            case DateTimePart.Days:
                return(new DateTime(source.Year, source.Month, 1 + MathUtilities.Floor(source.Day - 1, stepSize), 0, 0, 0, 0));

            case DateTimePart.Months:
                return(new DateTime(source.Year, 1 + MathUtilities.Floor(source.Month - 1, stepSize), 1, 0, 0, 0));

            case DateTimePart.Years:
                return(new DateTime(MathUtilities.Floor(source.Year, stepSize), 1, 1, 0, 0, 0));

            case DateTimePart.Weeks:
                throw new NotSupportedException($"FloorDateTime method does not support flooring of DateTime based on DateTimePart.Weeks");

            default:
                var partOptions = string.Join(", ", Enum.GetNames(typeof(DateTimePart)));
                throw new ArgumentException($"The provided part must be a member of the enumeration DateTimePart. Options are {partOptions}.");
            }
        }
コード例 #9
0
ファイル: DateTimeExtensions.cs プロジェクト: cairabbit/daf
 public static DateTime Interval(this DateTime date, DateTimePart? intervalType, int? intervalVal)
 {
     if (intervalType.HasValue && intervalVal.HasValue)
     {
         switch (intervalType.Value)
         {
             case DateTimePart.Year:
                 return date.AddYears(intervalVal.Value);
             case DateTimePart.Month:
                 return date.AddMonths(intervalVal.Value);
             case DateTimePart.Day:
                 return date.AddDays((double)intervalVal.Value);
             case DateTimePart.Hour:
                 return date.AddHours((double)intervalVal.Value);
             case DateTimePart.Munite:
                 return date.AddMinutes((double)intervalVal.Value);
             case DateTimePart.Second:
                 return date.AddSeconds((double)intervalVal.Value);
             case DateTimePart.Week:
                 return date.AddDays((double)intervalVal.Value * 7);
             case DateTimePart.Quarter:
                 return date.AddMonths(intervalVal.Value * 3);
         }
     }
     return date;
 }
コード例 #10
0
ファイル: DateHelper.cs プロジェクト: yifeidong/MasterChief
        /// <summary>
        /// 日期差计算
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="part">时间差枚举</param>
        /// <returns>时间差</returns>
        public static int GetDateDiff(this DateTime startTime, DateTime endTime, DateTimePart part)
        {
            int result = 0;

            switch (part)
            {
            case DateTimePart.year:
                result = endTime.Year - startTime.Year;
                break;

            case DateTimePart.month:
                result = (endTime.Year - startTime.Year) * 12 + (endTime.Month - startTime.Month);
                break;

            case DateTimePart.day:
                result = (int)(endTime - startTime).TotalDays;
                break;

            case DateTimePart.hour:
                result = (int)(endTime - startTime).TotalHours;
                break;

            case DateTimePart.minute:
                result = (int)(endTime - startTime).TotalMinutes;
                break;

            case DateTimePart.second:
                result = (int)(endTime - startTime).TotalSeconds;
                break;
            }

            return(result);
        }
コード例 #11
0
 public static string ToChinese(DateTime thisDateTime, DateTimePart parts, string defaultValue)
 {
     if (thisDateTime == DateTime.MinValue)
     {
         return defaultValue;
     }
     return ToChinese(thisDateTime, parts);
 }
コード例 #12
0
        public void Truncate_Cases_ReturnsExpectedResult(DateTime input, DateTimePart mode, DateTime expected)
        {
            // act
            var result = input.Truncate(mode);

            // assert
            result.Should().Be(expected);
        }
コード例 #13
0
 public DateTimePartHelper(DateTime dateTime, DateTimePart dateTimePart, DateTimeFormatInfo dateTimeFormatInfo, TextBox textBox, ToggleButton activeToggleButton)
 {
     _dateTime           = dateTime;
     _textBox            = textBox;
     _toggleButton       = activeToggleButton;
     _dateTimePart       = dateTimePart;
     _dateTimeFormatInfo = dateTimeFormatInfo;
 }
コード例 #14
0
        /// <summary>
        ///   Get the <see cref="TimeSpan" /> constructor which uses a certain amount of time units to initialize the <see cref="TimeSpan" />.
        /// </summary>
        /// <param name="unit">The units to use to initialize the <see cref="TimeSpan" /></param>
        /// <returns>A function which constructs a <see cref="TimeSpan" /> from an amount of time units.</returns>
        public static Func <double, TimeSpan> GetTimeSpanConstructor(DateTimePart unit)
        {
            if (TimeSpanConstructors.ContainsKey(unit))
            {
                return(TimeSpanConstructors[unit]);
            }

            throw new NotSupportedException("A TimeSpan can't be constructed from " + unit + ".");
        }
コード例 #15
0
        protected override void OnCurrentDateTimePartChanged(DateTimePart oldValue, DateTimePart newValue)
        {
            if (this.CurrentDateTimePart == DateTimePart.Millisecond && this.FractionalSecondsDigitsCount < 3)
            {
                this.SetCurrentValue(TimeSpanUpDown.FractionalSecondsDigitsCountProperty, _defaultFractionalSecondsDigitsCount);
            }

            base.OnCurrentDateTimePartChanged(oldValue, newValue);
        }
コード例 #16
0
 /// <summary>
 ///   Returns a new <see cref="DateTime" /> object which is rounded down to the specified <paramref name="part" />.
 /// </summary>
 /// <param name="source">The <see cref="DateTime" /> to round down.</param>
 /// <param name="part">The part to round down to.</param>
 public static DateTime Round( this DateTime source, DateTimePart part )
 {
     return new DateTime(
         source.Year,
         part >= DateTimePart.Month ? source.Month : 1,
         part >= DateTimePart.Day ? source.Day : 1,
         part >= DateTimePart.Hour ? source.Hour : 0,
         part >= DateTimePart.Minute ? source.Minute : 0,
         part >= DateTimePart.Second ? source.Second : 0,
         part >= DateTimePart.Millisecond ? source.Millisecond : 0 );
 }
コード例 #17
0
        private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)
        {
            _activeDateTimePart = (DateTimePart)((ToggleButton)sender).Tag;

            var activeNumericTextBox = (NumericTextBox)FindName(_activeDateTimePart.GetDateTimePartName());
            var activeToggleButton   = (ToggleButton)FindName(_activeDateTimePart.GetDateTimePartToggleButtonName());

            var dateTimePartHelper = new DateTimePartHelper(Value, _activeDateTimePart, activeNumericTextBox, activeToggleButton);

            dateTimePartHelper.CreatePopup();
        }
 /// <summary>
 ///   Returns a new <see cref="DateTime" /> object which is rounded down to the specified <paramref name="part" />.
 /// </summary>
 /// <param name="source">The <see cref="DateTime" /> to round down.</param>
 /// <param name="part">The part to round down to.</param>
 public static DateTime Round(this DateTime source, DateTimePart part)
 {
     return(new DateTime(
                source.Year,
                part >= DateTimePart.Month ? source.Month : 1,
                part >= DateTimePart.Day ? source.Day : 1,
                part >= DateTimePart.Hour ? source.Hour : 0,
                part >= DateTimePart.Minute ? source.Minute : 0,
                part >= DateTimePart.Second ? source.Second : 0,
                part >= DateTimePart.Millisecond ? source.Millisecond : 0));
 }
コード例 #19
0
 /// <summary>
 /// Returns a new <see cref="DateTime" /> that represents a fraction of this <see cref="DateTime" /> value specified by the <paramref name="part" /> parameter.
 /// </summary>
 /// <param name="dateTime">The <see cref="DateTime" /> value to be stripped.</param>
 /// <param name="part">The <see cref="DateTimePart" /> specifying, which fraction of <paramref name="dateTime" /> is returned.</param>
 /// <returns>
 /// A new <see cref="DateTime" /> that represents a fraction of this <see cref="DateTime" /> value specified by the <paramref name="part" /> parameter.
 /// </returns>
 public static DateTime GetPart(this DateTime dateTime, DateTimePart part)
 {
     return(part switch
     {
         DateTimePart.Full => dateTime,
         DateTimePart.DateTimeWithSeconds => new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Kind),
         DateTimePart.DateTime => new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, 0, dateTime.Kind),
         DateTimePart.Date => new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, dateTime.Kind),
         DateTimePart.YearMonth => new DateTime(dateTime.Year, dateTime.Month, 1, 0, 0, 0, dateTime.Kind),
         DateTimePart.Year => new DateTime(dateTime.Year, 1, 1, 0, 0, 0, dateTime.Kind),
         _ => throw Throw.InvalidEnumArgument(nameof(part), part)
     });
コード例 #20
0
        private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)
        {
            _activeDateTimePart = (DateTimePart)((ToggleButton)sender).Tag;

            var activeTextBox      = (TextBox)FindName(_activeDateTimePart.GetDateTimePartName());
            var activeToggleButton = (ToggleButton)FindName(_activeDateTimePart.GetDateTimePartToggleButtonName());

            var dateTime           = Value == null ? _todayValue : Value.Value;
            var dateTimePartHelper = new DateTimePartHelper(dateTime, _activeDateTimePart, _formatInfo, activeTextBox, activeToggleButton);

            dateTimePartHelper.CreatePopup();
        }
コード例 #21
0
        private static string DateAdd(DateTimePart part, string dt, string n)
        {
            switch (part)
            {
            case DateTimePart.Month:
            case DateTimePart.Year:
                return(string.Format("(add_months({0}, {1}))", dt, part == DateTimePart.Month ? n : "12 * " + n));

            default:
                return(string.Format("({0} + numtodsinterval({1}, '{2}'))", dt, n, part.ToString().ToUpper()));
            }
        }
コード例 #22
0
ファイル: DateTimeExtensions.cs プロジェクト: Michmcb/CsExt
 /// <summary>
 /// Returns a truncated instance so that it is only accurate to the part specified by <paramref name="truncateTo"/>.
 /// For example, if <paramref name="truncateTo"/> is Minute, then Seconds and Milliseconds are set to zero.
 /// Truncating days or months will cause them to be truncated to 1.
 /// </summary>
 public static DateTime Truncate(this DateTime dt, DateTimePart truncateTo)
 {
     return(truncateTo switch
     {
         DateTimePart.Year => new(dt.Year, 1, 1, 0, 0, 0, dt.Kind),
         DateTimePart.Month => new(dt.Year, dt.Month, 1, 0, 0, 0, dt.Kind),
         DateTimePart.Day => dt.AddTicks(-dt.Ticks % TimeSpan.TicksPerDay),
         DateTimePart.Hour => dt.AddTicks(-dt.Ticks % TimeSpan.TicksPerHour),
         DateTimePart.Minute => dt.AddTicks(-dt.Ticks % TimeSpan.TicksPerMinute),
         DateTimePart.Second => dt.AddTicks(-dt.Ticks % TimeSpan.TicksPerSecond),
         DateTimePart.Millisecond => dt.AddTicks(-dt.Ticks % TimeSpan.TicksPerMillisecond),
         _ => throw new ArgumentOutOfRangeException(nameof(truncateTo), "Parameter was not a valid value for DateTimePart"),
     });
コード例 #23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SchedulePart"/> class.
 /// </summary>
 /// <param name="valueText">The text that specifies the values for the <see cref="SchedulePart"/> object.</param>
 /// <param name="dateTimePart">The <see cref="DateTimePart"/> that the <see cref="SchedulePart"/> object represents.</param>
 public SchedulePart(string valueText, DateTimePart dateTimePart)
 {
     if (ValidateAndPopulate(valueText, dateTimePart))
     {
         // The text provided for populating the values is valid according to the specified date-time part.
         m_valueText    = valueText;
         m_dateTimePart = dateTimePart;
     }
     else
     {
         throw new ArgumentException("Text is not valid for " + dateTimePart + " schedule part");
     }
 }
コード例 #24
0
ファイル: SchedulePart.cs プロジェクト: avs009/gsf
 public SchedulePart(string text, DateTimePart dateTimePart)
 {
     if (ValidateAndPopulate(text, dateTimePart))
     {
         // The text provided for populating the values is valid according to the specified date-time part.
         m_text = text;
         m_dateTimePart = dateTimePart;
     }
     else
     {
         throw new ArgumentException("Text is not valid for " + dateTimePart + " schedule part.");
     }
 }
コード例 #25
0
        public List<KeyValuePair<string, string>> GetSuggestionList(DateTime dateTime, DateTimePart editablePart)
        {
            switch (editablePart)
            {
                case DateTimePart.Day:
                    var days = new List<KeyValuePair<string, string>>();
                    var daysInMonth = DateTime.DaysInMonth(dateTime.Year, dateTime.Month);
                    for (var i = 1; i <= daysInMonth; i++)
                    {
                        var day = new DateTime(dateTime.Year, dateTime.Month, i).ToString("d ddd", CultureInfo.InvariantCulture);
                        days.Add(CreateItem(i.ToString(), day));
                    }
                    return days;

                case DateTimePart.Month:
                    var months = new List<KeyValuePair<string, string>>();
                    for (var i = 1; i <= 12; i++)
                    {
                        var month = new DateTime(dateTime.Year, i, 1).ToString("M MMM", CultureInfo.InvariantCulture);
                        months.Add(CreateItem(i.ToString(), month));
                    }
                    return months;

                case DateTimePart.Hour:
                    var hours = new List<KeyValuePair<string, string>>();
                    for (var i = 0; i < 24; i++)
                    {
                        hours.Add(i < 10 ? CreateItem(i.ToString(), ("  " + i)) : CreateItem(i.ToString(), i.ToString()));
                    }
                    return hours;

                case DateTimePart.Minute:
                    var minutes = new List<KeyValuePair<string, string>>();
                    for (var i = 0; i < 60; i++)
                    {
                        minutes.Add(CreateItem(i.ToString("00"), i.ToString("00")));
                    }
                    return minutes;

                case DateTimePart.Second:
                    var seconds = new List<KeyValuePair<string, string>>();
                    for (var i = 0; i < 60; i++)
                    {
                        seconds.Add(CreateItem(i.ToString("00"), i.ToString("00")));
                    }
                    return seconds;

                default:
                    throw new InvalidOperationException();
            }
        }
コード例 #26
0
        private static string Extract(DateTimePart part, string dt)
        {
            switch (part)
            {
            case DateTimePart.Hour:
                return(string.Format("to_number(to_char({0}, 'HH24'))", dt));

            case DateTimePart.WeekDay:
                return(string.Format("(to_number(to_char({0}, 'D')) - 1)", dt));

            default:
                return(string.Format("extract({0} FROM {1})", part.ToString().ToLower(), dt));
            }
        }
コード例 #27
0
ファイル: SchedulePart.cs プロジェクト: gemstone/common
        /// <summary>
        /// Initializes a new instance of the <see cref="SchedulePart"/> class.
        /// </summary>
        /// <param name="valueText">The text that specifies the values for the <see cref="SchedulePart"/> object.</param>
        /// <param name="dateTimePart">The <see cref="DateTimePart"/> that the <see cref="SchedulePart"/> object represents.</param>
        public SchedulePart(string valueText, DateTimePart dateTimePart)
        {
            m_values = new List <int>();

            if (ValidateAndPopulate(valueText, dateTimePart))
            {
                // The text provided for populating the values is valid according to the specified date-time part.
                ValueText    = valueText;
                DateTimePart = dateTimePart;
            }
            else
            {
                throw new ArgumentException($"Text is not valid for {dateTimePart} schedule part");
            }
        }
コード例 #28
0
        /// <summary>
        /// Gibt ein DateTime zurück, bei dem im Vergleich zum eingehenden DateTime informationen weggeschnitten werden.
        /// </summary>
        /// <param name="dt">Das DateTime-Objekt das als Grundlage der Berechnung dient.</param>
        /// <param name="part">Gibt den Teil im DateTime an, bis zu dem reduziert wird.</param>
        /// <returns>Ein reduziertes DateTime</returns>
        public static DateTime Truncate(this DateTime dt, DateTimePart part)
        {
            if (part == DateTimePart.Year)
            {
                return(DateTime.MinValue);
            }

            return(new DateTime
                   (
                       dt.Year,
                       part < DateTimePart.Months ? dt.Month : 1,
                       part < DateTimePart.Days ? dt.Day : 1,
                       part < DateTimePart.Hours ? dt.Hour : 0,
                       part < DateTimePart.Minutes ? dt.Minute : 0,
                       part < DateTimePart.Seconds ? dt.Second : 0,
                       part < DateTimePart.Milliseconds ? dt.Millisecond : 0
                   ));
        }
コード例 #29
0
        protected override void OnCurrentDateTimePartChanged(DateTimePart oldValue, DateTimePart newValue)
        {
            base.OnCurrentDateTimePartChanged(oldValue, newValue);

            switch (newValue)
            {
            case DateTimePart.Hour12:
            case DateTimePart.Hour24:
                this.Step = 1;
                break;

            case DateTimePart.Minute:
                this.Step = Constant.Time.UtcOffsetGranularity.Minutes;
                break;

            default:
                this.Step = 0;
                break;
            }
        }
コード例 #30
0
        /// <summary>
        ///   Returns part of the <see cref="DateTime" /> object.
        /// </summary>
        /// <param name="source">The <see cref="DateTime" /> to retrieve a part from.</param>
        /// <param name="part">Which part of the <see cref="DateTime" /> to retrieve.</param>
        public static double GetDateTimePart( this DateTime source, DateTimePart part )
        {
            switch ( part )
            {
                case DateTimePart.Day:
                    return source.Day;
                case DateTimePart.Hour:
                    return source.Hour;
                case DateTimePart.Millisecond:
                    return source.Millisecond;
                case DateTimePart.Minute:
                    return source.Minute;
                case DateTimePart.Month:
                    return source.Month;
                case DateTimePart.Second:
                    return source.Second;
                case DateTimePart.Year:
                    return source.Year;
            }

            throw new NotSupportedException();
        }
コード例 #31
0
        private static string DateDiff(DateTimePart part, string dt1, string dt2)
        {
            string subtractFormat = "to_date(to_char({0}, '{2}'), '{2}') - to_date(to_char({1}, '{2}'), '{2}')";
            string dayFormat      = "round(" + subtractFormat + ")";
            string format         = "round((" + subtractFormat + ") * {3})";

            switch (part)
            {
            case DateTimePart.Day:
                return(string.Format(dayFormat, dt2, dt1, "YYYY-MM-DD"));

            case DateTimePart.Hour:
                return(string.Format(format, dt2, dt1, "YYYY-MM-DD HH24", 24));

            case DateTimePart.Minute:
                return(string.Format(format, dt2, dt1, "YYYY-MM-DD HH24:MI", 1440));

            case DateTimePart.Second:
                return(string.Format(format, dt2, dt1, "YYYY-MM-DD HH24:MI:SS", 86400));

            default:
                throw new NotImplementedException();
            }
        }
コード例 #32
0
        public static string ToStandard(DateTime thisDateTime, DateTimePart parts)
        {
            string s1 = string.Empty, s2 = string.Empty;

            if ((parts & DateTimePart.Year) == DateTimePart.Year)
            {
                s1 += thisDateTime.Year.ToString();
            }
            if ((parts & DateTimePart.Month) == DateTimePart.Month)
            {
                s1 = StringUtil.MergeIfExists(s1, "-", true);
                s1 += thisDateTime.Month.ToString();
            }
            if ((parts & DateTimePart.Day) == DateTimePart.Day)
            {
                s1 = StringUtil.MergeIfExists(s1, "-", true);
                s1 += thisDateTime.Day.ToString();
            }

            if ((parts & DateTimePart.Hour) == DateTimePart.Hour)
            {
                s2 += thisDateTime.Hour.ToString();
            }
            if ((parts & DateTimePart.Minute) == DateTimePart.Minute)
            {
                s2 = StringUtil.MergeIfExists(s2, ":", true);
                s2 += thisDateTime.Minute.ToString();
            }
            if ((parts & DateTimePart.Second) == DateTimePart.Second)
            {
                s2 = StringUtil.MergeIfExists(s2, ":", true);
                s2 += thisDateTime.Second.ToString();
            }

            return MergeDateAndTime(s1, s2, " ");
        }
コード例 #33
0
		internal void SetPart (int value, DateTimePart dt_part)
		{
			SetPart (value, dt_part, false);
		}
コード例 #34
0
ファイル: SchemaTypes.cs プロジェクト: paladin74/Dapple
        protected bool ParseDateTime(string s, DateTimePart part)
        {
            ParseContext context = new ParseContext(s);

            bool bDatePart = ( part & DateTimePart.Date ) != 0;
            bool bTimePart = ( part & DateTimePart.Time ) != 0;

            int year = 0, month = 0, day = 0;
            int hour = 0, minute = 0;
            double second = 0;

            eTZ = ETZ.Missing;
            offsetTZ = 0;

            if ( bDatePart )
            {
                // parse date
                bool bNegative = context.CheckAndAdvance( '-' );

                if ( (part & DateTimePart.Year ) != 0 )
                {
                    int digits = 0;
                    int temp = 0;
                    while ( context.ReadDigitAndAdvance( ref temp, 1, 9 ) )
                    {
                        year = year * 10 + temp;
                        digits += 1;
                        temp = 0;
                        if (digits >= 8) // overflow
                            return false;
                    }
                    if ( digits < 4 ) // invalid.
                        return false;
                    if ( digits > 4 && year < 10000 )
                        return false;
                    if (bNegative)
                        year = -year;
                }

                if ( (part & ( DateTimePart.Month | DateTimePart.Day )) != 0 )
                {
                    if ( !context.CheckAndAdvance( '-' ) ) return false;

                    if ( ( part & DateTimePart.Month ) != 0 )
                    {
                        if ( !context.ReadDigitAndAdvance( ref month, 10, 1 ) ) return false;
                        if ( !context.ReadDigitAndAdvance( ref month, 1, month < 10 ? 9 : 2 ) ) return false;
                        if ( month == 0 ) return false;
                    }

                    if ( ( part & DateTimePart.Day ) != 0 )
                    {
                        if ( !context.CheckAndAdvance(  '-') ) return false;

                        int maxFirstDigit = month != 2 ? 3 : 2;

                        // complicate things by making them complicated.
                        if ( !context.ReadDigitAndAdvance( ref day, 10, maxFirstDigit ) ) return false;
                        if ( !context.ReadDigitAndAdvance( ref day, 1, 9 ) ) return false;
                        if ( day == 0 || day > 31 ) return false;

                        if ( ( part & DateTimePart.Month ) != 0 )
                        {
                            bool b1 = month <= 7;
                            bool b2 = ( month & 1 ) == 0;

                            // month 1, 3, 5, 7, 8, 10, 12
                            if ( b1 == b2 && day > 30 )
                                return false;

                            // february.
                            if ( month == 2 && day > 29 )
                                return false;

                            // leap years.
                            if ( month == 2 && ( part & DateTimePart.Year ) != 0 &&
                                ( year % 4 != 0 || year % 100 == 0 ) && year % 400 != 0 &&
                                day > 28 )
                                return false;
                        }
                    }
                }

                if ( bTimePart )
                {
                    // a 'T' must follow
                    if ( !context.CheckAndAdvance( 'T') ) return false;
                }
            }

            if ( bTimePart )
            {
                // check format here

                // hour from 0 to 2
                if ( !context.ReadDigitAndAdvance( ref hour, 10, 2 ) ) return false;
                if ( !context.ReadDigitAndAdvance( ref hour, 1, hour < 20 ? 9 : 4 ) ) return false;
                if ( !context.CheckAndAdvance( ':' ) ) return false;
                int maxFirstDigit = hour == 24 ? 0 : 5;
                int maxSecondDigit = hour == 24 ? 0 : 9;
                if ( !context.ReadDigitAndAdvance( ref minute, 10, maxFirstDigit ) ) return false;
                if ( !context.ReadDigitAndAdvance( ref minute, 1, maxSecondDigit ) ) return false;
                if ( !context.CheckAndAdvance( ':' ) ) return false;
                int secondInt = 0;
                if ( !context.ReadDigitAndAdvance( ref secondInt, 10, maxFirstDigit ) ) return false;
                if ( !context.ReadDigitAndAdvance( ref secondInt, 1, maxSecondDigit ) ) return false;

                second = secondInt;

                if ( context.CheckAndAdvance( '.' ) )
                {
                    // fraction. do whatever seems fit.
                    int val = 0;
                    int digits = 0;
                    while ( context.ReadDigitAndAdvance( ref val, 1, 9) )
                    {
                        val *= 10;
                        digits += 1;
                        if ( digits >= 8 ) // precision loss - ignore
                            break;
                    }

                    if ( digits == 0 )
                        return false;

                    second += val * Math.Pow( 10.0, -digits - 1 );

                    // skip any further digits.
                    while ( context.ReadDigitAndAdvance( ref val, 0, 9) )
                        ;
                }
            }

            // timezone
            if ( context.CheckAndAdvance('Z') )
            {
                // timezone specified, it is UTC.
                eTZ = ETZ.UTC;
                offsetTZ = 0;
            }
            else if ( context.Check('+') || context.Check('-' ) )
            {
                // timezone offset, in hour:minute format
                bool bNegative = context.Check('-');
                context.Advance();

                // do not check the hour part, for those who are obscure.
                int temp = 0;
                if ( !context.ReadDigitAndAdvance( ref temp, 600, 9 ) ) return false;
                if ( !context.ReadDigitAndAdvance( ref temp, 60, 9 ) ) return false;
                if ( !context.CheckAndAdvance( ':' ) ) return false;
                if ( !context.ReadDigitAndAdvance( ref temp, 10, 5 ) ) return false;
                if ( !context.ReadDigitAndAdvance( ref temp, 1, 9 ) ) return false;

                eTZ = ETZ.Offset;
                offsetTZ = bNegative ? -temp : temp;
            }

            if ( context.IsValid() )
                return false;

            // C# specific
            if (year <= 0) year = 1;
            if (month == 0) month = 1;
            bool badjust = false;
            if (hour == 24)
            {
                hour = 0;
                badjust = true;
            }
            if (day == 0) day = 1;
            try {
                myValue = new System.DateTime(year, month, day, hour, minute, (int)second,
                    (int)(second * 1000) % 1000);
                if (badjust)
                    myValue.AddDays(1);
            }
            catch
            {
                return false;
            }
            return true;
        }
コード例 #35
0
 internal DateTimeInfo GetDateTimeInfo(DateTimePart part)
 {
     return(_dateTimeInfoList.FirstOrDefault((info) => info.Type == part));
 }
コード例 #36
0
 protected virtual void OnCurrentDateTimePartChanged(DateTimePart oldValue, DateTimePart newValue)
 {
     this.Select(this.GetDateTimeInfo(newValue));
 }
コード例 #37
0
		// set the specified part of the date to the specified value
		internal void SetPart (int value, DateTimePart dt_part, bool adjust)
		{
			switch (dt_part)
			{
				case DateTimePart.Seconds:
					if (value == -1)
						value = 59;
					if (value >= 0 && value <= 59)
						Value = new DateTime(Value.Year, Value.Month, Value.Day, Value.Hour, Value.Minute, value, Value.Millisecond);
					break;
				case DateTimePart.Minutes:
					if (value == -1)
						value = 59;
					if (value >= 0 && value <= 59)
						Value = new DateTime(Value.Year, Value.Month, Value.Day, Value.Hour, value, Value.Second, Value.Millisecond);
					break;
				case DateTimePart.AMPMHour:
					if (value == -1)
						value = 23;
					if (value >= 0 && value <= 23) {
						int prev_hour = Value.Hour;
						if ((prev_hour >= 12 && prev_hour <= 23) && value < 12) // Adjust to p.m.
							value += 12;
						Value = new DateTime (Value.Year, Value.Month, Value.Day, value, Value.Minute, Value.Second, Value.Millisecond);
					}
					break;
				case DateTimePart.Hour:
					if (value == -1)
						value = 23;
					if (value >= 0 && value <= 23)
						Value = new DateTime(Value.Year, Value.Month, Value.Day, value, Value.Minute, Value.Second, Value.Millisecond);
					break;
				case DateTimePart.Day:
					int max_days = DateTime.DaysInMonth(Value.Year, Value.Month);
					if (value >= 1 && value <= 31 && value <= max_days)
						Value = new DateTime(Value.Year, Value.Month, value, Value.Hour, Value.Minute, Value.Second, Value.Millisecond);
					break;
				case DateTimePart.Month:
					DateTime date = Value;

					if (adjust) {
						if (value == 0) {
							date = date.AddYears (-1);
							value = 12;
						} else if (value == 13) {
							date = date.AddYears (1);
							value = 1;
						}
					}

					if (value >= 1 && value <= 12) {
						// if we move from say december to november with days on 31, we must
						// remap to the maximum number of days
						int days_in_new_month = DateTime.DaysInMonth (date.Year, value);
						
						if (date.Day > days_in_new_month)
							Value = new DateTime (date.Year, value, days_in_new_month, date.Hour, date.Minute, date.Second, date.Millisecond);
						else
							Value = new DateTime (date.Year, value, date.Day, date.Hour, date.Minute, date.Second, date.Millisecond);
					}
					break;
				case DateTimePart.Year:
					if (value >= min_date.Year && value <= max_date.Year) {
						// if we move to a leap year, the days in month could throw an exception
						int days_in_new_month = DateTime.DaysInMonth (value, Value.Month);
						
						if (Value.Day > days_in_new_month)
							Value = new DateTime (value, Value.Month, days_in_new_month, Value.Hour, Value.Minute, Value.Second, Value.Millisecond);
						else
							Value = new DateTime (value, Value.Month, Value.Day, Value.Hour, Value.Minute, Value.Second, Value.Millisecond);
					}
					break;
			}
		}
コード例 #38
0
 private static string DatePart(DateTimePart part, string dt)
 {
     return(string.Format("datepart({0}, {1})", part.ToString().ToLower(), dt));
 }
コード例 #39
0
			internal PartData(string value, bool is_literal, DateTimePicker owner)
			{
				this.value = value;
				this.is_literal = is_literal;
				this.owner = owner;
				date_time_part = GetDateTimePart (value);
			}
コード例 #40
0
ファイル: SchedulePart.cs プロジェクト: avs009/gsf
        private bool ValidateAndPopulate(string schedulePart, DateTimePart dateTimePart)
        {
            int minValue = 0;
            int maxValue = 0;

            switch (dateTimePart)
            {
                case DateTimePart.Minute:
                    maxValue = 59;
                    break;
                case DateTimePart.Hour:
                    maxValue = 23;
                    break;
                case DateTimePart.Day:
                    minValue = 1;
                    maxValue = 31;
                    break;
                case DateTimePart.Month:
                    minValue = 1;
                    maxValue = 12;
                    break;
                case DateTimePart.DayOfWeek:
                    maxValue = 6;
                    break;
            }

            m_values = new List<int>();

            if (Regex.Match(schedulePart, "^(\\*){1}$").Success)
            {
                // ^(\*){1}$             Matches: *
                m_textSyntax = SchedulePartTextSyntax.Any;
                PopulateValues(minValue, maxValue, 1);

                return true;
            }
            else if (Regex.Match(schedulePart, "^(\\*/\\d+){1}$").Success)
            {
                // ^(\*/\d+){1}$         Matches: */[any digit]
                int interval = Convert.ToInt32(schedulePart.Split('/')[1]);
                if (interval > 0 && interval >= minValue && interval <= maxValue)
                {
                    m_textSyntax = SchedulePartTextSyntax.EveryN;
                    PopulateValues(minValue, maxValue, interval);

                    return true;
                }
            }
            else if (Regex.Match(schedulePart, "^(\\d+\\-\\d+){1}$").Success)
            {
                // ^(\d+\-\d+){1}$       Matches: [any digit]-[any digit]
                string[] range = schedulePart.Split('-');
                int lowRange = Convert.ToInt32(range[0]);
                int highRange = Convert.ToInt32(range[1]);
                if (lowRange < highRange && lowRange >= minValue && highRange <= maxValue)
                {
                    m_textSyntax = SchedulePartTextSyntax.Range;
                    PopulateValues(lowRange, highRange, 1);

                    return true;
                }
            }
            else if (Regex.Match(schedulePart, "^((\\d+,?)+){1}$").Success)
            {
                // ^((\d+,?)+){1}$       Matches: [any digit] AND [any digit], ..., [any digit]
                m_textSyntax = SchedulePartTextSyntax.Specific;

                int value;

                foreach (string part in schedulePart.Split(','))
                {
                    if (int.TryParse(part, out value))
                    {
                        if (!(value >= minValue && value <= maxValue))
                        {
                            return false;
                        }
                        else
                        {
                            if (!m_values.Contains(value))
                                m_values.Add(value);
                        }
                    }
                    else
                    {
                        return false;
                    }
                }

                return true;
            }

            return false;
        }
コード例 #41
0
ファイル: types.cs プロジェクト: Phil-Ruben/Residuals
 public bool ParseDateTime(string s, DateTimePart part)
 {
     return ParseDateTime(s, part, 0);
 }
コード例 #42
0
        private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)
        {
            _activeDateTimePart = (DateTimePart) ((ToggleButton) sender).Tag;

            var activeNumericTextBox = (NumericTextBox)FindName(_activeDateTimePart.GetDateTimePartName());
            var activeToggleButton = (ToggleButton)FindName(_activeDateTimePart.GetDateTimePartToggleButtonName());

            var dateTimePartHelper = new DateTimePartHelper(Value, _activeDateTimePart, activeNumericTextBox, activeToggleButton);
            dateTimePartHelper.CreatePopup();
        }
コード例 #43
0
 private static string DateAdd(DateTimePart part, string dt, string n)
 {
     return(string.Format("dateadd({0}, {1}, {2})", part.ToString().ToLower(), n, dt));
 }
コード例 #44
0
ファイル: RecurDateTime.cs プロジェクト: nhaberl/PDI
        /// <summary>
        /// Compares two <c>RecurDateTime</c> values and returns an integer that indicates their relationship.
        /// This version only compares up to the specified date/time part.
        /// </summary>
        /// <param name="r1">The first date/time</param>
        /// <param name="r2">The second date/time</param>
        /// <param name="part">The part up to which comparisons are made.  Parts smaller than this are ignored.</param>
        /// <returns>Returns -1 if the first instance is less than the second, 0 if they are equal, or 1 if the
        /// first instance is greater than the second.</returns>
        public static int Compare(RecurDateTime r1, RecurDateTime r2, DateTimePart part)
        {
            // Cast as object for null checks or it goes recursive
            if ((object)r1 == null && (object)r2 == null)
            {
                return(0);
            }

            if ((object)r1 != null && (object)r2 == null)
            {
                return(1);
            }

            if ((object)r1 == null && (object)r2 != null)
            {
                return(-1);
            }

            if (r1.Year < r2.Year)
            {
                return(-1);
            }

            if (r1.Year > r2.Year)
            {
                return(1);
            }

            if (part == DateTimePart.Year)
            {
                return(0);
            }

            if (r1.Month < r2.Month)
            {
                return(-1);
            }

            if (r1.Month > r2.Month)
            {
                return(1);
            }

            if (part == DateTimePart.Month)
            {
                return(0);
            }

            if (r1.Day < r2.Day)
            {
                return(-1);
            }

            if (r1.Day > r2.Day)
            {
                return(1);
            }

            if (part == DateTimePart.Day)
            {
                return(0);
            }

            if (r1.Hour < r2.Hour)
            {
                return(-1);
            }

            if (r1.Hour > r2.Hour)
            {
                return(1);
            }

            if (part == DateTimePart.Hour)
            {
                return(0);
            }

            if (r1.Minute < r2.Minute)
            {
                return(-1);
            }

            if (r1.Minute > r2.Minute)
            {
                return(1);
            }

            if (part == DateTimePart.Minute)
            {
                return(0);
            }

            if (r1.Second < r2.Second)
            {
                return(-1);
            }

            if (r1.Second > r2.Second)
            {
                return(1);
            }

            return(0);
        }
コード例 #45
0
 private static string DateDiff(DateTimePart part, string dt1, string dt2)
 {
     return(string.Format("datediff({0}, {1}, {2})", part.ToString().ToLower(), dt1, dt2));
 }
コード例 #46
0
        private bool ValidateAndPopulate(string schedulePart, DateTimePart dateTimePart)
        {
            int minValue = 0;
            int maxValue = 0;

            switch (dateTimePart)
            {
            case DateTimePart.Minute:
                maxValue = 59;
                break;

            case DateTimePart.Hour:
                maxValue = 23;
                break;

            case DateTimePart.Day:
                minValue = 1;
                maxValue = 31;
                break;

            case DateTimePart.Month:
                minValue = 1;
                maxValue = 12;
                break;

            case DateTimePart.DayOfWeek:
                maxValue = 6;
                break;
            }

            m_values = new List <int>();

            // Match literal asterisk
            if (Regex.Match(schedulePart, "^(\\*){1}$").Success)
            {
                // ^(\*){1}$             Matches: *
                m_valueTextSyntax = SchedulePartTextSyntax.Any;
                PopulateValues(minValue, maxValue, 1);

                return(true);
            }

            if (Regex.Match(schedulePart, "^(\\d+\\-\\d+/\\d+){1}$").Success)
            {
                // ^(\d+\-\d+/\d+){1}$   Matches: [any digit]-[any digit]/[any digit]
                string[] range     = schedulePart.Split('-');
                string[] parts     = range[1].Split('/');
                int      lowRange  = Convert.ToInt32(range[0]);
                int      highRange = Convert.ToInt32(parts[0]);
                int      interval  = Convert.ToInt32(parts[1]);

                if (lowRange < highRange && lowRange >= minValue && highRange <= maxValue && interval > 0 && interval >= minValue && interval <= maxValue)
                {
                    m_valueTextSyntax = SchedulePartTextSyntax.RangeWithEveryN;
                    PopulateValues(lowRange, highRange, interval);
                    return(true);
                }
            }
            else if (Regex.Match(schedulePart, "^(\\*/\\d+){1}$").Success)
            {
                // ^(\*/\d+){1}$         Matches: */[any digit]
                int interval = Convert.ToInt32(schedulePart.Split('/')[1]);

                if (interval > 0 && interval >= minValue && interval <= maxValue)
                {
                    m_valueTextSyntax = SchedulePartTextSyntax.EveryN;
                    PopulateValues(minValue, maxValue, interval);
                    return(true);
                }
            }
            else if (Regex.Match(schedulePart, "^(\\d+\\-\\d+){1}$").Success)
            {
                // ^(\d+\-\d+){1}$       Matches: [any digit]-[any digit]
                string[] range     = schedulePart.Split('-');
                int      lowRange  = Convert.ToInt32(range[0]);
                int      highRange = Convert.ToInt32(range[1]);

                if (lowRange < highRange && lowRange >= minValue && highRange <= maxValue)
                {
                    m_valueTextSyntax = SchedulePartTextSyntax.Range;
                    PopulateValues(lowRange, highRange, 1);
                    return(true);
                }
            }
            else if (Regex.Match(schedulePart, "^((\\d+,?)+){1}$").Success)
            {
                // ^((\d+,?)+){1}$       Matches: [any digit] AND [any digit], ..., [any digit]
                m_valueTextSyntax = SchedulePartTextSyntax.Specific;

                foreach (string part in schedulePart.Split(','))
                {
                    int value;

                    if (int.TryParse(part, out value))
                    {
                        if (!(value >= minValue && value <= maxValue))
                        {
                            return(false);
                        }

                        if (!m_values.Contains(value))
                        {
                            m_values.Add(value);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }

                return(true);
            }

            return(false);
        }
コード例 #47
0
ファイル: RecurDateTime.cs プロジェクト: modulexcite/PDI
        /// <summary>
        /// Compares two <c>RecurDateTime</c> values and returns an integer that indicates their relationship.
        /// This version only compares up to the specified date/time part.
        /// </summary>
        /// <param name="r1">The first date/time</param>
        /// <param name="r2">The second date/time</param>
        /// <param name="part">The part up to which comparisons are made.  Parts smaller than this are ignored.</param>
        /// <returns>Returns -1 if the first instance is less than the second, 0 if they are equal, or 1 if the
        /// first instance is greater than the second.</returns>
        public static int Compare(RecurDateTime r1, RecurDateTime r2, DateTimePart part)
        {
            // Cast as object for null checks or it goes recursive
            if((object)r1 == null && (object)r2 == null)
                return 0;

            if((object)r1 != null && (object)r2 == null)
                return 1;

            if((object)r1 == null && (object)r2 != null)
                return -1;

            if(r1.Year < r2.Year)
                return -1;

            if(r1.Year > r2.Year)
                return 1;

            if(part == DateTimePart.Year)
                return 0;

            if(r1.Month < r2.Month)
                return -1;

            if(r1.Month > r2.Month)
                return 1;

            if(part == DateTimePart.Month)
                return 0;

            if(r1.Day < r2.Day)
                return -1;

            if(r1.Day > r2.Day)
                return 1;

            if(part == DateTimePart.Day)
                return 0;

            if(r1.Hour < r2.Hour)
                return -1;

            if(r1.Hour > r2.Hour)
                return 1;

            if(part == DateTimePart.Hour)
                return 0;

            if(r1.Minute < r2.Minute)
                return -1;

            if(r1.Minute > r2.Minute)
                return 1;

            if(part == DateTimePart.Minute)
                return 0;

            if(r1.Second < r2.Second)
                return -1;

            if(r1.Second > r2.Second)
                return 1;

            return 0;
        }