/// <summary>
 ///   Returns a new <see cref="TimeSpan" /> object which is rounded down to the specified <paramref name="part" />.
 /// </summary>
 /// <param name="source">The <see cref="TimeSpan" /> to round down.</param>
 /// <param name="part">The part to round down to.</param>
 public static TimeSpan Round(this TimeSpan source, TimeSpanPart part)
 {
     return(new TimeSpan(
                part >= TimeSpanPart.Day ? source.Days : 0,
                part >= TimeSpanPart.Hour ? source.Hours : 0,
                part >= TimeSpanPart.Minute ? source.Minutes : 0,
                part >= TimeSpanPart.Second ? source.Seconds : 0,
                part >= TimeSpanPart.Millisecond ? source.Milliseconds : 0));
 }
Example #2
0
 private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
 {
     if (e.ClickCount == 2)
     {
         _activeTextBoxPart = (TimeSpanPart)((TextBlock)sender).Tag;
         NumericTBEditorContainer.SetCurrentValue(VisibilityProperty, Visibility.Visible);
         _isInEditMode = true;
     }
 }
		/// <summary>
		///   Returns a new <see cref="TimeSpan" /> object which is rounded down to the specified <paramref name="part" />.
		/// </summary>
		/// <param name="source">The <see cref="TimeSpan" /> to round down.</param>
		/// <param name="part">The part to round down to.</param>
		public static TimeSpan Round( this TimeSpan source, TimeSpanPart part )
		{
			return new TimeSpan(
				part >= TimeSpanPart.Day ? source.Days : 0,
				part >= TimeSpanPart.Hour ? source.Hours : 0,
				part >= TimeSpanPart.Minute ? source.Minutes : 0,
				part >= TimeSpanPart.Second ? source.Seconds : 0,
				part >= TimeSpanPart.Millisecond ? source.Milliseconds : 0 );
		}
Example #4
0
        /// <summary>
        /// Converts this timespan to a human readable string.
        /// </summary>
        /// <param name="timespan">The timespan.</param>
        /// <param name="parts">The parts (flagable).</param>
        /// <returns>
        /// Always returns a value (empty when no ticks) to allow method chaining.
        /// </returns>
        public static string Humanise(this TimeSpan timespan, TimeSpanPart parts = TimeSpanPart.Day | TimeSpanPart.Hour | TimeSpanPart.Minute | TimeSpanPart.Second)
        {
            var result = string.Empty;

            if (timespan.Ticks != 0)
            {
                var formattedSections = new List <string>();

                if (parts.HasFlag(TimeSpanPart.Day))
                {
                    formattedSections.Add(FormatSection(timespan.Days, TimeSpanPart.Day));
                }


                if (parts.HasFlag(TimeSpanPart.Hour))
                {
                    formattedSections.Add(FormatSection(timespan.Hours, TimeSpanPart.Hour));
                }


                if (parts.HasFlag(TimeSpanPart.Minute))
                {
                    formattedSections.Add(FormatSection(timespan.Minutes, TimeSpanPart.Minute));
                }


                if (parts.HasFlag(TimeSpanPart.Second))
                {
                    formattedSections.Add(FormatSection(timespan.Seconds, TimeSpanPart.Second));
                }

                result = string.Join(", ", formattedSections.Where(s => !string.IsNullOrEmpty(s)));

                if (string.IsNullOrEmpty(result))
                {
                    if (parts.HasFlag(TimeSpanPart.Second))
                    {
                        result = "Less than a second";
                    }
                    else if (parts.HasFlag(TimeSpanPart.Minute))
                    {
                        result = "Less than a minute";
                    }
                    else if (parts.HasFlag(TimeSpanPart.Hour))
                    {
                        result = "Less than an hour";
                    }
                    else if (parts.HasFlag(TimeSpanPart.Day))
                    {
                        result = "Less than a day";
                    }
                }
            }

            return(result);
        }
Example #5
0
        /// <summary>
        /// Converts this timespan to a human readable string.
        /// </summary>
        /// <param name="timespan">The timespan.</param>
        /// <param name="parts">The parts (flagable).</param>
        /// <returns>
        /// Always returns a value (empty when no ticks) to allow method chaining.
        /// </returns>
        public static string Humanise(this TimeSpan timespan, TimeSpanPart parts = TimeSpanPart.Day | TimeSpanPart.Hour | TimeSpanPart.Minute | TimeSpanPart.Second)
        {
            var result = string.Empty;

            if (timespan.Ticks > 0)
            {
                var formattedSections = new List<string>();

                if (parts.HasFlag(TimeSpanPart.Day))
                {
                    formattedSections.Add(FormatSection(timespan.Days, TimeSpanPart.Day));
                }

                if (parts.HasFlag(TimeSpanPart.Hour))
                {
                    formattedSections.Add(FormatSection(timespan.Hours, TimeSpanPart.Hour));
                }

                if (parts.HasFlag(TimeSpanPart.Minute))
                {
                    formattedSections.Add(FormatSection(timespan.Minutes, TimeSpanPart.Minute));
                }

                if (parts.HasFlag(TimeSpanPart.Second))
                {
                    formattedSections.Add(FormatSection(timespan.Seconds, TimeSpanPart.Second));
                }

                result = string.Join(", ", formattedSections.Where(s => !string.IsNullOrEmpty(s)));

                if (string.IsNullOrEmpty(result))
                {
                    if (parts.HasFlag(TimeSpanPart.Second))
                    {
                        result = "Less than a second";
                    }
                    else if (parts.HasFlag(TimeSpanPart.Minute))
                    {
                        result = "Less than a minute";
                    }
                    else if (parts.HasFlag(TimeSpanPart.Hour))
                    {
                        result = "Less than an hour";
                    }
                    else if (parts.HasFlag(TimeSpanPart.Day))
                    {
                        result = "Less than a day";
                    }
                }
            }

            return result;
        }
