PKHeX/PKHeX.Core/Saves/Substructures/Time/Epoch1900DateValue.cs
Jonathan Herbert 92c964d6fa
Refactor And Deduplicate Some Switch Games Common Logic (#4187)
- Improve Epoch 1900 classes using similar logic from PlayTime7b.
- Move Time Classes into non Gen specific folder since it appears the logic is shared across a few of them.
- Use Epoch1900DateTimeValue for LastSaved in PlayTime7b since the logic is the same.
- Remove TeamIndexes9 since it is a duplicate of TeamIndexes9. Use the similar pattern like Box8 where it is reused in multiple locations.
- Add BlueberryClubRoom9 to ISaveBlock9Main since it wasn't added when the class was introduced.
- Simplify RaidSevenStar9 creation since GetBlockSafe does basically what the if-else block does.
- Change SAV8SWSH Base Raid to RaidGalar to match the same way the SAV9SV does for its Base Raid.
2024-02-23 13:19:17 -06:00

47 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.ComponentModel;
using static System.Buffers.Binary.BinaryPrimitives;
namespace PKHeX.Core;
/// <summary>
/// Stores the <see cref="Timestamp"/> to indicate the seconds since 1900 (rounded to days) that an event occurred.
/// </summary>
[TypeConverter(typeof(ExpandableObjectConverter))]
public sealed class Epoch1900DateValue(Memory<byte> Data)
{
// Data should be 4 bytes where we only care about the first 3 bytes i.e. 24 bits
// First 12 bits are year from 1900, next 6 bits are 0 indexed month, next 6 are days
private Span<byte> Span => Data.Span;
public Epoch1900DateValue(SCBlock block) : this(block.Data) { }
private static DateTime Epoch => new(1900, 1, 1);
private uint RawDate { get => ReadUInt32LittleEndian(Span); set => WriteUInt32LittleEndian(Span, value); }
public int Year { get => (int)(RawDate & 0xFFF) + Epoch.Year; set => RawDate = (RawDate & 0xFFFFF000) | (uint)(value - Epoch.Year); }
public int Month { get => (int)((RawDate >> 12) & 0x3F) + 1; set => RawDate = (RawDate & 0xFFFC0FFF) | (((uint)(value - 1) & 0x3F) << 12); }
public int Day { get => (int)((RawDate >> 18) & 0x3F); set => RawDate = (RawDate & 0xFF03FFFF) | (((uint)value & 0x3F) << 18); }
public DateTime Timestamp
{
get => new(Year, Month, Day);
set
{
Year = value.Year;
Month = value.Month;
Day = value.Day;
}
}
public string DisplayValue => $"{Timestamp.Year:0000}-{Timestamp.Month:00}-{Timestamp.Day:00} {Timestamp.Hour:00}ː{Timestamp.Minute:00}ː{Timestamp.Second:00}"; // not :
/// <summary>
/// time_t (seconds since 1900 Epoch rounded to days)
/// </summary>
public ulong TotalSeconds
{
get => (ulong)(Timestamp - Epoch).TotalSeconds;
set => Timestamp = Epoch.AddSeconds(value);
}
}