namespace SPTarkov.Server.Core.Extensions; public static class DateTimeExtensions { /// /// 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 static string FormatToBsgTime(this DateTimeOffset dateTimeOffset) { var universalTime = dateTimeOffset.ToUniversalTime(); var hour = Pad(universalTime.Hour); var minute = Pad(universalTime.Minute); var second = Pad(universalTime.Second); return $"{hour}-{minute}-{second}"; } /// /// 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 static string FormatToBsgTime(this DateTime dateTime) { var universalTime = dateTime.ToUniversalTime(); var hour = Pad(universalTime.Hour); var minute = Pad(universalTime.Minute); var second = Pad(universalTime.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 static string FormatToBsgDate(this DateTimeOffset dateTimeOffset) { var universalTime = dateTimeOffset.ToUniversalTime(); var day = Pad(universalTime.Day); var month = Pad(universalTime.Month); var year = Pad(universalTime.Year); return $"{year}-{month}-{day}"; } /// /// 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 static string FormatToBsgDate(this DateTime dateTime) { var universalTime = dateTime.ToUniversalTime(); var day = Pad(universalTime.Day); var month = Pad(universalTime.Month); var year = Pad(universalTime.Year); return $"{year}-{month}-{day}"; } /// /// 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'); } /// /// Get current time formatted to fit BSGs requirement /// /// Date to format into bsg style /// Time formatted in BSG format public static string GetBsgFormattedWeatherTime(this DateTime date) { return date.FormatToBsgTime().Replace("-", ":").Replace("-", ":"); } /// /// Does the provided date fit between the two defined dates? /// Excludes year /// Inclusive of end date up to 23 hours 59 minutes /// /// Date to check is between 2 dates /// Lower bound for month /// Lower bound for day /// Upper bound for month /// Upper bound for day /// True when inside date range public static bool DateIsBetweenTwoDates(this DateTime dateToCheck, int startMonth, int startDay, int endMonth, int endDay) { var eventStartDate = new DateTime(dateToCheck.Year, startMonth, startDay); var eventEndDate = new DateTime(dateToCheck.Year, endMonth, endDay, 23, 59, 0); return dateToCheck >= eventStartDate && dateToCheck <= eventEndDate; } /// /// Get the closest monday to passed in datetime /// /// Date to get closest monday of /// Starting day of week - Default = Monday /// Monday as DateTime public static DateTime GetClosestDate(this DateTime dateTime, DayOfWeek startDay = DayOfWeek.Monday) { // Calculate difference from current day to Monday var diff = (7 + (dateTime.DayOfWeek - startDay)) % 7; // Subtract difference to get date of most recent Monday return dateTime.AddDays(-1 * diff).Date; } /// /// Get the most recent requested day from date /// /// Date to start from /// Desired day to find /// Should today be included in check, default = true /// Datetime of desired day public static DateTime GetMostRecentPreviousDay(this DateTime dateTime, DayOfWeek desiredDay, bool inclusiveOfToday = true) { // Get difference in day count from today to what day we want var dayDifferenceCount = (dateTime.DayOfWeek - desiredDay + 7) % 7; // Today is wanted day + we are not counting today, we know desired day is exactly 7 days ago if (!inclusiveOfToday && dayDifferenceCount == 0) { dayDifferenceCount = 7; } // Remove count of day difference to get desired day return dateTime.AddDays(-dayDifferenceCount); } }