Ejemplo n.º 1
0
        private static double GetDouble(TimeSpan value, TimeSpanUnit unit)
        {
            switch (unit)
            {
            case TimeSpanUnit.Days:
                return((double)value.Ticks / TimeSpan.TicksPerDay);

            case TimeSpanUnit.Hours:
                return(((double)value.Ticks / TimeSpan.TicksPerHour) % 24);

            case TimeSpanUnit.Minutes:
                return(((double)value.Ticks / TimeSpan.TicksPerMinute) % 60);

            case TimeSpanUnit.Seconds:
                return(((double)value.Ticks / TimeSpan.TicksPerSecond) % 60);

            case TimeSpanUnit.Milliseconds:
                return(((double)value.Ticks / TimeSpan.TicksPerMillisecond) % 1000);

            //case TimeSpanUnit.Microseconds:
            //return ((double)value.Ticks/10)%1000;

            //case TimeSpanUnit.Nanoseconds:
            //return ((double)value.Ticks*100)%1000;

            default:
                throw new ArgumentException(
                          string.Format(
                              "Flag not supported [{0}]. Note that flag must not be a combination of multiple flags.",
                              unit));
            }
        }
Ejemplo n.º 2
0
        private static IList <UnitValue> GetNonZeroUnitValues(TimeSpan value, TimeSpanUnit highestUnit,
                                                              TimeSpanUnit lowestUnit, IntegerRounding lowestUnitRounding, int maxUnitGroups)
        {
            value = GetTimeSpanWithLowestUnitRounded(value, lowestUnit, lowestUnitRounding);

            IList <UnitValue> unitValues = UnitsLargeToSmall
                                           .Where(unit => unit <= highestUnit && unit >= lowestUnit)
                                           .Select(unit => GetUnitValue(value, DefaultRounding, unit))
                                           .Where(unitValue => unitValue.Value >= 1)
                                           .Take(maxUnitGroups)
                                           .ToList();

            if (!unitValues.Any())
            {
                return(unitValues);
            }

            UnitValue last = unitValues.Last();

            // ReSharper disable once CompareOfFloatsByEqualityOperator
            if (last.DoubleValue == 0)
            {
                return(unitValues);
            }

            List <UnitValue> unitValuesWithLastUnitRoundedUp = unitValues
                                                               .Take(unitValues.Count - 1)
                                                               .Concat(new[] { GetUnitValue(value, lowestUnitRounding, last.Unit) })
                                                               .ToList();

            return(unitValuesWithLastUnitRoundedUp);
        }
Ejemplo n.º 3
0
        private static string GetShortUnitString(int value, TimeSpanUnit unit)
        {
            switch (unit)
            {
            case TimeSpanUnit.Days:
                return(value == 1 ? "day" : "days");

            case TimeSpanUnit.Hours:
                return(value == 1 ? "hr" : "hrs");

            case TimeSpanUnit.Minutes:
                return(value == 1 ? "min" : "mins");

            case TimeSpanUnit.Seconds:
                return(value == 1 ? "sec" : "secs");

            case TimeSpanUnit.Milliseconds:
                return(value == 1 ? "msec" : "msecs");

            //case TimeSpanUnit.Microseconds:
            //    return value == 1 ? "µsec" : "µsecs";

            //case TimeSpanUnit.Nanoseconds:
            //    return value == 1 ? "nsec" : "nsecs";

            default:
                throw new NotImplementedException("TimeSpanUnit: " + unit);
            }
        }
Ejemplo n.º 4
0
        private static string GetUnitValueString(int value, TimeSpanUnit unit, TimeFormat timeFormat,
                                                 UnitStringRepresentation rep, IFormatProvider formatProvider)
        {
            string unitString = GetUnitString(value, unit, rep);

            return(value.ToString(formatProvider) + timeFormat.UnitValueSeparator + unitString);
        }
Ejemplo n.º 5
0
        private static string GetShortUnitString(int value, TimeSpanUnit unit)
        {
            switch (unit)
            {
            case TimeSpanUnit.Days:
                return(value == 1 ? "天" : "天");

            case TimeSpanUnit.Hours:
                return(value == 1 ? "小时" : "小时");

            case TimeSpanUnit.Minutes:
                return(value == 1 ? "分钟" : "分钟");

            case TimeSpanUnit.Seconds:
                return(value == 1 ? "秒" : "秒");

            case TimeSpanUnit.Milliseconds:
                return(value == 1 ? "毫秒" : "毫秒");

            //case TimeSpanUnit.Microseconds:
            //    return value == 1 ? "µsec" : "µsecs";

            //case TimeSpanUnit.Nanoseconds:
            //    return value == 1 ? "nsec" : "nsecs";

            default:
                throw new NotImplementedException("TimeSpanUnit: " + unit);
            }
        }
