using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Extensions;
namespace SPTarkov.Server.Core.Utils;
[Injectable(InjectionType.Singleton)]
public class TimeUtil
{
public const int OneHourAsSeconds = 3600;
///
/// Gets the current date as a formatted UTC string.
///
/// The current date as 'YYYY-MM-DD'.
public string GetDate()
{
return DateTimeOffset.UtcNow.FormatToBsgDate();
}
public DateTime GetDateTimeNow()
{
return DateTime.UtcNow;
}
///
/// Gets the current time as a formatted UTC string.
///
/// The current time as 'HH-MM-SS'.
public string GetTime()
{
return DateTimeOffset.UtcNow.FormatToBsgTime();
}
///
/// Gets the current timestamp in seconds in UTC.
///
/// The current timestamp in seconds since the Unix epoch in UTC.
public long GetTimeStamp()
{
return DateTimeOffset.UtcNow.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(long? timestamp)
{
var now = timestamp.HasValue ? DateTimeOffset.FromUnixTimeMilliseconds(timestamp.Value).DateTime : GetDateTimeNow();
var startOfDay = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
return ((DateTimeOffset)startOfDay).ToUnixTimeMilliseconds();
}
///
/// Get timestamp of today + passed in day count
///
/// Days from now
///
public long GetTimeStampFromNowDays(int daysFromNow)
{
return DateTimeOffset.UtcNow.AddDays(daysFromNow).ToUnixTimeSeconds();
}
///
/// Get timestamp of today + passed in hour count
///
///
///
public long GetTimeStampFromNowHours(int hoursFromNow)
{
return DateTimeOffset.UtcNow.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.
/// GetTimeMailFormat
public string GetBsgTimeMailFormat()
{
return DateTimeOffset.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 GetBsgDateMailFormat()
{
return DateTimeOffset.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 timeUntilNextHour = TimeSpan
.FromMinutes(60 - now.Minute)
.Subtract(TimeSpan.FromSeconds(now.Second))
.Subtract(TimeSpan.FromMilliseconds(now.Millisecond));
var time = ((DateTimeOffset)now.Add(timeUntilNextHour)).ToUnixTimeSeconds();
return time;
}
///
/// 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 hours = now.Hour;
var minutes = now.Minute;
// If minutes greater than 0, subtract 1 hour
if (hours > 0 && minutes > 0)
{
hours--;
}
// Create a new DateTime with the last full hour, 0 minutes, and 0 seconds
var lastFullHour = new DateTime(now.Year, now.Month, now.Day, hours, 0, 0);
return ((DateTimeOffset)lastFullHour).ToUnixTimeSeconds();
}
///
/// Takes a timestamp and converts to its date with Epoch
///
///
///
public DateTime GetDateTimeFromTimeStamp(long timeStamp)
{
return DateTimeOffset.FromUnixTimeSeconds(timeStamp).DateTime;
}
///
/// Takes a unix timestamp and converts to its UTC date
///
///
///
public DateTime GetUtcDateTimeFromTimeStamp(long timeStamp)
{
return DateTimeOffset.FromUnixTimeSeconds(timeStamp).UtcDateTime;
}
///
/// Get passed in minutes as seconds
///
/// Minutes
/// Seconds
public double GetMinutesAsSeconds(int minutes)
{
return minutes * 60;
}
}