/* * SPDX-License-Identifier: AGPL-3.0-or-later * Copyright (C) 2025 Sergej Görzen * This file is part of OmiLAXR. */ using System; namespace OmiLAXR.Types { /// /// Unity-serializable wrapper for DateTime that can be configured in the Inspector. /// Provides conversion utilities and implicit operators for seamless DateTime interoperability. /// All dates are treated as UTC to ensure consistency across different time zones. /// [Serializable] public sealed class SerializableDateTime { /// Year component (1-9999). Defaults to 2025. public int year = 2025; /// Month component (1-12). Defaults to 1 (January). public int month = 1; /// Day component (1-31). Defaults to 1. public int day = 1; /// Hour component (0-23). Defaults to 0. public int hour; /// Minute component (0-59). Defaults to 0. public int minute; /// Second component (0-59). Defaults to 0. public int second; /// Millisecond component (0-999). Defaults to 0. public int millisecond; /// /// Converts this SerializableDateTime to a standard DateTime object. /// Always creates UTC DateTime to ensure consistency across time zones. /// /// DateTime representation of this SerializableDateTime in UTC public DateTime ToDateTime() => new DateTime(year, month, day, hour, minute, second, millisecond, DateTimeKind.Utc); /// /// Updates this SerializableDateTime with values from a standard DateTime. /// Extracts all time components and stores them in the individual fields. /// /// DateTime to copy values from public void FromDateTime(DateTime dt) { year = dt.Year; month = dt.Month; day = dt.Day; hour = dt.Hour; minute = dt.Minute; second = dt.Second; millisecond = dt.Millisecond; } /// /// Creates a new SerializableDateTime representing the current UTC time. /// Uses current system time converted to UTC. /// public static SerializableDateTime Now => new SerializableDateTime(); /// /// Implicit conversion from SerializableDateTime to DateTime. /// Enables seamless use in DateTime contexts without explicit conversion. /// /// SerializableDateTime to convert /// DateTime representation public static implicit operator DateTime(SerializableDateTime sdt) => sdt.ToDateTime(); /// /// Implicit conversion from DateTime to SerializableDateTime. /// Enables automatic wrapping of DateTime objects for serialization. /// /// DateTime to convert /// SerializableDateTime representation public static implicit operator SerializableDateTime(DateTime dt) { var sdt = new SerializableDateTime(); sdt.FromDateTime(dt); return sdt; } } }