2023-01-22 04:02:33 +00:00
|
|
|
using System;
|
2016-08-10 14:02:31 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
public static class DateUtil
|
2016-08-10 14:02:31 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// Determines whether the given date components are valid.
|
2022-06-18 18:04:24 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="year">The year of the date of which to check the validity.</param>
|
|
|
|
/// <param name="month">The month of the date of which to check the validity.</param>
|
|
|
|
/// <param name="day">The day of the date of which to check the validity.</param>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// <returns>A boolean indicating if the date is valid.</returns>
|
2022-06-18 18:04:24 +00:00
|
|
|
public static bool IsDateValid(int year, int month, int day)
|
2016-08-10 14:02:31 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (year is <= 0 or > 9999)
|
|
|
|
return false;
|
|
|
|
if (month is < 1 or > 12)
|
|
|
|
return false;
|
|
|
|
if (day < 1 || day > DateTime.DaysInMonth(year, month))
|
|
|
|
return false;
|
2016-08-10 14:02:31 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-09-16 05:12:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// Determines whether the given date components are valid.
|
2022-06-18 18:04:24 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="year">The year of the date of which to check the validity.</param>
|
|
|
|
/// <param name="month">The month of the date of which to check the validity.</param>
|
|
|
|
/// <param name="day">The day of the date of which to check the validity.</param>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// <returns>A boolean indicating if the date is valid.</returns>
|
2022-06-18 18:04:24 +00:00
|
|
|
public static bool IsDateValid(uint year, uint month, uint day)
|
|
|
|
{
|
|
|
|
return year < int.MaxValue && month < int.MaxValue && day < int.MaxValue && IsDateValid((int)year, (int)month, (int)day);
|
|
|
|
}
|
2019-09-16 05:12:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static readonly DateTime Epoch2000 = new(2000, 1, 1);
|
|
|
|
private const int SecondsPerDay = 60*60*24; // 86400
|
2019-09-16 05:12:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static int GetSecondsFrom2000(DateTime date, DateTime time)
|
|
|
|
{
|
|
|
|
int seconds = (int)(date - Epoch2000).TotalSeconds;
|
|
|
|
seconds -= seconds % SecondsPerDay;
|
|
|
|
seconds += (int)(time - Epoch2000).TotalSeconds;
|
|
|
|
return seconds;
|
|
|
|
}
|
2019-09-16 05:12:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void GetDateTime2000(uint seconds, out DateTime date, out DateTime time)
|
|
|
|
{
|
|
|
|
date = Epoch2000.AddSeconds(seconds);
|
|
|
|
time = Epoch2000.AddSeconds(seconds % SecondsPerDay);
|
|
|
|
}
|
2021-05-18 23:58:53 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static string ConvertDateValueToString(int value, int secondsBias = -1)
|
|
|
|
{
|
|
|
|
var sb = new System.Text.StringBuilder();
|
|
|
|
if (value >= SecondsPerDay)
|
|
|
|
sb.Append(value / SecondsPerDay).Append("d ");
|
2024-02-10 06:25:22 +00:00
|
|
|
value %= SecondsPerDay;
|
2023-01-22 04:02:33 +00:00
|
|
|
sb.Append(new TimeOnly(ticks: value * TimeSpan.TicksPerSecond).ToString("HH:mm:ss"));
|
2022-06-18 18:04:24 +00:00
|
|
|
if (secondsBias >= 0)
|
|
|
|
sb.Append(Environment.NewLine).Append("Date: ").Append(Epoch2000.AddSeconds(value + secondsBias));
|
|
|
|
return sb.ToString();
|
|
|
|
}
|
2021-05-18 23:58:53 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a random day within the random range of <see cref="start"/> and <see cref="end"/> days, inclusive.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="start">First valid date</param>
|
|
|
|
/// <param name="end">Last valid date</param>
|
|
|
|
/// <param name="r">Random to use</param>
|
|
|
|
/// <returns>Date within the specified range, inclusive.</returns>
|
2023-01-22 04:02:33 +00:00
|
|
|
public static DateOnly GetRandomDateWithin(DateOnly start, DateOnly end, Random r)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var delta = end.DayNumber - start.DayNumber;
|
|
|
|
var bias = r.Next(delta + 1);
|
2022-06-18 18:04:24 +00:00
|
|
|
return start.AddDays(bias);
|
2016-08-10 14:02:31 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
/// <inheritdoc cref="GetRandomDateWithin(DateOnly,DateOnly,Random)"/>
|
|
|
|
public static DateOnly GetRandomDateWithin(DateOnly start, DateOnly end) => GetRandomDateWithin(start, end, Util.Rand);
|
2016-08-10 14:02:31 +00:00
|
|
|
}
|