Example #6
0
        private static string FormatSection(int span, TimeSpanPart part)
        {
            var section = string.Empty;

            if (span != 0)
            {
                var partString = part.ToString().ToLower();

                if (Math.Abs(span) > 1)
                {
                    partString += "s";
                }

                section = $"{span} {partString}";
            }

            return(section);
        }
Example #7
0
        public static string GetTimeSpanPartName(this TimeSpanPart timeSpanPart)
        {
            switch (timeSpanPart)
            {
            case TimeSpanPart.Days:
                return("days");

            case TimeSpanPart.Hours:
                return("hours");

            case TimeSpanPart.Minutes:
                return("minutes");

            case TimeSpanPart.Seconds:
                return("seconds");

            default:
                throw new InvalidOperationException();
            }
        }
Example #8
0
        public static double GetTimeSpanPartValue(this TimeSpan value, TimeSpanPart timeSpanPart)
        {
            switch (timeSpanPart)
            {
            case TimeSpanPart.Days:
                return(value.TotalDays);

            case TimeSpanPart.Hours:
                return(value.TotalHours);

            case TimeSpanPart.Minutes:
                return(value.TotalMinutes);

            case TimeSpanPart.Seconds:
                return(value.TotalSeconds);

            default:
                throw new InvalidOperationException();
            }
        }
Example #9
0
        public static TimeSpan CreateTimeSpan(this TimeSpanPart timeSpanPart, double value)
        {
            switch (timeSpanPart)
            {
            case TimeSpanPart.Days:
                return(TimeSpan.FromDays(value));

            case TimeSpanPart.Hours:
                return(TimeSpan.FromHours(value));

            case TimeSpanPart.Minutes:
                return(TimeSpan.FromMinutes(value));

            case TimeSpanPart.Seconds:
                return(TimeSpan.FromSeconds(value));

            default:
                throw new InvalidOperationException();
            }
        }
        public static double GetTimeSpanPartValue(this TimeSpan value, TimeSpanPart timeSpanPart)
        {
            switch (timeSpanPart)
            {
                case TimeSpanPart.Days:
                    return value.TotalDays;

                case TimeSpanPart.Hours:
                    return value.TotalHours;

                case TimeSpanPart.Minutes:
                    return value.TotalMinutes;

                case TimeSpanPart.Seconds:
                    return value.TotalSeconds;

                default:
                    throw new InvalidOperationException();
            }
        }
Example #11
0
        /// <summary>
        /// Returns a string that describes the TimeSpan in the specified part part
        /// </summary>
        /// <param name="part">The desired part to be described</param>
        /// <param name="round">The number of decimals to be returned for partial time spans</param>
        public static string ToShortDescriptionString(this TimeSpan timeSpan, TimeSpanPart part, int round = 2)
        {
            switch (part)
            {
            case TimeSpanPart.Years:
                var y = Math.Round(timeSpan.TotalDays / 365, round);
                return(string.Format("{0} {1}", y, "year".MakePlural(y)));

            case TimeSpanPart.Months:
                var mo = Math.Round(timeSpan.TotalDays / 30, round);
                return(string.Format("{0} {1}", mo, "month".MakePlural(mo)));

            case TimeSpanPart.Weeks:
                var w = Math.Round(timeSpan.TotalDays / 7, round);
                return(string.Format("{0} {1}", w, "week".MakePlural(w)));

            case TimeSpanPart.Days:
                var d = Math.Round(timeSpan.TotalDays, round);
                return(string.Format("{0} {1}", d, "day".MakePlural(d)));

            case TimeSpanPart.Hours:
                var h = Math.Round(timeSpan.TotalHours, round);
                return(string.Format("{0} {1}", h, "hour".MakePlural(h)));

            case TimeSpanPart.Minutes:
                var m = Math.Round(timeSpan.TotalMinutes, round);
                return(string.Format("{0} {1}", m, "minute".MakePlural(m)));

            case TimeSpanPart.Seconds:
                var s = Math.Round(timeSpan.TotalSeconds, round);
                return(string.Format("{0} {1}", s, "second".MakePlural(s)));

            case TimeSpanPart.Milliseconds:
                var mm = Math.Round(timeSpan.TotalMilliseconds, round);
                return(string.Format("{0} {1}", mm, "millisecond".MakePlural(mm)));

            default:
                return(timeSpan.ToLongDescriptionString());
            }
        }
Example #12
0
        private static string FormatSection(int span, TimeSpanPart part)
        {
            var section = string.Empty;

            if (span > 0)
            {
                var partString = part.ToString().ToLower();

                if (span > 1)
                {
                    partString += "s";
                }

                section = $"{span} {partString}";
            }

            return section;
        }
 private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
 {
     if (e.ClickCount == 2)
     {
         _activeTextBoxPart = (TimeSpanPart) ((TextBlock)sender).Tag;
         NumericTBEditorContainer.Visibility = Visibility.Visible;
         _isInEditMode = true;
     }
 }