From eea0872aa7626924db5ed53e27aed2bb46d55a3f Mon Sep 17 00:00:00 2001 From: Cj <161484149+CJ-SPT@users.noreply.github.com> Date: Mon, 6 Jan 2025 01:30:38 -0500 Subject: [PATCH] Add TimeUtil.cs --- Core/Utils/HashUtil.cs | 4 +- Core/Utils/TimeUtil.cs | 179 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 Core/Utils/TimeUtil.cs diff --git a/Core/Utils/HashUtil.cs b/Core/Utils/HashUtil.cs index b00a3e3f..c0667142 100644 --- a/Core/Utils/HashUtil.cs +++ b/Core/Utils/HashUtil.cs @@ -1,10 +1,12 @@ using System.Text; using System.Text.RegularExpressions; using System.Security.Cryptography; +using Types.Annotations; namespace Types.Utils; -public static partial class HashUtil +[Injectable(InjectionType.Singleton)] +public partial class HashUtil { /// /// Create a 24 character id using the sha256 algorithm + current timestamp diff --git a/Core/Utils/TimeUtil.cs b/Core/Utils/TimeUtil.cs new file mode 100644 index 00000000..b8621dce --- /dev/null +++ b/Core/Utils/TimeUtil.cs @@ -0,0 +1,179 @@ +using Types.Annotations; + +namespace Types.Utils; + +[Injectable(InjectionType.Singleton)] +public class TimeUtil +{ + public const int OneHourAsSeconds = 3600; + + /// + /// Formats the time part of a date as a UTC string. + /// + /// The date to format in UTC. + /// The formatted time as 'HH-MM-SS'. + public string FormatTime(DateTime dateTime) + { + var hour = Pad(dateTime.ToUniversalTime().Hour); + var minute = Pad(dateTime.ToUniversalTime().Minute); + var second = Pad(dateTime.ToUniversalTime().Second); + + return $"{hour}-{minute}-{second}"; + } + + /// + /// Formats the date part of a date as a UTC string. + /// + /// The date to format in UTC. + /// The formatted date as 'YYYY-MM-DD'. + public string FormatDate(DateTime dateTime) + { + var day = Pad(dateTime.ToUniversalTime().Day); + var month = Pad(dateTime.ToUniversalTime().Month); + var year = Pad(dateTime.ToUniversalTime().Year); + + return $"{year}-{month}-{day}"; + } + + /// + /// Gets the current date as a formatted UTC string. + /// + /// The current date as 'YYYY-MM-DD'. + public string GetDate() + { + return FormatDate(DateTime.Now); + } + + /// + /// Gets the current time as a formatted UTC string. + /// + /// The current time as 'HH-MM-SS'. + public string GetTime() + { + return FormatTime(DateTime.Now); + } + + /// + /// Gets the current timestamp in seconds in UTC. + /// + /// The current timestamp in seconds since the Unix epoch in UTC. + public long GetTimeStamp() + { + return DateTimeOffset.Now.ToUnixTimeSeconds(); + } + + /// + /// Gets the start of day timestamp for the given date + /// + /// datetime to get the time stamp for, if null it uses current date. + /// Unix epoch for the start of day of the calculated date + public long GetStartOfDayTimeStamp(DateTime? dateTime) + { + var now = dateTime ?? DateTime.Now; + + return new DateTimeOffset(new DateTime(now.Year, now.Month, now.Day, 0, 0, 0)) + .ToUnixTimeSeconds(); + } + + /// + /// Get timestamp of today + passed in day count + /// + /// Days from now + /// + public long GetTimeStampFromNowDays(int daysFromNow) + { + return DateTimeOffset.Now.AddDays(daysFromNow).ToUnixTimeSeconds(); + } + + /// + /// Get timestamp of today + passed in hour count + /// + /// + /// + public long GetTimeStampFromNowHours(int hoursFromNow) + { + return DateTimeOffset.Now.AddHours(hoursFromNow).ToUnixTimeSeconds(); + } + + /// + /// Gets the current time in UTC in a format suitable for mail in EFT. + /// + /// The current time as 'HH:MM' in UTC. + public string GetTimeMailFormat() + { + return DateTime.UtcNow.ToString("HH:mm"); + } + + /// + /// Gets the current date in UTC in a format suitable for emails in EFT. + /// + /// The current date as 'DD.MM.YYYY' in UTC. + public string GetDateMailFormat() + { + return DateTime.UtcNow.ToString("dd.MM.yyyy"); + } + + /// + /// Converts a number of hours into seconds. + /// + /// The number of hours to convert. + /// The equivalent number of seconds. + public int GetHoursAsSeconds(int hours) + { + return OneHourAsSeconds * hours; + } + + /// + /// Gets the time stamp of the start of the next hour in UTC + /// + /// Time stamp of the next hour in unix time seconds + public long GetTimeStampOfNextHour() + { + var now = DateTime.UtcNow; + + var nextHour = new DateTime( + now.Year, + now.Month, + now.Day, + now.Hour, + 0, + 0, + DateTimeKind.Utc + ).AddHours(1); + + return new DateTimeOffset(nextHour).ToUnixTimeSeconds(); + } + + /// + /// Returns the current days timestamp at 00:00 + /// e.g. current time: 13th March 14:22 will return 13th March 00:00 + /// + /// Timestamp + public long GetTodayMidNightTimeStamp() + { + var now = DateTime.UtcNow; + + var midNight = new DateTime( + now.Year, + now.Month, + now.Day, + 0, + 0, + 0, + DateTimeKind.Utc + ); + + return new DateTimeOffset(midNight).ToUnixTimeSeconds(); + } + + /// + /// Pads a number with a leading zero if it is less than 10. + /// + /// The number to pad. + /// The padded number as a string. + /// + private static string Pad(int number) + { + return number.ToString().PadLeft(2, '0'); + } +} \ No newline at end of file