PKHeX/PKHeX.Core/Util/DateUtil.cs
Kurt 88830e0d00
Update from .NET Framework 4.6 to .NET 7 (#3729)
Updates from net46->net7, dropping support for mono in favor of using the latest runtime (along with the performance/API improvements). Releases will be posted as 64bit only for now.

Refactors a good amount of internal API methods to be more performant and more customizable for future updates & fixes.

Adds functionality for Batch Editor commands to `>`, `<` and <=/>=

TID/SID properties renamed to TID16/SID16 for clarity; other properties exposed for Gen7 / display variants.

Main window has a new layout to account for DPI scaling (8 point grid)

Fixed: Tatsugiri and Paldean Tauros now output Showdown form names as Showdown expects
Changed: Gen9 species now interact based on the confirmed National Dex IDs (closes #3724)
Fixed: Pokedex set all no longer clears species with unavailable non-base forms (closes #3720)
Changed: Hyper Training suggestions now apply for level 50 in SV. (closes #3714)
Fixed: B2/W2 hatched egg met locations exclusive to specific versions are now explicitly checked (closes #3691)
Added: Properties for ribbon/mark count (closes #3659)
Fixed: Traded SV eggs are now checked correctly (closes #3692)
2023-01-21 20:02:33 -08:00

82 lines
3.3 KiB
C#

using System;
namespace PKHeX.Core;
public static class DateUtil
{
/// <summary>
/// Determines whether or not the given date components are valid.
/// </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>
/// <returns>A boolean indicating whether or not the date is valid.</returns>
public static bool IsDateValid(int year, int month, int day)
{
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;
return true;
}
/// <summary>
/// Determines whether or not the given date components are valid.
/// </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>
/// <returns>A boolean indicating whether or not the date is valid.</returns>
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);
}
private static readonly DateTime Epoch2000 = new(2000, 1, 1);
private const int SecondsPerDay = 60*60*24; // 86400
public static int GetSecondsFrom2000(DateTime date, DateTime time)
{
int seconds = (int)(date - Epoch2000).TotalSeconds;
seconds -= seconds % SecondsPerDay;
seconds += (int)(time - Epoch2000).TotalSeconds;
return seconds;
}
public static void GetDateTime2000(uint seconds, out DateTime date, out DateTime time)
{
date = Epoch2000.AddSeconds(seconds);
time = Epoch2000.AddSeconds(seconds % SecondsPerDay);
}
public static string ConvertDateValueToString(int value, int secondsBias = -1)
{
var sb = new System.Text.StringBuilder();
if (value >= SecondsPerDay)
sb.Append(value / SecondsPerDay).Append("d ");
sb.Append(new TimeOnly(ticks: value * TimeSpan.TicksPerSecond).ToString("HH:mm:ss"));
if (secondsBias >= 0)
sb.Append(Environment.NewLine).Append("Date: ").Append(Epoch2000.AddSeconds(value + secondsBias));
return sb.ToString();
}
/// <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>
public static DateOnly GetRandomDateWithin(DateOnly start, DateOnly end, Random r)
{
var delta = end.DayNumber - start.DayNumber;
var bias = r.Next(delta + 1);
return start.AddDays(bias);
}
/// <inheritdoc cref="GetRandomDateWithin(DateOnly,DateOnly,Random)"/>
public static DateOnly GetRandomDateWithin(DateOnly start, DateOnly end) => GetRandomDateWithin(start, end, Util.Rand);
}