namespace UniversalNetFrame451;

public class DatetimeConverter
{
    private readonly static DateTime Default1970 = new(1970, 1, 1, 0, 0, 0);
    public static bool TryGetIntSeconds(DateTime dateTime, out int seconds)
    {
        seconds = 0;
        TimeSpan timeSpan = dateTime - Default1970;
        try
        {
            seconds = Convert.ToInt32(timeSpan.TotalSeconds);
        }
        catch
        {
            return false;
        }

        return true;
    }

    public static bool GetDataTimeSeconds(int timeSeconds, out DateTime? dateTime)
    {
        dateTime = null;

        if (timeSeconds < 0)
            return false;

        dateTime = Default1970.AddSeconds(timeSeconds);

        return true;
    }

    public static bool GetDataTimeMillionSeconds(ulong timeMillionSeconds, out DateTime? dateTime)
    {
        dateTime = null;

        if (timeMillionSeconds < 0)
            return false;

        dateTime = Default1970.AddMilliseconds(timeMillionSeconds);

        return true;
    }

    public static ulong GetlongMillionSeconds(DateTime dateTime)
    {
        TimeSpan timeSpan = dateTime - Default1970;
        return Convert.ToUInt64(timeSpan.TotalMilliseconds);
    }
}
