This commit is contained in:
159
server/util/DateUtil.cs
Normal file
159
server/util/DateUtil.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Sockeye.Util
|
||||
{
|
||||
internal static class DateUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// Is the current date after the referenced date by at least the duration specified
|
||||
/// </summary>
|
||||
/// <param name="startDate">UTC start point to compare to current UTC date</param>
|
||||
/// <param name="Hours"></param>
|
||||
/// <param name="Minutes"></param>
|
||||
/// <param name="Seconds"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsAfterDuration(DateTime startDate, int Hours, int Minutes = 0, int Seconds = 0)
|
||||
{
|
||||
TimeSpan ts = new TimeSpan(Hours, Minutes, Seconds);
|
||||
return IsAfterDuration(startDate, ts);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Is the current date after the referenced date by at least the timespan specified
|
||||
/// </summary>
|
||||
/// <param name="startDate">UTC start point to compare to current UTC date</param>
|
||||
/// <param name="tspan"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsAfterDuration(DateTime startDate, TimeSpan tspan)
|
||||
{
|
||||
if (DateTime.UtcNow - startDate < tspan)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// An internally consistent empty or not relevant date marker:
|
||||
/// January 1st 5555
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static DateTime EmptyDateValue
|
||||
{
|
||||
get
|
||||
{
|
||||
return new DateTime(5555, 1, 1);
|
||||
//Was going to use MaxValue but apparently that varies depending on culture
|
||||
// and PostgreSQL has issues with year 1 as it interprets as year 2001
|
||||
// so to be on safe side just defining one for all usage
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// returns a UTC short date, short time formatted date for local display to end user in logs, errors etc at the server level
|
||||
/// (Not related to UI display of dates and times)
|
||||
/// </summary>
|
||||
/// <param name="DateToDisplay"></param>
|
||||
/// <returns></returns>
|
||||
public static string ServerDateTimeString(DateTime DateToDisplay)
|
||||
{
|
||||
return DateToDisplay.ToLocalTime().ToString("g");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns current date/time in sortable format
|
||||
///(used for duplicate names by stringUtil and others)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string SortableShortCurrentDateTimeValue
|
||||
{
|
||||
get
|
||||
{
|
||||
return DateTime.Now.ToString("s");
|
||||
//Was going to use MaxValue but apparently that varies depending on culture
|
||||
// and PostgreSQL has issues with year 1 as it interprets as year 2001
|
||||
// so to be on safe side just defining one for all usage
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// returns passed in date as a string format ISO8661 UTC date (no conversion of date is done, it's assumed to be in UTC already)
|
||||
/// </summary>
|
||||
/// <param name="DateToDisplay"></param>
|
||||
/// <returns></returns>
|
||||
public static string UniversalISO8661Format(DateTime DateToDisplay)
|
||||
{
|
||||
DateTime dtUTC = new DateTime(DateToDisplay.Ticks, DateTimeKind.Utc);
|
||||
return dtUTC.ToString("o");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// returns passed in timespan to human readable format
|
||||
/// </summary>
|
||||
/// <param name="timeSpan"></param>
|
||||
/// <returns></returns>
|
||||
public static string FormatTimeSpan(TimeSpan timeSpan)
|
||||
{
|
||||
Func<Tuple<int, string>, string> tupleFormatter = t => $"{t.Item1} {t.Item2}{(t.Item1 == 1 ? string.Empty : "s")}";
|
||||
var components = new List<Tuple<int, string>>
|
||||
{
|
||||
Tuple.Create((int) timeSpan.TotalDays, "day"),
|
||||
Tuple.Create(timeSpan.Hours, "hour"),
|
||||
Tuple.Create(timeSpan.Minutes, "minute"),
|
||||
Tuple.Create(timeSpan.Seconds, "second"),
|
||||
};
|
||||
|
||||
components.RemoveAll(i => i.Item1 == 0);
|
||||
|
||||
string extra = "";
|
||||
|
||||
if (components.Count > 1)
|
||||
{
|
||||
var finalComponent = components[components.Count - 1];
|
||||
components.RemoveAt(components.Count - 1);
|
||||
extra = $" and {tupleFormatter(finalComponent)}";
|
||||
}
|
||||
|
||||
return $"{string.Join(", ", components.Select(tupleFormatter))}{extra}";
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// returns passed in timespan to human readable format
|
||||
/// as short as possible using passed in time span translations
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string FormatTimeSpan(TimeSpan timeSpan, string dayz = "days", string hourz = "hours", string minutez = "minutes", string secondz = "seconds")
|
||||
{
|
||||
if (timeSpan == TimeSpan.Zero)
|
||||
return "";
|
||||
|
||||
var components = new List<Tuple<int, string>>
|
||||
{
|
||||
Tuple.Create((int) timeSpan.TotalDays, dayz),
|
||||
Tuple.Create(timeSpan.Hours, hourz),
|
||||
Tuple.Create(timeSpan.Minutes, minutez),
|
||||
Tuple.Create(timeSpan.Seconds, secondz),
|
||||
};
|
||||
|
||||
components.RemoveAll(i => i.Item1 == 0);
|
||||
System.Text.StringBuilder sb = new System.Text.StringBuilder();
|
||||
foreach (var t in components)
|
||||
{
|
||||
sb.Append(t.Item1);
|
||||
sb.Append(" ");
|
||||
sb.Append(t.Item2);
|
||||
sb.Append(" ");
|
||||
}
|
||||
|
||||
return sb.ToString().Trim();
|
||||
}
|
||||
|
||||
|
||||
}//eoc
|
||||
|
||||
}//eons
|
||||
Reference in New Issue
Block a user