Skip to content
Snippets Groups Projects
Commit 7cecf8de authored by Niko Ehrenfeuchter's avatar Niko Ehrenfeuchter :keyboard:
Browse files

Make the time delta description suffix optional.

Refers to #25
parent 0b515eb1
No related branches found
No related tags found
No related merge requests found
...@@ -34,14 +34,18 @@ namespace ATxCommon ...@@ -34,14 +34,18 @@ namespace ATxCommon
/// Convert a number of seconds to a human readable string. /// Convert a number of seconds to a human readable string.
/// </summary> /// </summary>
/// <param name="delta">The time span in seconds.</param> /// <param name="delta">The time span in seconds.</param>
/// <param name="addDesc">Add a description suffix like "ago" or "in the future".</param>
/// <returns>A string describing the duration, e.g. "2 hours 34 minutes".</returns> /// <returns>A string describing the duration, e.g. "2 hours 34 minutes".</returns>
public static string SecondsToHuman(long delta) { public static string SecondsToHuman(long delta, bool addDesc = true) {
var desc = "ago"; var desc = " ago";
if (delta < 0) { if (delta < 0) {
delta *= -1; delta *= -1;
desc = "in the future"; desc = " in the future";
} }
if (!addDesc)
desc = "";
const int second = 1; const int second = 1;
const int minute = second * 60; const int minute = second * 60;
const int hour = minute * 60; const int hour = minute * 60;
...@@ -51,27 +55,27 @@ namespace ATxCommon ...@@ -51,27 +55,27 @@ namespace ATxCommon
const int year = day * 365; const int year = day * 365;
if (delta < minute) if (delta < minute)
return $"{delta} seconds {desc}"; return $"{delta} seconds{desc}";
if (delta < 2 * minute) if (delta < 2 * minute)
return $"a minute {desc}"; return $"a minute{desc}";
if (delta < hour) if (delta < hour)
return $"{delta / minute} minutes {desc}"; return $"{delta / minute} minutes{desc}";
if (delta < day) { if (delta < day) {
var hours = delta / hour; var hours = delta / hour;
var mins = (delta - hours * hour) / minute; var mins = (delta - hours * hour) / minute;
if (mins > 0) if (mins > 0)
return $"{hours} hours {mins} minutes {desc}"; return $"{hours} hours {mins} minutes{desc}";
return $"{hours} hours {desc}"; return $"{hours} hours{desc}";
} }
if (delta < 2 * week) if (delta < 2 * week)
return $"{delta / day} days ${desc}"; return $"{delta / day} days{desc}";
if (delta < 2 * month) if (delta < 2 * month)
return $"{delta / week} weeks {desc}"; return $"{delta / week} weeks{desc}";
if (delta < year) { if (delta < year) {
var months = delta / month; var months = delta / month;
...@@ -82,7 +86,7 @@ namespace ATxCommon ...@@ -82,7 +86,7 @@ namespace ATxCommon
ret += $" {weeks} weeks"; ret += $" {weeks} weeks";
if (days > 0) if (days > 0)
ret += $" {days} days"; ret += $" {days} days";
return $"{ret} {desc}"; return $"{ret}{desc}";
} }
if (delta < 10 * year) { if (delta < 10 * year) {
...@@ -94,34 +98,37 @@ namespace ATxCommon ...@@ -94,34 +98,37 @@ namespace ATxCommon
ret += $" {months} months"; ret += $" {months} months";
if (days > 0) if (days > 0)
ret += $" {days} days"; ret += $" {days} days";
return $"{ret} {desc}"; return $"{ret}{desc}";
} }
return $"{delta / year} years {desc}"; return $"{delta / year} years{desc}";
} }
/// <summary> /// <summary>
/// Wrapper to use <see cref="SecondsToHuman"/> with minutes as input. /// Wrapper to use <see cref="SecondsToHuman"/> with minutes as input.
/// </summary> /// </summary>
/// <param name="delta">The time span in minutes.</param> /// <param name="delta">The time span in minutes.</param>
/// <param name="addDesc">Add a description suffix like "ago" or "in the future".</param>
/// <returns>A string describing the duration, e.g. "2 hours 34 minutes".</returns> /// <returns>A string describing the duration, e.g. "2 hours 34 minutes".</returns>
public static string MinutesToHuman(long delta) { public static string MinutesToHuman(long delta, bool addDesc = true) {
return SecondsToHuman(delta * 60); return SecondsToHuman(delta * 60, addDesc);
} }
/// <summary> /// <summary>
/// Wrapper to use <see cref="SecondsToHuman"/> with days as input. /// Wrapper to use <see cref="SecondsToHuman"/> with days as input.
/// </summary> /// </summary>
/// <param name="delta">The time span in days.</param> /// <param name="delta">The time span in days.</param>
/// <param name="addDesc">Add a description suffix like "ago" or "in the future".</param>
/// <returns>A string describing the duration, e.g. "12 days" or "3 weeks".</returns> /// <returns>A string describing the duration, e.g. "12 days" or "3 weeks".</returns>
public static string DaysToHuman(long delta) { public static string DaysToHuman(long delta, bool addDesc = true) {
return MinutesToHuman(delta * 60 * 24); return MinutesToHuman(delta * 60 * 24, addDesc);
} }
/// <summary> /// <summary>
/// Wrapper to convert a date into a human readable string relative to now. /// Wrapper to convert a date into a human readable string relative to now.
/// </summary> /// </summary>
/// <param name="refDate">The reference DateTime to check.</param> /// <param name="refDate">The reference DateTime to check.</param>
/// <param name="addDesc">Add a description suffix like "ago" or "in the future".</param>
/// <returns>A string describing the delta, e.g. "12 days" or "3 weeks".</returns> /// <returns>A string describing the delta, e.g. "12 days" or "3 weeks".</returns>
public static string HumanSince(DateTime refDate) { public static string HumanSince(DateTime refDate) {
if (refDate == DateTime.MinValue) { if (refDate == DateTime.MinValue) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment