Exemplo n.º 1
0
        protected override string Format(string resourceKey, int number, ShowQuantityAs showQuantityAs)
        {
            var format      = Resources.GetResource(GetResourceKey(resourceKey, number), _romanianCulture);
            var preposition = ShouldUsePreposition(number)
                                     ? UnitPreposition
                                     : string.Empty;

            return(format.FormatWith(number, preposition));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns the string representation of the provided TimeSpan
        /// </summary>
        /// <param name="timeUnit">Must be less than or equal to TimeUnit.Week</param>
        /// <param name="unit"></param>
        /// <param name="showQuantityAs">How to show the quantity.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentOutOfRangeException">Is thrown when timeUnit is larger than TimeUnit.Week</exception>
        public virtual string TimeSpanHumanize(TimeUnit timeUnit, int unit, ShowQuantityAs showQuantityAs)
        {
            if (timeUnit > TimeUnit.Week)
            {
                throw new ArgumentOutOfRangeException("timeUnit", "There's no meaningful way to humanize passed timeUnit.");
            }

            return(GetResourceForTimeSpan(timeUnit, unit, showQuantityAs));
        }
Exemplo n.º 3
0
        private static string ToQuantity(this string input, int quantity, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric, string format = null, IFormatProvider formatProvider = null)
        {
            var transformedInput = quantity == 1
                ? input.Singularize(inputIsKnownToBePlural: false)
                : input.Pluralize(inputIsKnownToBeSingular: false);

            if (showQuantityAs == ShowQuantityAs.None)
                return transformedInput;

            if (showQuantityAs == ShowQuantityAs.Numeric)
                return string.Format(formatProvider, "{0} {1}", quantity.ToString(format, formatProvider), transformedInput);

            return string.Format("{0} {1}", quantity.ToWords(), transformedInput);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Prefixes the provided word with the number and accordingly pluralizes or singularizes the word
        /// </summary>
        /// <param name="input">The word to be prefixes</param>
        /// <param name="quantity">The quantity of the word</param>
        /// <param name="showQuantityAs">How to show the quantity. Numeric by default</param>
        /// <example>
        /// "request".ToQuantity(0) => "0 requests"
        /// "request".ToQuantity(1) => "1 request"
        /// "request".ToQuantity(2) => "2 requests"
        /// "men".ToQuantity(2) => "2 men"
        /// "process".ToQuantity(1200, ShowQuantityAs.Words) => "one thousand two hundred processes"
        /// </example>
        /// <returns></returns>
        public static string ToQuantity(this string input, int quantity, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric)
        {
            var transformedInput = quantity == 1
                ? input.Singularize(Plurality.CouldBeEither)
                : input.Pluralize(Plurality.CouldBeEither);

            if (showQuantityAs == ShowQuantityAs.None)
                return transformedInput;

            if (showQuantityAs == ShowQuantityAs.Numeric)
                return string.Format("{0} {1}", quantity, transformedInput);

            return string.Format("{0} {1}", quantity.ToWords(), transformedInput);
        }
        /// <summary>
        /// Prefixes the provided word with the number and accordingly pluralizes or singularizes the word
        /// </summary>
        /// <param name="input">The word to be prefixes</param>
        /// <param name="quantity">The quantity of the word</param>
        /// <param name="showQuantityAs">How to show the quantity. Numeric by default</param>
        /// <example>
        /// "request".ToQuantity(0) => "0 requests"
        /// "request".ToQuantity(1) => "1 request"
        /// "request".ToQuantity(2) => "2 requests"
        /// "men".ToQuantity(2) => "2 men"
        /// "process".ToQuantity(1200, ShowQuantityAs.Words) => "one thousand two hundred processes"
        /// </example>
        /// <returns></returns>
        public static string ToQuantity(this string input, int quantity, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric)
        {
            var transformedInput = quantity == 1
                ? input.Singularize(Plurality.CouldBeEither)
                : input.Pluralize(Plurality.CouldBeEither);

            if (showQuantityAs == ShowQuantityAs.None)
            {
                return(transformedInput);
            }

            if (showQuantityAs == ShowQuantityAs.Numeric)
            {
                return(string.Format("{0} {1}", quantity, transformedInput));
            }

            return(string.Format("{0} {1}", quantity.ToWords(), transformedInput));
        }
        private static string ToQuantity(this string input, long quantity, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric, string format = null, IFormatProvider formatProvider = null)
        {
            var transformedInput = quantity == 1
                ? input.Singularize(inputIsKnownToBePlural: false)
                : input.Pluralize(inputIsKnownToBeSingular: false);

            if (showQuantityAs == ShowQuantityAs.None)
            {
                return(transformedInput);
            }

            if (showQuantityAs == ShowQuantityAs.Numeric)
            {
                return(string.Format(formatProvider, "{0} {1}", quantity.ToString(format, formatProvider), transformedInput));
            }

            return(string.Format("{0} {1}", quantity.ToWords(), transformedInput));
        }
Exemplo n.º 7
0
        private static string ToQuantity(this string input, int quantity, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric, string format = null, IFormatProvider formatProvider = null)
        {
            var transformedInput = quantity == 1
                ? input.Singularize(Plurality.CouldBeEither)
                : input.Pluralize(Plurality.CouldBeEither);

            if (showQuantityAs == ShowQuantityAs.None)
            {
                return(transformedInput);
            }

            if (showQuantityAs == ShowQuantityAs.Numeric)
            {
                return(string.Format(formatProvider, "{0} {1}", quantity.ToString(format, formatProvider), transformedInput));
            }

            return(string.Format("{0} {1}", quantity.ToWords(), transformedInput));
        }
Exemplo n.º 8
0
        // Token: 0x060000F8 RID: 248 RVA: 0x000039E0 File Offset: 0x00001BE0
        private static string ToQuantity(this string input, long quantity, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric, string format = null, IFormatProvider formatProvider = null)
        {
            string transformedInput = (quantity == 1L) ? input.Singularize(false) : input.Pluralize(false);

            if (showQuantityAs == ShowQuantityAs.None)
            {
                return(transformedInput);
            }
            if (showQuantityAs == ShowQuantityAs.Numeric)
            {
                return(string.Format(formatProvider, "{0} {1}", new object[]
                {
                    quantity.ToString(format, formatProvider),
                    transformedInput
                }));
            }
            return(string.Format("{0} {1}", new object[]
            {
                quantity.ToWords(null),
                transformedInput
            }));
        }
Exemplo n.º 9
0
        public static void Verify(string expectedString, int unit, TimeUnit timeUnit, Tense tense, double?precision = null, CultureInfo culture = null, DateTime?baseDate = null, DateTime?baseDateUtc = null, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric)
        {
            if (precision.HasValue)
            {
                Configurator.DateTimeHumanizeStrategy = new PrecisionDateTimeHumanizeStrategy(precision.Value);
            }
            else
            {
                Configurator.DateTimeHumanizeStrategy = new DefaultDateTimeHumanizeStrategy();
            }

            var deltaFromNow = new TimeSpan();

            unit = Math.Abs(unit);

            if (tense == Tense.Past)
            {
                unit = -unit;
            }

            switch (timeUnit)
            {
            case TimeUnit.Millisecond:
                deltaFromNow = TimeSpan.FromMilliseconds(unit);
                break;

            case TimeUnit.Second:
                deltaFromNow = TimeSpan.FromSeconds(unit);
                break;

            case TimeUnit.Minute:
                deltaFromNow = TimeSpan.FromMinutes(unit);
                break;

            case TimeUnit.Hour:
                deltaFromNow = TimeSpan.FromHours(unit);
                break;

            case TimeUnit.Day:
                deltaFromNow = TimeSpan.FromDays(unit);
                break;

            case TimeUnit.Month:
                deltaFromNow = TimeSpan.FromDays(unit * 31);
                break;

            case TimeUnit.Year:
                deltaFromNow = TimeSpan.FromDays(unit * 366);
                break;
            }

            if (baseDate == null)
            {
                VerifyWithCurrentDate(expectedString, deltaFromNow, culture, showQuantityAs);
                VerifyWithDateInjection(expectedString, deltaFromNow, culture, showQuantityAs);
            }
            else
            {
                VerifyWithDate(expectedString, deltaFromNow, culture, baseDate.Value, baseDateUtc.Value, showQuantityAs);
            }
        }
Exemplo n.º 10
0
 static void VerifyWithDate(string expectedString, TimeSpan deltaFromBase, CultureInfo culture, DateTime baseDate, DateTime baseDateUtc, ShowQuantityAs showQuantityAs)
 {
     Assert.Equal(expectedString, baseDateUtc.Add(deltaFromBase).Humanize(utcDate: true, dateToCompareAgainst: baseDateUtc, culture: culture, showQuantityAs: showQuantityAs));
     Assert.Equal(expectedString, baseDate.Add(deltaFromBase).Humanize(false, baseDate, culture: culture, showQuantityAs: showQuantityAs));
 }
Exemplo n.º 11
0
        static void VerifyWithDateInjection(string expectedString, TimeSpan deltaFromNow, CultureInfo culture, ShowQuantityAs showQuantityAs)
        {
            var utcNow = new DateTime(2013, 6, 20, 9, 58, 22, DateTimeKind.Utc);
            var now    = new DateTime(2013, 6, 20, 11, 58, 22, DateTimeKind.Local);

            VerifyWithDate(expectedString, deltaFromNow, culture, now, utcNow, showQuantityAs);
        }
Exemplo n.º 12
0
        static void VerifyWithCurrentDate(string expectedString, TimeSpan deltaFromNow, CultureInfo culture, ShowQuantityAs showQuantityAs)
        {
            var utcNow   = DateTime.UtcNow;
            var localNow = DateTime.Now;

            // feels like the only way to avoid breaking tests because CPU ticks over is to inject the base date
            VerifyWithDate(expectedString, deltaFromNow, culture, localNow, utcNow, showQuantityAs);
        }
        // http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time
        /// <summary>
        /// Calculates the distance of time in words between two provided dates
        /// </summary>
        public static string DefaultHumanize(DateTime input, DateTime comparisonBase, CultureInfo culture, ShowQuantityAs showQuantityAs)
        {
            var tense = input > comparisonBase ? Tense.Future : Tense.Past;
            var ts    = new TimeSpan(Math.Abs(comparisonBase.Ticks - input.Ticks));

            var formatter = Configurator.GetFormatter(culture);

            if (ts.TotalMilliseconds < 500)
            {
                return(formatter.DateHumanize(TimeUnit.Millisecond, tense, 0, showQuantityAs));
            }

            if (ts.TotalSeconds < 60)
            {
                return(formatter.DateHumanize(TimeUnit.Second, tense, ts.Seconds, showQuantityAs));
            }

            if (ts.TotalSeconds < 120)
            {
                return(formatter.DateHumanize(TimeUnit.Minute, tense, 1, showQuantityAs));
            }

            if (ts.TotalMinutes < 60)
            {
                return(formatter.DateHumanize(TimeUnit.Minute, tense, ts.Minutes, showQuantityAs));
            }

            if (ts.TotalMinutes < 90)
            {
                return(formatter.DateHumanize(TimeUnit.Hour, tense, 1, showQuantityAs));
            }

            if (ts.TotalHours < 24)
            {
                return(formatter.DateHumanize(TimeUnit.Hour, tense, ts.Hours, showQuantityAs));
            }

            if (ts.TotalHours < 48)
            {
                var days = Math.Abs((input.Date - comparisonBase.Date).Days);
                return(formatter.DateHumanize(TimeUnit.Day, tense, days, showQuantityAs));
            }

            if (ts.TotalDays < 28)
            {
                return(formatter.DateHumanize(TimeUnit.Day, tense, ts.Days, showQuantityAs));
            }

            if (ts.TotalDays >= 28 && ts.TotalDays < 30)
            {
                if (comparisonBase.Date.AddMonths(tense == Tense.Future ? 1 : -1) == input.Date)
                {
                    return(formatter.DateHumanize(TimeUnit.Month, tense, 1, showQuantityAs));
                }
                return(formatter.DateHumanize(TimeUnit.Day, tense, ts.Days, showQuantityAs));
            }

            if (ts.TotalDays < 345)
            {
                int months = Convert.ToInt32(Math.Floor(ts.TotalDays / 29.5));
                return(formatter.DateHumanize(TimeUnit.Month, tense, months, showQuantityAs));
            }

            int years = Convert.ToInt32(Math.Floor(ts.TotalDays / 365));

            if (years == 0)
            {
                years = 1;
            }

            return(formatter.DateHumanize(TimeUnit.Year, tense, years, showQuantityAs));
        }
Exemplo n.º 14
0
        /// <summary>
        /// Turns the current or provided date into a human readable sentence
        /// </summary>
        /// <param name="input">The date to be humanized</param>
        /// <param name="dateToCompareAgainst">Date to compare the input against. If null, current date is used as base</param>
        /// <param name="culture">Culture to use. If null, current thread's UI culture is used.</param>
        /// <param name="showQuantityAs">How to show the quantity. Numeric by default</param>
        /// <returns>distance of time in words</returns>
        public static string Humanize(this DateTimeOffset input, DateTimeOffset?dateToCompareAgainst = null, CultureInfo culture = null, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric)
        {
            var comparisonBase = dateToCompareAgainst ?? DateTimeOffset.UtcNow;

            return(Configurator.DateTimeOffsetHumanizeStrategy.Humanize(input, comparisonBase, culture, showQuantityAs));
        }
Exemplo n.º 15
0
 // Token: 0x060000F4 RID: 244 RVA: 0x000039AB File Offset: 0x00001BAB
 public static string ToQuantity(this string input, int quantity, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric)
 {
     return(input.ToQuantity((long)quantity, showQuantityAs, null, null));
 }
        /// <summary>
        /// Turns a TimeSpan into a human readable form. E.g. 1 day.
        /// </summary>
        /// <param name="timeSpan"></param>
        /// <param name="precision">The maximum number of time units to return.</param>
        /// <param name="countEmptyUnits">Controls whether empty time units should be counted towards maximum number of time units. Leading empty time units never count.</param>
        /// <param name="culture">Culture to use. If null, current thread's UI culture is used.</param>
        /// <param name="maxUnit">The maximum unit of time to output.</param>
        /// <param name="showQuantityAs">How to show the quantity. Numeric by default</param>
        /// <returns></returns>
        public static string Humanize(this TimeSpan timeSpan, int precision, bool countEmptyUnits, CultureInfo culture = null, TimeUnit maxUnit = TimeUnit.Week, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric)
        {
            var timeParts = GetTimeParts(timeSpan, culture, maxUnit, showQuantityAs);

            if (!countEmptyUnits)
            {
                timeParts = timeParts.Where(x => x != null);
            }
            timeParts = timeParts.Take(precision);
            if (countEmptyUnits)
            {
                timeParts = timeParts.Where(x => x != null);
            }
            return(string.Join(", ", timeParts));
        }
Exemplo n.º 17
0
 /// <summary>
 /// Returns the string representation of the provided DateTime
 /// </summary>
 /// <param name="timeUnit"></param>
 /// <param name="timeUnitTense"></param>
 /// <param name="unit"></param>
 /// <param name="showQuantityAs">How to show the quantity.</param>
 /// <returns></returns>
 public virtual string DateHumanize(TimeUnit timeUnit, Tense timeUnitTense, int unit, ShowQuantityAs showQuantityAs)
 {
     return(GetResourceForDate(timeUnit, timeUnitTense, unit, showQuantityAs));
 }
Exemplo n.º 18
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="resourceKey"></param>
 /// <param name="number"></param>
 /// <param name="showQuantityAs">How to show the quantity.</param>
 /// <returns></returns>
 protected virtual string Format(string resourceKey, int number, ShowQuantityAs showQuantityAs)
 {
     return(Resources.GetResource(GetResourceKey(resourceKey, number), _culture).FormatWith((showQuantityAs == ShowQuantityAs.Words ? (object)number.ToWords(_culture) : number)));
 }
 /// <summary>
 /// Returns localized &amp; humanized distance of time between two dates; given a specific precision.
 /// </summary>
 public string Humanize(DateTime input, DateTime comparisonBase, CultureInfo culture, ShowQuantityAs showQuantityAs)
 {
     return(DateTimeHumanizeAlgorithms.PrecisionHumanize(input, comparisonBase, _precision, culture, showQuantityAs));
 }
 private static string GetTimePart(IFormatter formatter, TimeUnit timeUnit, int unit, ShowQuantityAs showQuantityAs)
 {
     return(unit != 0
         ? formatter.TimeSpanHumanize(timeUnit, unit, showQuantityAs)
         : null);
 }
Exemplo n.º 21
0
        public void CanSpecifyHowToShowQuantity(int ms, int precision, ShowQuantityAs showQuantityAs, string expected)
        {
            var actual = TimeSpan.FromMilliseconds(ms).Humanize(precision: precision, showQuantityAs: showQuantityAs);

            Assert.Equal(expected, actual);
        }
 /// <summary>
 /// Turns a TimeSpan into a human readable form. E.g. 1 day.
 /// </summary>
 /// <param name="timeSpan"></param>
 /// <param name="precision">The maximum number of time units to return. Defaulted is 1 which means the largest unit is returned</param>
 /// <param name="culture">Culture to use. If null, current thread's UI culture is used.</param>
 /// <param name="maxUnit">The maximum unit of time to output.</param>
 /// <param name="showQuantityAs">How to show the quantity. Numeric by default</param>
 /// <returns></returns>
 public static string Humanize(this TimeSpan timeSpan, int precision = 1, CultureInfo culture = null, TimeUnit maxUnit = TimeUnit.Week, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric)
 {
     return(Humanize(timeSpan, precision, false, culture, maxUnit, showQuantityAs));
 }
Exemplo n.º 23
0
        /// <summary>
        /// Turns the current or provided date into a human readable sentence
        /// </summary>
        /// <param name="input">The date to be humanized</param>
        /// <param name="utcDate">Boolean value indicating whether the date is in UTC or local</param>
        /// <param name="dateToCompareAgainst">Date to compare the input against. If null, current date is used as base</param>
        /// <param name="culture">Culture to use. If null, current thread's UI culture is used.</param>
        /// <param name="showQuantityAs">How to show the quantity. Numeric by default</param>
        /// <returns>distance of time in words</returns>
        public static string Humanize(this DateTime input, bool utcDate = true, DateTime?dateToCompareAgainst = null, CultureInfo culture = null, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric)
        {
            var comparisonBase = dateToCompareAgainst ?? DateTime.UtcNow;

            if (!utcDate)
            {
                comparisonBase = comparisonBase.ToLocalTime();
            }

            return(Configurator.DateTimeHumanizeStrategy.Humanize(input, comparisonBase, culture, showQuantityAs));
        }
        private static IEnumerable <string> GetTimeParts(TimeSpan timespan, CultureInfo culture, TimeUnit maxUnit, ShowQuantityAs showQuantityAs)
        {
            var weeks        = maxUnit > TimeUnit.Day ? timespan.Days / 7 : 0;
            var days         = maxUnit > TimeUnit.Day ? timespan.Days % 7 : (int)timespan.TotalDays;
            var hours        = maxUnit > TimeUnit.Hour ? timespan.Hours : (int)timespan.TotalHours;
            var minutes      = maxUnit > TimeUnit.Minute ? timespan.Minutes : (int)timespan.TotalMinutes;
            var seconds      = maxUnit > TimeUnit.Second ? timespan.Seconds : (int)timespan.TotalSeconds;
            var milliseconds = maxUnit > TimeUnit.Millisecond ? timespan.Milliseconds : (int)timespan.TotalMilliseconds;

            var outputWeeks        = weeks > 0 && maxUnit == TimeUnit.Week;
            var outputDays         = (outputWeeks || days > 0) && maxUnit >= TimeUnit.Day;
            var outputHours        = (outputDays || hours > 0) && maxUnit >= TimeUnit.Hour;
            var outputMinutes      = (outputHours || minutes > 0) && maxUnit >= TimeUnit.Minute;
            var outputSeconds      = (outputMinutes || seconds > 0) && maxUnit >= TimeUnit.Second;
            var outputMilliseconds = (outputSeconds || milliseconds > 0) && maxUnit >= TimeUnit.Millisecond;

            var formatter = Configurator.GetFormatter(culture);

            if (outputWeeks)
            {
                yield return(GetTimePart(formatter, TimeUnit.Week, weeks, showQuantityAs));
            }
            if (outputDays)
            {
                yield return(GetTimePart(formatter, TimeUnit.Day, days, showQuantityAs));
            }
            if (outputHours)
            {
                yield return(GetTimePart(formatter, TimeUnit.Hour, hours, showQuantityAs));
            }
            if (outputMinutes)
            {
                yield return(GetTimePart(formatter, TimeUnit.Minute, minutes, showQuantityAs));
            }
            if (outputSeconds)
            {
                yield return(GetTimePart(formatter, TimeUnit.Second, seconds, showQuantityAs));
            }
            if (outputMilliseconds)
            {
                yield return(GetTimePart(formatter, TimeUnit.Millisecond, milliseconds, showQuantityAs));
            }
            else
            {
                yield return(formatter.TimeSpanHumanize_Zero());
            }
        }
Exemplo n.º 25
0
        private string GetResourceForDate(TimeUnit unit, Tense timeUnitTense, int count, ShowQuantityAs showQuantityAs)
        {
            string resourceKey = ResourceKeys.DateHumanize.GetResourceKey(unit, timeUnitTense: timeUnitTense, count: count);

            return(count == 1 ? Format(resourceKey) : Format(resourceKey, count, showQuantityAs));
        }
Exemplo n.º 26
0
 /// <summary>
 /// Prefixes the provided word with the number and accordingly pluralizes or singularizes the word
 /// </summary>
 /// <param name="input">The word to be prefixed</param>
 /// <param name="quantity">The quantity of the word</param>
 /// <param name="showQuantityAs">How to show the quantity. Numeric by default</param>
 /// <example>
 /// "request".ToQuantity(0) => "0 requests"
 /// "request".ToQuantity(1) => "1 request"
 /// "request".ToQuantity(2) => "2 requests"
 /// "men".ToQuantity(2) => "2 men"
 /// "process".ToQuantity(1200, ShowQuantityAs.Words) => "one thousand two hundred processes"
 /// </example>
 /// <returns></returns>
 public static string ToQuantity(this string input, int quantity, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric)
 {
     return input.ToQuantity(quantity, showQuantityAs, format: null, formatProvider: null);
 }
Exemplo n.º 27
0
        private string GetResourceForTimeSpan(TimeUnit unit, int count, ShowQuantityAs showQuantityAs)
        {
            string resourceKey = ResourceKeys.TimeSpanHumanize.GetResourceKey(unit, count);

            return(count == 1 ? Format(resourceKey) : Format(resourceKey, count, showQuantityAs));
        }
Exemplo n.º 28
0
 /// <summary>
 /// Prefixes the provided word with the number and accordingly pluralizes or singularizes the word
 /// </summary>
 /// <param name="input">The word to be prefixed</param>
 /// <param name="quantity">The quantity of the word</param>
 /// <param name="showQuantityAs">How to show the quantity. Numeric by default</param>
 /// <example>
 /// "request".ToQuantity(0) => "0 requests"
 /// "request".ToQuantity(1) => "1 request"
 /// "request".ToQuantity(2) => "2 requests"
 /// "men".ToQuantity(2) => "2 men"
 /// "process".ToQuantity(1200, ShowQuantityAs.Words) => "one thousand two hundred processes"
 /// </example>
 /// <returns></returns>
 public static string ToQuantity(this string input, long quantity, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric)
 {
     return(input.ToQuantity(quantity, showQuantityAs, format: null, formatProvider: null));
 }
 public void CanSpecifyHowToShowQuantity(int unit, TimeUnit timeUnit, Tense tense, ShowQuantityAs showQuantityAs, string expected)
 {
     DateHumanize.Verify(expected, unit, timeUnit, tense, showQuantityAs: showQuantityAs);
 }
 /// <summary>
 /// Calculates the distance of time in words between two provided dates
 /// </summary>
 public string Humanize(DateTimeOffset input, DateTimeOffset comparisonBase, CultureInfo culture, ShowQuantityAs showQuantityAs)
 {
     return(DateTimeHumanizeAlgorithms.DefaultHumanize(input.UtcDateTime, comparisonBase.UtcDateTime, culture, showQuantityAs));
 }
        /// <summary>
        /// Returns localized &amp; humanized distance of time between two dates; given a specific precision.
        /// </summary>
        public static string PrecisionHumanize(DateTime input, DateTime comparisonBase, double precision, CultureInfo culture, ShowQuantityAs showQuantityAs)
        {
            var ts    = new TimeSpan(Math.Abs(comparisonBase.Ticks - input.Ticks));
            var tense = input > comparisonBase ? Tense.Future : Tense.Past;

            int seconds = ts.Seconds, minutes = ts.Minutes, hours = ts.Hours, days = ts.Days;
            int years = 0, months = 0;

            // start approximate from smaller units towards bigger ones
            if (ts.Milliseconds >= 999 * precision)
            {
                seconds += 1;
            }
            if (seconds >= 59 * precision)
            {
                minutes += 1;
            }
            if (minutes >= 59 * precision)
            {
                hours += 1;
            }
            if (hours >= 23 * precision)
            {
                days += 1;
            }

            // month calculation
            if (days >= 30 * precision & days <= 31)
            {
                months = 1;
            }
            if (days > 31 && days < 365 * precision)
            {
                int factor    = Convert.ToInt32(Math.Floor((double)days / 30));
                int maxMonths = Convert.ToInt32(Math.Ceiling((double)days / 30));
                months = (days >= 30 * (factor + precision)) ? maxMonths : maxMonths - 1;
            }

            // year calculation
            if (days >= 365 * precision && days <= 366)
            {
                years = 1;
            }
            if (days > 365)
            {
                int factor    = Convert.ToInt32(Math.Floor((double)days / 365));
                int maxMonths = Convert.ToInt32(Math.Ceiling((double)days / 365));
                years = (days >= 365 * (factor + precision)) ? maxMonths : maxMonths - 1;
            }

            // start computing result from larger units to smaller ones
            var formatter = Configurator.GetFormatter(culture);

            if (years > 0)
            {
                return(formatter.DateHumanize(TimeUnit.Year, tense, years, showQuantityAs));
            }
            if (months > 0)
            {
                return(formatter.DateHumanize(TimeUnit.Month, tense, months, showQuantityAs));
            }
            if (days > 0)
            {
                return(formatter.DateHumanize(TimeUnit.Day, tense, days, showQuantityAs));
            }
            if (hours > 0)
            {
                return(formatter.DateHumanize(TimeUnit.Hour, tense, hours, showQuantityAs));
            }
            if (minutes > 0)
            {
                return(formatter.DateHumanize(TimeUnit.Minute, tense, minutes, showQuantityAs));
            }
            if (seconds > 0)
            {
                return(formatter.DateHumanize(TimeUnit.Second, tense, seconds, showQuantityAs));
            }
            return(formatter.DateHumanize(TimeUnit.Millisecond, tense, 0, showQuantityAs));
        }