コード例 #1
0
 /// <summary>
 /// Constructs a recurring timer that will fire once a day at the specified time offset.
 /// </summary>
 /// <param name="timeOfDay">The time of day offset.</param>
 public RecurringTimer(TimeOfDay timeOfDay)
 {
     this.type       = RecurringTimerType.Daily;
     this.timeOffset = timeOfDay.TimeSpan;
 }
コード例 #2
0
        /// <summary>
        /// Attempts to parse the configuration value.
        /// </summary>
        /// <param name="input">The configuration value.</param>
        /// <returns><c>true</c> if the value could be parsed, <b></b> if the value is not valid for the type.</returns>
        private bool TryParse(string input)
        {
            Covenant.Requires <ArgumentNullException>(input != null);

            string[] fields;
            double   hours   = 0;
            double   minutes = 0;
            double   seconds = 0;

            fields = input.Split(new char[] { ':' }, 2);

            switch (fields[0].Trim().ToLower())
            {
            case "disabled":

                this.type = RecurringTimerType.Disabled;
                return(true);

            case "minute":

                this.type       = RecurringTimerType.Minute;
                this.timeOffset = TimeSpan.Zero;
                return(true);

            case "quarterhour":

                this.type = RecurringTimerType.QuarterHour;

                if (fields.Length == 1)
                {
                    this.timeOffset = TimeSpan.Zero;
                    return(true);
                }

                fields = fields[1].Split(':');

                if (fields.Length > 2)
                {
                    return(false);
                }

                try
                {
                    minutes = double.Parse(fields[0], NumberFormatInfo.InvariantInfo);

                    if (fields.Length > 1)
                    {
                        seconds = double.Parse(fields[1], NumberFormatInfo.InvariantInfo);
                    }

                    timeOffset = TimeSpan.FromMinutes(minutes) + TimeSpan.FromSeconds(seconds);

                    if (timeOffset >= TimeSpan.FromMinutes(15))
                    {
                        timeOffset = TimeSpan.Zero;
                    }
                }
                catch
                {
                    return(false);
                }

                return(true);

            case "hourly":

                this.type = RecurringTimerType.Hourly;

                if (fields.Length == 1)
                {
                    this.timeOffset = TimeSpan.Zero;
                    return(true);
                }

                fields = fields[1].Split(':');

                if (fields.Length > 2)
                {
                    return(false);
                }

                try
                {
                    minutes = double.Parse(fields[0], NumberFormatInfo.InvariantInfo);

                    if (fields.Length > 1)
                    {
                        seconds = double.Parse(fields[1], NumberFormatInfo.InvariantInfo);
                    }

                    timeOffset = TimeSpan.FromMinutes(minutes) + TimeSpan.FromSeconds(seconds);

                    if (timeOffset >= TimeSpan.FromHours(1))
                    {
                        timeOffset = TimeSpan.Zero;
                    }
                }
                catch
                {
                    return(false);
                }

                return(true);

            case "daily":

                TimeOfDay timeOfDay;

                this.type = RecurringTimerType.Daily;

                if (fields.Length == 1)
                {
                    this.timeOffset = TimeSpan.Zero;
                    return(true);
                }

                if (!TimeOfDay.TryParse(fields[1], out timeOfDay))
                {
                    return(false);
                }

                this.timeOffset = timeOfDay.TimeSpan;

                if (timeOffset >= TimeSpan.FromDays(1))
                {
                    timeOffset = TimeSpan.Zero;
                }
                return(true);

            case "interval":

                this.type = RecurringTimerType.Interval;

                if (fields.Length != 2)
                {
                    return(false);
                }

                fields = fields[1].Split(':');

                if (fields.Length > 3)
                {
                    return(false);
                }

                try
                {
                    hours      = double.Parse(fields[0], NumberFormatInfo.InvariantInfo);
                    minutes    = double.Parse(fields[1], NumberFormatInfo.InvariantInfo);
                    seconds    = double.Parse(fields[2], NumberFormatInfo.InvariantInfo);
                    timeOffset = TimeSpan.FromHours(hours) + TimeSpan.FromMinutes(minutes) + TimeSpan.FromSeconds(seconds);
                }
                catch
                {
                    return(false);
                }

                return(true);

            default:

                return(false);
            }
        }
コード例 #3
0
        //---------------------------------------------------------------------
        // Static members

        /// <summary>
        /// Attempts to parse a string of the form HH:MM or HH:MM:SS into a
        /// time of day offset.
        /// </summary>
        /// <param name="value">The string to be parsed.</param>
        /// <param name="timeOfDay">Returns as the parsed time of day on success.</param>
        /// <returns><c>true</c> if the string was parsed successfully, <c>false</c> otherwise.</returns>
        public static bool TryParse(string value, out TimeOfDay timeOfDay)
        {
            string[] fields;
            int      hours;
            int      minutes;
            int      seconds;
            TimeSpan offset;

            timeOfDay = default;

            if (value == null)
            {
                return(false);
            }

            fields = value.Split(':');

            switch (fields.Length)
            {
            case 2:

                // Parsing HH:MM

                if (!int.TryParse(fields[0].Trim(), out hours))
                {
                    return(false);
                }

                if (!int.TryParse(fields[1].Trim(), out minutes))
                {
                    return(false);
                }

                seconds = 0;
                break;

            case 3:

                // Parsing HH:MM:SS

                if (!int.TryParse(fields[0].Trim(), out hours))
                {
                    return(false);
                }

                if (!int.TryParse(fields[1].Trim(), out minutes))
                {
                    return(false);
                }

                if (!int.TryParse(fields[2].Trim(), out seconds))
                {
                    return(false);
                }
                break;

            default:

                return(false);
            }

            offset = new TimeSpan(hours, minutes, seconds);

            if (offset < TimeSpan.Zero || offset.TotalHours >= 24)
            {
                return(false);
            }

            timeOfDay.offset = offset;
            return(true);
        }