Ejemplo n.º 6
0
        private static string GetCompactUnitString(TimeSpanUnit unit)
        {
            switch (unit)
            {
            case TimeSpanUnit.Days:
                return("天");

            case TimeSpanUnit.Hours:
                return("时");

            case TimeSpanUnit.Minutes:
                return("分");

            case TimeSpanUnit.Seconds:
                return("秒");

            case TimeSpanUnit.Milliseconds:
                return("毫秒");

            //case TimeSpanUnit.Microseconds:
            //    return value == 1 ? "microsecond" : "microseconds";

            //case TimeSpanUnit.Nanoseconds:
            //    return value == 1 ? "nanosecond" : "nanoseconds";

            default:
                throw new NotImplementedException("TimeSpanUnit: " + unit);
            }
        }
Ejemplo n.º 7
0
 public UnitValue(double doubleValue, TimeSpanUnit unit, int value) //, int valueWithLowestUnitRounding)
 {
     DoubleValue = doubleValue;
     Unit        = unit;
     Value       = value;
     //ValueRoundedDown = value;
     //ValueWithLowestUnitRounding = valueWithLowestUnitRounding;
 }
Ejemplo n.º 8
0
        public static List <DateTime> GenerateDateList(DateTime first, DateTime endDate,
                                                       int timeSpan, TimeSpanUnit timeSpanUnit, TemplateTimeType dateType, bool isForward, bool ignoreReduplicateDays)
        {
            if (timeSpan < 1)
            {
                throw new ApplicationException("Time span can't be 0");
            }

            var dateList = new List <DateTime>();

            DateTime date = first;

            for (int i = 0; ; ++i)
            {
                //避免每个月日期数不同导致的的向下取整问题
                //e.g.
                //(20160131).AddMonths(1).AddMonths(1) = 20160329
                //(20160131).AddMonths(2) = 20160331
                var interval = timeSpan * i;
                switch (timeSpanUnit)
                {
                case TimeSpanUnit.Year:
                    date = first.AddYears(interval);
                    break;

                case TimeSpanUnit.Month:
                    date = first.AddMonths(interval);
                    break;

                case TimeSpanUnit.Day:
                    date = first.AddDays(interval);
                    break;
                }

                if (dateType == TemplateTimeType.TradingDay && !CalendarCache.IsTradingDay(date))
                {
                    date = isForward ? GetPreviousTradingDay(date) : GetNextTradingDay(date);
                }
                else if (dateType == TemplateTimeType.WorkingDay && !CalendarCache.IsWorkingDay(date))
                {
                    date = isForward ? GetPreviousWorkingDay(date) : GetNextWorkingDay(date);
                }

                if (date > endDate)
                {
                    break;
                }

                bool isIgnore = false;
                isIgnore = ignoreReduplicateDays && dateList.Contains(date);
                if (!isIgnore)
                {
                    dateList.Add(date);
                }
            }

            return(dateList);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Gets the TimeSpanUnit's text
        /// </summary>
        /// <param name="value">The value of the TimeSpanUnit. </param>
        /// <param name="timeSpanUnit">The TimeSpanUnit, TimeSpanUnits.Minutes</param>
        /// <returns>The text for the TimeSpanUnit. This is  e.g. 'd' for TimeSpanUnits.Days</returns>
        /// <param name="longUnits">if true use long units rather than short ones</param>
        private static string GetTimeSpanUnitsString(long value, TimeSpanUnit timeSpanUnit, bool longUnits)
        {
            if (longUnits)
            {
                switch (timeSpanUnit)
                {
                case TimeSpanUnit.Years:
                    return(value > 1 ? Resources.NhsTimeSpanResources.YearsLongUnit : Resources.NhsTimeSpanResources.YearLongUnit);

                case TimeSpanUnit.Months:
                    return(value > 1 ? Resources.NhsTimeSpanResources.MonthsLongUnit : Resources.NhsTimeSpanResources.MonthLongUnit);

                case TimeSpanUnit.Weeks:
                    return(value > 1 ? Resources.NhsTimeSpanResources.WeeksLongUnit : Resources.NhsTimeSpanResources.WeekLongUnit);

                case TimeSpanUnit.Days:
                    return(value > 1 ? Resources.NhsTimeSpanResources.DaysLongUnit : Resources.NhsTimeSpanResources.DayLongUnit);

                case TimeSpanUnit.Hours:
                    return(value > 1 ? Resources.NhsTimeSpanResources.HoursLongUnit : Resources.NhsTimeSpanResources.HourLongUnit);

                case TimeSpanUnit.Minutes:
                    return(value > 1 ? Resources.NhsTimeSpanResources.MinutesLongUnit : Resources.NhsTimeSpanResources.MinuteLongUnit);

                case TimeSpanUnit.Seconds:
                    return(value > 1 ? Resources.NhsTimeSpanResources.SecondsLongUnit : Resources.NhsTimeSpanResources.SecondLongUnit);
                }
            }
            else
            {
                switch (timeSpanUnit)
                {
                case TimeSpanUnit.Years:
                    return(value > 1 ? Resources.NhsTimeSpanResources.YearsUnit : Resources.NhsTimeSpanResources.YearUnit);

                case TimeSpanUnit.Months:
                    return(value > 1 ? Resources.NhsTimeSpanResources.MonthsUnit : Resources.NhsTimeSpanResources.MonthUnit);

                case TimeSpanUnit.Weeks:
                    return(value > 1 ? Resources.NhsTimeSpanResources.WeeksUnit : Resources.NhsTimeSpanResources.WeekUnit);

                case TimeSpanUnit.Days:
                    return(value > 1 ? Resources.NhsTimeSpanResources.DaysUnit : Resources.NhsTimeSpanResources.DayUnit);

                case TimeSpanUnit.Hours:
                    return(value > 1 ? Resources.NhsTimeSpanResources.HoursUnit : Resources.NhsTimeSpanResources.HourUnit);

                case TimeSpanUnit.Minutes:
                    return(value > 1 ? Resources.NhsTimeSpanResources.MinutesUnit : Resources.NhsTimeSpanResources.MinuteUnit);

                case TimeSpanUnit.Seconds:
                    return(value > 1 ? Resources.NhsTimeSpanResources.SecondsUnit : Resources.NhsTimeSpanResources.SecondUnit);
                }
            }

            throw new ArgumentOutOfRangeException("timeSpanUnit");
        }
Ejemplo n.º 10
0
 public static string ToTimeRemainingString(this TimeSpan value,
                                            int maxUnitGroups              = 1,
                                            UnitStringRepresentation rep   = UnitStringRepresentation.Long,
                                            TimeSpanUnit highestUnit       = TimeSpanUnit.Days,
                                            TimeSpanUnit lowestUnit        = TimeSpanUnit.Seconds,
                                            IFormatProvider formatProvider = null)
 {
     return(ToPrettyString(value, maxUnitGroups, rep, highestUnit, lowestUnit, IntegerRounding.Up, formatProvider));
 }
Ejemplo n.º 11
0
        public override TableTemplateTime GetTableObject()
        {
            var templateTime = new TableTemplateTime();

            templateTime.template_time_id   = TemplateTimeId;
            templateTime.template_id        = TemplateId;
            templateTime.template_time_name = TemplateTimeName;
            templateTime.begin_time         = BeginTime;
            templateTime.end_time           = EndTime;
            templateTime.time_span          = TimeSpan;
            templateTime.time_span_unit     = TimeSpanUnit.ToString();
            templateTime.template_time_type = TemplateTimeType.ToString();
            templateTime.search_direction   = SearchDirection.ToString();
            templateTime.handle_reduplicate = HandleReduplicate.ToString();
            return(templateTime);
        }
Ejemplo n.º 12
0
        private static string GetUnitString(int value, TimeSpanUnit unit, UnitStringRepresentation rep)
        {
            switch (rep)
            {
            case UnitStringRepresentation.Compact:
            case UnitStringRepresentation.CompactWithSpace:
                return(GetCompactUnitString(unit));

            case UnitStringRepresentation.Long:
                return(GetLongUnitString(value, unit));

            case UnitStringRepresentation.Short:
                return(GetShortUnitString(value, unit));

            default:
                throw new NotImplementedException("TimeSpanUnit: " + unit);
            }
        }
        public void NoMiliseconds()
        {
            var t1 = new TimeSpanUnit("1 day", "", new TimeSpan(1, 0, 0, 0, 550));

            Assert.AreEqual(new TimeSpan(1, 0, 0, 0), t1.Span);

            var t2 = new TimeSpanUnit("1 hour", "", new TimeSpan(0, 1, 0, 0, 550));

            Assert.AreEqual(new TimeSpan(0, 1, 0, 0), t2.Span);

            var t3 = new TimeSpanUnit("1 min", "", new TimeSpan(0, 0, 1, 0, 550));

            Assert.AreEqual(new TimeSpan(0, 0, 1, 0), t3.Span);

            var t4 = new TimeSpanUnit("1 sec", "", new TimeSpan(0, 0, 0, 1, 550));

            Assert.AreEqual(new TimeSpan(0, 0, 0, 1), t4.Span);
        }
Ejemplo n.º 14
0
 public WatchLinkTimer(string _Url, int TimeSpan, TimeSpanUnit timeUnit, string _Id)
 {
     Id           = _Id;
     sUrl         = _Url;
     tTimer       = new Timer();
     tTimer.Tick += TimerBack;
     if (timeUnit == TimeSpanUnit.Hour)
     {
         tTimer.Interval = TimeSpan * 1000 * 60 * 60;
     }
     else if (timeUnit == TimeSpanUnit.Minute)
     {
         tTimer.Interval = TimeSpan * 1000 * 60;
     }
     else
     {
         tTimer.Interval = TimeSpan * 1000;
     }
 }
Ejemplo n.º 15
0
        private static TimeSpan GetTimeSpanWithLowestUnitRounded(TimeSpan value, TimeSpanUnit lowestUnit,
                                                                 IntegerRounding lowestUnitRounding)
        {
            int lowestUnitValueRounded = GetInteger(GetDouble(value, lowestUnit), lowestUnitRounding);

            // Round the lowest unit, then reconstruct TimeSpan to round 60 seconds to 1 minute etc.
            int days         = value.Days,
                hours        = value.Hours,
                minutes      = value.Minutes,
                seconds      = value.Seconds,
                milliseconds = value.Milliseconds;

            switch (lowestUnit)
            {
            case TimeSpanUnit.Days:
                days  = lowestUnitValueRounded;
                hours = minutes = seconds = milliseconds = 0;
                break;

            case TimeSpanUnit.Hours:
                hours   = lowestUnitValueRounded;
                minutes = seconds = milliseconds = 0;
                break;

            case TimeSpanUnit.Minutes:
                minutes = lowestUnitValueRounded;
                seconds = milliseconds = 0;
                break;

            case TimeSpanUnit.Seconds:
                seconds      = lowestUnitValueRounded;
                milliseconds = 0;
                break;

            case TimeSpanUnit.Milliseconds:
                milliseconds = lowestUnitValueRounded;
                break;
            }

            value = new TimeSpan(days, hours, minutes, seconds, milliseconds);
            return(value);
        }
        public void ConstructorSpan()
        {
            var _2andHalfDays = new TimeSpanUnit("2.5 days", "", new TimeSpan(2, 12, 0, 0));

            Assert.AreEqual(new TimeSpan(2, 12, 0, 0), _2andHalfDays.Span);
            Assert.AreEqual(2.5m * TimeSpanUnit.Day.AmountOfBaseUnits, _2andHalfDays.AmountOfBaseUnits);

            var _2andHalfHours = new TimeSpanUnit("2.5 hours", "", new TimeSpan(0, 2, 30, 0));

            Assert.AreEqual(new TimeSpan(0, 2, 30, 0), _2andHalfHours.Span);
            Assert.AreEqual(2.5m * TimeSpanUnit.Hour.AmountOfBaseUnits, _2andHalfHours.AmountOfBaseUnits);

            var _2andHalfMinutes = new TimeSpanUnit("2.5 mins", "", new TimeSpan(0, 0, 2, 30));

            Assert.AreEqual(new TimeSpan(0, 0, 2, 30), _2andHalfMinutes.Span);
            Assert.AreEqual(2.5m * TimeSpanUnit.Minute.AmountOfBaseUnits, _2andHalfMinutes.AmountOfBaseUnits);

            var _5Seconds = new TimeSpanUnit("5 secs", "", new TimeSpan(0, 0, 0, 5));

            Assert.AreEqual(new TimeSpan(0, 0, 0, 5), _5Seconds.Span);
            Assert.AreEqual(5m * TimeSpanUnit.Second.AmountOfBaseUnits, _5Seconds.AmountOfBaseUnits);
        }
Ejemplo n.º 17
0
 /// <summary>
 /// If the TimeSpan represents an Age, the Granularity and Threshold values must be set to an agreed combination of values.
 /// </summary>
 private void FixGranularityAndThreshold()
 {
     if (this.internalClrTimeSpan.TotalHours < 2)
     {
         this.granularityForAnAge = TimeSpanUnit.Minutes;
         this.thresholdForAnAge   = TimeSpanUnit.Minutes;
     }
     else if (this.internalClrTimeSpan.TotalDays < 2)
     {
         this.granularityForAnAge = TimeSpanUnit.Hours;
         this.thresholdForAnAge   = TimeSpanUnit.Hours;
     }
     else if (this.TotalWeeks < 4)
     {
         this.granularityForAnAge = TimeSpanUnit.Days;
         this.thresholdForAnAge   = TimeSpanUnit.Days;
     }
     else if (this.Years < 1)
     {
         this.granularityForAnAge = TimeSpanUnit.Weeks;
         this.thresholdForAnAge   = TimeSpanUnit.Days;
     }
     else if (this.Years < 2)
     {
         this.granularityForAnAge = TimeSpanUnit.Months;
         this.thresholdForAnAge   = TimeSpanUnit.Days;
     }
     else if (this.Years < 18)
     {
         this.granularityForAnAge = TimeSpanUnit.Years;
         this.thresholdForAnAge   = TimeSpanUnit.Months;
     }
     else if (this.Years >= 18)
     {
         this.granularityForAnAge = TimeSpanUnit.Years;
         this.thresholdForAnAge   = TimeSpanUnit.Years;
     }
 }
Ejemplo n.º 18
0
        /// <summary>
        /// Gets part of the TimeSpan data used to make up a string representation of the current TimeSpan.
        /// </summary>
        /// <param name="beginningOfToString">Is this part the first part in the string representation</param>
        /// <param name="timeSpanUnit">The part of the TimeSpan required</param>
        /// <param name="relevantGranularity">Relevant granularity</param>
        /// <returns>A numeric value for the part</returns>
        private long GetToStringPartValue(bool beginningOfToString, TimeSpanUnit timeSpanUnit, TimeSpanUnit relevantGranularity)
        {
            switch (timeSpanUnit)
            {
            case TimeSpanUnit.Years:
                return(this.Years);

            case TimeSpanUnit.Months:
                if (beginningOfToString == true)
                {
                    return(this.TotalMonths);
                }
                else
                {
                    return(this.Months);
                }

            case TimeSpanUnit.Weeks:
                if (beginningOfToString == true)
                {
                    return(this.TotalWeeks);
                }
                else
                {
                    return(this.Weeks);
                }

            case TimeSpanUnit.Days:
                if (beginningOfToString == true)
                {
                    return((long)this.internalClrTimeSpan.TotalDays);
                }
                else if (this.WeeksAreRelevant == false)
                {
                    // If weeks are not relevant then they will have not been
                    // used as part of the ToString so use Days rather than calculating the remaining
                    return(this.Days);
                }
                else
                {
                    if (relevantGranularity == TimeSpanUnit.Weeks)
                    {
                        return((long)this.internalClrTimeSpan.TotalDays % 7);
                    }
                    else
                    {
                        // The remaining days once weeks have been taken into consideration.

                        // Of course we know that we only want remaining days because this is
                        // not beginningOfToString so Weeks have already been read off
                        return(this.Days % 7);
                    }
                }

            case TimeSpanUnit.Hours:
                if (beginningOfToString == true)
                {
                    return((long)this.internalClrTimeSpan.TotalHours);
                }
                else
                {
                    return((long)this.Hours);
                }

            case TimeSpanUnit.Minutes:
                if (beginningOfToString == true)
                {
                    return((long)this.internalClrTimeSpan.TotalMinutes);
                }
                else
                {
                    return((long)this.Minutes);
                }

            case TimeSpanUnit.Seconds:
                if (beginningOfToString == true)
                {
                    return((long)this.internalClrTimeSpan.TotalSeconds);
                }
                else
                {
                    return((long)this.Seconds);
                }
            }

            throw new ArgumentOutOfRangeException("timeSpanUnit");
        }
Ejemplo n.º 19
0
 TimeIt(TimeSpanUnit timeSpanUnit = TimeSpanUnit.Milliseconds, Action callback = null, [CallerMemberName] string name = "")
 {
     this.name         = name;
     this.timeSpanUnit = timeSpanUnit;
     watch             = Stopwatch.StartNew();
 }
Ejemplo n.º 20
0
 /// <summary>
 /// If the TimeSpan represents an Age, the Granularity and Threshold values must be set to an agreed combination of values. 
 /// </summary>
 private void FixGranularityAndThreshold()
 {
     if (this.internalClrTimeSpan.TotalHours < 2)
     {
         this.granularityForAnAge = TimeSpanUnit.Minutes;
         this.thresholdForAnAge = TimeSpanUnit.Minutes;
     }
     else if (this.internalClrTimeSpan.TotalDays < 2)
     {
         this.granularityForAnAge = TimeSpanUnit.Hours;
         this.thresholdForAnAge = TimeSpanUnit.Hours;
     }
     else if (this.TotalWeeks < 4)
     {
         this.granularityForAnAge = TimeSpanUnit.Days;
         this.thresholdForAnAge = TimeSpanUnit.Days;
     }
     else if (this.Years < 1)
     {
         this.granularityForAnAge = TimeSpanUnit.Weeks;
         this.thresholdForAnAge = TimeSpanUnit.Days;
     }
     else if (this.Years < 2)
     {
         this.granularityForAnAge = TimeSpanUnit.Months;
         this.thresholdForAnAge = TimeSpanUnit.Days;
     }
     else if (this.Years < 18)
     {
         this.granularityForAnAge = TimeSpanUnit.Years;
         this.thresholdForAnAge = TimeSpanUnit.Months;
     }
     else if (this.Years >= 18)
     {
         this.granularityForAnAge = TimeSpanUnit.Years;
         this.thresholdForAnAge = TimeSpanUnit.Years;
     }
 }
Ejemplo n.º 21
0
        private static UnitValue GetUnitValue(TimeSpan value, IntegerRounding rounding, TimeSpanUnit unit)
        {
            double doubleValue = GetDouble(value, unit);
            //int valueRoundedDown = GetInteger(doubleValue, DefaultRounding);
            //int valueWithLowestUnitRounding = GetInteger(doubleValue, rounding);
            int valueRounded = GetInteger(doubleValue, rounding);

            return(new UnitValue(doubleValue, unit, valueRounded)); //Down, valueWithLowestUnitRounding);
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Gets the TimeSpanUnit's text.
        /// </summary>
        /// <param name="value">The value of the TimeSpanUnit. </param>
        /// <param name="timeSpanUnit">The TimeSpanUnit, TimeSpanUnits.Minutes.</param>
        /// <returns>The text for the TimeSpanUnit. This is  e.g. 'd' for TimeSpanUnits.Days.</returns>
        /// <param name="longUnits">If true use long units rather than short ones.</param>
        private static string GetTimeSpanUnitsString(long value, TimeSpanUnit timeSpanUnit, bool longUnits)
        {
            if (longUnits)
            {
                switch (timeSpanUnit)
                {
                    case TimeSpanUnit.Years:
                        return (value > 1 ? Resources.CuiTimeSpanResources.YearsLongUnit : Resources.CuiTimeSpanResources.YearLongUnit);
                    case TimeSpanUnit.Months:
                        return (value > 1 ? Resources.CuiTimeSpanResources.MonthsLongUnit : Resources.CuiTimeSpanResources.MonthLongUnit);
                    case TimeSpanUnit.Weeks:
                        return (value > 1 ? Resources.CuiTimeSpanResources.WeeksLongUnit : Resources.CuiTimeSpanResources.WeekLongUnit);
                    case TimeSpanUnit.Days:
                        return (value > 1 ? Resources.CuiTimeSpanResources.DaysLongUnit : Resources.CuiTimeSpanResources.DayLongUnit);
                    case TimeSpanUnit.Hours:
                        return (value > 1 ? Resources.CuiTimeSpanResources.HoursLongUnit : Resources.CuiTimeSpanResources.HourLongUnit);
                    case TimeSpanUnit.Minutes:
                        return (value > 1 ? Resources.CuiTimeSpanResources.MinutesLongUnit : Resources.CuiTimeSpanResources.MinuteLongUnit);
                    case TimeSpanUnit.Seconds:
                        return (value > 1 ? Resources.CuiTimeSpanResources.SecondsLongUnit : Resources.CuiTimeSpanResources.SecondLongUnit);
                }
            }
            else
            {
                switch (timeSpanUnit)
                {
                    case TimeSpanUnit.Years:
                        return (value > 1 ? Resources.CuiTimeSpanResources.YearsUnit : Resources.CuiTimeSpanResources.YearUnit);
                    case TimeSpanUnit.Months:
                        return (value > 1 ? Resources.CuiTimeSpanResources.MonthsUnit : Resources.CuiTimeSpanResources.MonthUnit);
                    case TimeSpanUnit.Weeks:
                        return (value > 1 ? Resources.CuiTimeSpanResources.WeeksUnit : Resources.CuiTimeSpanResources.WeekUnit);
                    case TimeSpanUnit.Days:
                        return (value > 1 ? Resources.CuiTimeSpanResources.DaysUnit : Resources.CuiTimeSpanResources.DayUnit);
                    case TimeSpanUnit.Hours:
                        return (value > 1 ? Resources.CuiTimeSpanResources.HoursUnit : Resources.CuiTimeSpanResources.HourUnit);
                    case TimeSpanUnit.Minutes:
                        return (value > 1 ? Resources.CuiTimeSpanResources.MinutesUnit : Resources.CuiTimeSpanResources.MinuteUnit);
                    case TimeSpanUnit.Seconds:
                        return (value > 1 ? Resources.CuiTimeSpanResources.SecondsUnit : Resources.CuiTimeSpanResources.SecondUnit);
                }
            }

            throw new ArgumentOutOfRangeException("timeSpanUnit");
        }
Ejemplo n.º 23
0
 public static TimeIt Start(
     TimeSpanUnit timeSpanUnit      = TimeSpanUnit.Milliseconds,
     Action callback                = null,
     [CallerMemberName] string name = ""
     ) => new TimeIt(timeSpanUnit, callback, name);
Ejemplo n.º 24
0
        /// <summary>
        /// Gets part of the TimeSpan data used to make up a string representation of the current TimeSpan.
        /// </summary>
        /// <param name="beginningOfToString">Is this part the first part in the string representation.</param>
        /// <param name="timeSpanUnit">The part of the TimeSpan required.</param>
        /// <param name="relevantGranularity">Relevant granularity.</param>
        /// <returns>A numeric value for the part.</returns>
        private long GetToStringPartValue(bool beginningOfToString, TimeSpanUnit timeSpanUnit, TimeSpanUnit relevantGranularity)
        {
            switch (timeSpanUnit)
            {
                case TimeSpanUnit.Years:
                    return this.Years;
                case TimeSpanUnit.Months:
                    if (beginningOfToString == true)
                    {
                        return this.TotalMonths;
                    }
                    else
                    {
                        return this.Months;
                    }

                case TimeSpanUnit.Weeks:
                    if (beginningOfToString == true)
                    {
                        return this.TotalWeeks;
                    }
                    else
                    {
                        return this.Weeks;
                    }

                case TimeSpanUnit.Days:
                    if (beginningOfToString == true)
                    {
                        return (long)this.internalClrTimeSpan.TotalDays;
                    }
                    else if (this.WeeksAreRelevant == false)
                    {
                        // If weeks are not relevant then they will have not been 
                        // used as part of the ToString so use Days rather than calculating the remaining
                        return this.Days;
                    }
                    else
                    {
                        if (relevantGranularity == TimeSpanUnit.Weeks)
                        {
                            return (long)this.internalClrTimeSpan.TotalDays % 7;
                        }
                        else
                        {
                            // The remaining days once weeks have been taken into consideration.

                            // Of course we know that we only want remaining days because this is 
                            // not beginningOfToString so Weeks have already been read off
                            return this.Days % 7;
                        }
                    }

                case TimeSpanUnit.Hours:
                    if (beginningOfToString == true)
                    {
                        return (long)this.internalClrTimeSpan.TotalHours;
                    }
                    else
                    {
                        return (long)this.Hours;
                    }

                case TimeSpanUnit.Minutes:
                    if (beginningOfToString == true)
                    {
                        return (long)this.internalClrTimeSpan.TotalMinutes;
                    }
                    else
                    {
                        return (long)this.Minutes;
                    }

                case TimeSpanUnit.Seconds:
                    if (beginningOfToString == true)
                    {
                        return (long)this.internalClrTimeSpan.TotalSeconds;
                    }
                    else
                    {
                        return (long)this.Seconds;
                    }
            }

            throw new ArgumentOutOfRangeException("timeSpanUnit");
        }
Ejemplo n.º 25
0
        /// <summary>
        ///     Returns a human readable string from TimeSpan, with a max number of parts to include. Units are included from
        ///     largest to smallest.
        /// </summary>
        /// <param name="value">Time span value.</param>
        /// <param name="maxUnitGroups">
        ///     The max number of timespan units to use.
        /// </param>
        /// <param name="lowestUnit">Lowest unit to include in string.</param>
        /// <param name="rep"></param>
        /// <param name="highestUnit">Highest unit to include in string.</param>
        /// <param name="lowestUnitRounding">Rounding behavior of <paramref name="lowestUnit" />.</param>
        /// <param name="formatProvider">Specify the formatProvider used to .ToString() the numeric values.</param>
        /// <returns>Human readable string.</returns>
        public static string ToPrettyString(this TimeSpan value,
                                            int maxUnitGroups                  = 1,
                                            UnitStringRepresentation rep       = UnitStringRepresentation.Long,
                                            TimeSpanUnit highestUnit           = TimeSpanUnit.Days,
                                            TimeSpanUnit lowestUnit            = TimeSpanUnit.Seconds,
                                            IntegerRounding lowestUnitRounding = IntegerRounding.ToNearestOrUp,
                                            IFormatProvider formatProvider     = null)
        {
            if (maxUnitGroups <= 0)
            {
                maxUnitGroups = 1;
            }

            TimeFormat timeFormat;

            if (!Formats.TryGetValue(rep, out timeFormat))
            {
                throw new NotImplementedException("UnitStringRepresentation: " + rep);
            }

            if (value == TimeSpan.Zero)
            {
                return(GetUnitValueString(0, lowestUnit, timeFormat, rep, formatProvider));
            }

            //int days = value.Days,
            //    hours = value.Hours,
            //    minutes = value.Minutes,
            //    seconds = value.Seconds,
            //    milliseconds = value.Milliseconds;
            //if (highestUnit < TimeSpanUnit.Days || lowestUnit > TimeSpanUnit.Days)
            //    days = 0;
            //if (highestUnit < TimeSpanUnit.Hours || lowestUnit > TimeSpanUnit.Hours)
            //    hours = 0;
            //if (highestUnit < TimeSpanUnit.Minutes || lowestUnit > TimeSpanUnit.Minutes)
            //    minutes = 0;
            //if (highestUnit < TimeSpanUnit.Seconds || lowestUnit > TimeSpanUnit.Seconds)
            //    seconds = 0;
            //if (highestUnit < TimeSpanUnit.Milliseconds || lowestUnit > TimeSpanUnit.Milliseconds)
            //    milliseconds = 0;

            // Trim off any values outside range
            TimeSpan trimmedValue = value; //new TimeSpan(days, hours, minutes, seconds, milliseconds);

            //var roundedValue = GetRoundedValue(trimmedValue, highestUnit, lowestUnit, lowestUnitRounding, maxUnitGroups);
            IList <UnitValue> nonZeroUnitValues =
                GetNonZeroUnitValues(trimmedValue, highestUnit, lowestUnit, lowestUnitRounding, maxUnitGroups)
                .RoundUnitsUp();

            if (!nonZeroUnitValues.Any())
            {
                // Value is zero or near zero. Fall back to lowest unit.
                // Example: 0.1 seconds when lowest unit is seconds
                int nearZeroValueRounded = GetInteger(GetDouble(value, lowestUnit), lowestUnitRounding);
                return(GetUnitValueString(nearZeroValueRounded, lowestUnit, timeFormat, rep, formatProvider));
            }

            List <string> unitStrings =
                nonZeroUnitValues
                .Select((uv, i) => GetUnitValueString(uv.Value, uv.Unit, timeFormat, rep, formatProvider))
                .ToList();

            // Example: "3 hours"
            if (unitStrings.Count == 1)
            {
                return(unitStrings.First());
            }

            // Example: "1 weeks, 2 days"
            string firstParts = string.Join(timeFormat.GroupSeparator, unitStrings.Take(unitStrings.Count - 1).ToArray());

            // Example: "3 hours"
            string lastPart = unitStrings.Last();

            // Example: "1 weeks, 2 days and 3 hours"
            return(firstParts + timeFormat.LastGroupSeparator + lastPart);
        }
Ejemplo n.º 26
0
        /// <summary> 根据当前指定的时间跨度来对当前时间的增减 </summary>
        /// <param name="originTime">初始时间</param>
        /// <param name="spanValue">时间跨度的数值。正值表示增加时间跨度,负值表示送去时间跨度。</param>
        /// <param name="spanUnit">时间跨度的单位</param>
        /// <returns></returns>
        public static DateTime GetTimeFromTimeSpan(DateTime originTime, double spanValue, TimeSpanUnit spanUnit)
        {
            //
            DateTime modifiedTime = default(DateTime);

            switch (spanUnit)
            {
            case TimeSpanUnit.Years:
            {
                modifiedTime = originTime.AddYears((int)spanValue);
                break;
            }

            case TimeSpanUnit.Months:
            {
                modifiedTime = originTime.AddMonths((int)spanValue);
                break;
            }

            case TimeSpanUnit.Days:
            {
                modifiedTime = originTime.AddDays(spanValue);
                break;
            }

            case TimeSpanUnit.Hours:
            {
                modifiedTime = originTime.AddHours(spanValue);
                break;
            }

            case TimeSpanUnit.Minites:
            {
                modifiedTime = originTime.AddMinutes(spanValue);
                break;
            }
            }

            return(modifiedTime);
        }