TimeStamp.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Globalization;
  3. using JetBrains.Annotations;
  4. using Unity.Cloud.Collaborate.Settings;
  5. namespace Unity.Cloud.Collaborate.Utilities
  6. {
  7. /// <summary>
  8. /// Static class that presents methods to provide timestamps for the UI.
  9. /// </summary>
  10. static class TimeStamp
  11. {
  12. /// <summary>
  13. /// Bool to decide whether timestamps should be exact values or relative values.
  14. /// </summary>
  15. public static bool UseRelativeTimeStamps =>
  16. CollabSettingsManager.Get(CollabSettings.settingRelativeTimestamp, fallback: true);
  17. /// <summary>
  18. /// Values to translate a number to a string representation.
  19. /// </summary>
  20. static readonly string[] k_UnitsMap = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" };
  21. /// <summary>
  22. /// Get the localized or relative timestamp for the given DateTime based on the current settings.
  23. /// </summary>
  24. /// <param name="dateTime">DateTime to convert.</param>
  25. /// <returns>String representation of the given DateTime.</returns>
  26. [NotNull]
  27. public static string GetTimeStamp(DateTimeOffset dateTime)
  28. {
  29. // ReSharper disable once ConditionIsAlwaysTrueOrFalse
  30. return UseRelativeTimeStamps
  31. ? GetElapsedTime(dateTime)
  32. : GetLocalisedTimeStamp(dateTime);
  33. }
  34. /// <summary>
  35. /// Get the localised timestamp for the given DateTime.
  36. /// </summary>
  37. /// <param name="dateTime">DateTime to convert.</param>
  38. /// <returns>Localised string representation of the given DateTime.</returns>
  39. [NotNull]
  40. public static string GetLocalisedTimeStamp(DateTimeOffset dateTime)
  41. {
  42. return dateTime.ToString(CultureInfo.CurrentCulture.DateTimeFormat.FullDateTimePattern);
  43. }
  44. // Original credit: https://codereview.stackexchange.com/questions/93239/get-elapsed-time-as-human-friendly-string
  45. /// <summary>
  46. /// Convert a DateTime into a relative timestamp.
  47. /// </summary>
  48. /// <param name="dateTime">Datetime to calculate the timestamp from.</param>
  49. /// <returns>Relative timestamp for the given DateTime.</returns>
  50. [NotNull]
  51. static string GetElapsedTime(DateTimeOffset dateTime)
  52. {
  53. var offset = DateTimeOffset.Now.Subtract(dateTime);
  54. // The trick: make variable contain date and time representing the desired timespan,
  55. // having +1 in each date component.
  56. var date = DateTimeOffset.MinValue + offset;
  57. return ProcessPeriod(date.Year - 1, date.Month - 1, "year")
  58. ?? ProcessPeriod(date.Month - 1, date.Day - 1, "month")
  59. ?? ProcessPeriod(date.Day - 1, date.Hour, "day", "Yesterday")
  60. ?? ProcessPeriod(date.Hour, date.Minute, "hour")
  61. ?? ProcessPeriod(date.Minute, date.Second, "minute")
  62. ?? ProcessPeriod(date.Second, 0, "second")
  63. ?? "Right now";
  64. }
  65. // Original credit: https://codereview.stackexchange.com/questions/93239/get-elapsed-time-as-human-friendly-string
  66. /// <summary>
  67. /// Output the string representation for the given time frame. If it's not in that time frame, it returns null.
  68. /// </summary>
  69. /// <param name="value">Bigger time value.</param>
  70. /// <param name="subValue">Smaller time value eg: minutes if value is hours.</param>
  71. /// <param name="name">Name of the period.</param>
  72. /// <param name="singularName">Name for period that is singular. Null if it's not singular eg: yesterday.</param>
  73. /// <returns>String representation of the period, or null if it's outside of it.</returns>
  74. [CanBeNull]
  75. static string ProcessPeriod(int value, int subValue, string name, string singularName = null)
  76. {
  77. // If the value is less than this time frame, skip.
  78. if (value == 0)
  79. {
  80. return null;
  81. }
  82. // If a multiple of this time frame eg: 20 minutes.
  83. if (value != 1)
  84. {
  85. // Convert values specified to string numbers.
  86. var stringValue = value <k_UnitsMap.Length ? k_UnitsMap[value] : value.ToString();
  87. return subValue == 0
  88. ? $"{stringValue.FirstCharToUpper()} {name}s ago"
  89. : $"About {stringValue} {name}s ago";
  90. }
  91. // Special case for one-off names eg: yesterday.
  92. if (!string.IsNullOrEmpty(singularName))
  93. {
  94. return singularName;
  95. }
  96. // Singular time frame eg: an hour, a minute.
  97. var articleSuffix = name[0] == 'h' ? "n" : string.Empty;
  98. return subValue == 0
  99. ? $"A{articleSuffix} {name} ago"
  100. : $"About a{articleSuffix} {name} ago";
  101. }
  102. }
  103. }