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;
}
}
}