private string GetResourceForDate(TimeUnit unit, Tense timeUnitTense, int count, DateTimeExpressionProvider dateTimeExpressionProvider = null) { var resourceKey = ResourceKeys.DateHumanize.GetResourceKey(unit, timeUnitTense: timeUnitTense, count: count, dateTimeExpressionProvider: dateTimeExpressionProvider); return(count == 1 ? Format(resourceKey) : Format(resourceKey, count)); }
/// <summary> /// Calculates the distance of time in words between two provided dates /// </summary> public string Humanize(DateTime input, DateTime comparisonBase, CultureInfo culture, DateTimeExpressionProvider dateTimeTextProvider) { return(DateTimeHumanizeAlgorithms.DefaultHumanize(input, comparisonBase, culture, dateTimeTextProvider)); }
/// <summary> /// Returns the string representation of the provided DateTime /// </summary> /// <param name="timeUnit"></param> /// <param name="timeUnitTense"></param> /// <param name="unit"></param> /// <returns></returns> public virtual string DateHumanize(TimeUnit timeUnit, Tense timeUnitTense, int unit, DateTimeExpressionProvider dateTimeTextProvider = null) { return(GetResourceForDate(timeUnit, timeUnitTense, unit, dateTimeTextProvider)); }
/// <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> /// <returns>distance of time in words</returns> public static string Humanize(this DateTime input, bool utcDate = true, DateTime?dateToCompareAgainst = null, CultureInfo culture = null, DateTimeExpressionProvider dateTimeExpressionProvider = null) { var comparisonBase = dateToCompareAgainst ?? DateTime.UtcNow; if (!utcDate) { comparisonBase = comparisonBase.ToLocalTime(); } return(Configurator.DateTimeHumanizeStrategy.Humanize(input, comparisonBase, culture, dateTimeExpressionProvider)); }
/// <summary> /// Returns localized & humanized distance of time between two dates; given a specific precision. /// </summary> public static string PrecisionHumanize(DateTime input, DateTime comparisonBase, double precision, CultureInfo culture, DateTimeExpressionProvider dateTimeExpressionProvider = null) { 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) { var factor = Convert.ToInt32(Math.Floor((double)days / 30)); var 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) { var factor = Convert.ToInt32(Math.Floor((double)days / 365)); var 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, dateTimeExpressionProvider)); } if (months > 0) { return(formatter.DateHumanize(TimeUnit.Month, tense, months, dateTimeExpressionProvider)); } if (days > 0) { return(formatter.DateHumanize(TimeUnit.Day, tense, days, dateTimeExpressionProvider)); } if (hours > 0) { return(formatter.DateHumanize(TimeUnit.Hour, tense, hours, dateTimeExpressionProvider)); } if (minutes > 0) { return(formatter.DateHumanize(TimeUnit.Minute, tense, minutes, dateTimeExpressionProvider)); } if (seconds > 0) { return(formatter.DateHumanize(TimeUnit.Second, tense, seconds, dateTimeExpressionProvider)); } return(formatter.DateHumanize(TimeUnit.Millisecond, tense, 0, dateTimeExpressionProvider)); }
// 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, DateTimeExpressionProvider dateTimeExpressionProvider = null) { 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, dateTimeExpressionProvider)); } if (ts.TotalSeconds < 60) { return(formatter.DateHumanize(TimeUnit.Second, tense, ts.Seconds, dateTimeExpressionProvider)); } if (ts.TotalSeconds < 120) { return(formatter.DateHumanize(TimeUnit.Minute, tense, 1, dateTimeExpressionProvider)); } if (ts.TotalMinutes < 60) { return(formatter.DateHumanize(TimeUnit.Minute, tense, ts.Minutes, dateTimeExpressionProvider)); } if (ts.TotalMinutes < 90) { return(formatter.DateHumanize(TimeUnit.Hour, tense, 1, dateTimeExpressionProvider)); } if (ts.TotalHours < 24) { return(formatter.DateHumanize(TimeUnit.Hour, tense, ts.Hours, dateTimeExpressionProvider)); } if (ts.TotalHours < 48) { var days = Math.Abs((input.Date - comparisonBase.Date).Days); return(formatter.DateHumanize(TimeUnit.Day, tense, days, dateTimeExpressionProvider)); } if (ts.TotalDays < 28) { return(formatter.DateHumanize(TimeUnit.Day, tense, ts.Days, dateTimeExpressionProvider)); } 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, dateTimeExpressionProvider)); } return(formatter.DateHumanize(TimeUnit.Day, tense, ts.Days, dateTimeExpressionProvider)); } if (ts.TotalDays < 345) { var months = Convert.ToInt32(Math.Floor(ts.TotalDays / 29.5)); return(formatter.DateHumanize(TimeUnit.Month, tense, months, dateTimeExpressionProvider)); } var years = Convert.ToInt32(Math.Floor(ts.TotalDays / 365)); if (years == 0) { years = 1; } return(formatter.DateHumanize(TimeUnit.Year, tense, years, dateTimeExpressionProvider)); }
/// <summary> /// Generates Resource Keys accordning to convention. /// </summary> /// <param name="timeUnit">Time unit</param> /// <param name="timeUnitTense">Is time unit in future or past</param> /// <param name="count">Number of units, default is One.</param> /// <returns>Resource key, like DateHumanize_SingleMinuteAgo</returns> public static string GetResourceKey(TimeUnit timeUnit, Tense timeUnitTense, int count = 1, DateTimeExpressionProvider dateTimeExpressionProvider = null) { ValidateRange(count); if (count == 0) { return(Now); } var singularity = count == 1 ? Single : Multiple; dateTimeExpressionProvider ??= DateTimeExpressionProvider.Default; var tense = timeUnitTense == Tense.Future ? dateTimeExpressionProvider.GetFutureTimeExpression().ToString() : dateTimeExpressionProvider.GetPastTimeExpression().ToString(); var unit = timeUnit.ToString().ToQuantity(count, ShowQuantityAs.None); return(DateTimeFormat.FormatWith(singularity, unit, tense)); }