From ee804df4e9fce47ff92a39524646c4aba0a90ddc Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 27 Oct 2022 20:10:48 +0700 Subject: DateTimeUtilities improvements: - DateTimeToUnixMs converts to UTC - UnixMsToDateTime checks input range --- crypto/src/util/date/DateTimeUtilities.cs | 58 ++++++++++++++++++------------- 1 file changed, 34 insertions(+), 24 deletions(-) (limited to 'crypto/src/util') diff --git a/crypto/src/util/date/DateTimeUtilities.cs b/crypto/src/util/date/DateTimeUtilities.cs index 72e5123a2..3660e29c2 100644 --- a/crypto/src/util/date/DateTimeUtilities.cs +++ b/crypto/src/util/date/DateTimeUtilities.cs @@ -2,38 +2,48 @@ using System; namespace Org.BouncyCastle.Utilities.Date { - public class DateTimeUtilities + public static class DateTimeUtilities { - public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public static readonly DateTime UnixEpoch = DateTime.UnixEpoch; +#else + public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); +#endif - private DateTimeUtilities() - { - } + public static readonly long MaxUnixMs = + (DateTime.MaxValue.Ticks - UnixEpoch.Ticks) / TimeSpan.TicksPerMillisecond; + public static readonly long MinUnixMs = 0L; - /// - /// Return the number of milliseconds since the Unix epoch (1 Jan., 1970 UTC) for a given DateTime value. - /// - /// A UTC DateTime value not before epoch. - /// Number of whole milliseconds after epoch. - /// 'dateTime' is before epoch. - public static long DateTimeToUnixMs( - DateTime dateTime) + /// + /// Return the number of milliseconds since the Unix epoch (1 Jan., 1970 UTC) for a given DateTime value. + /// + /// The DateTime value will be converted to UTC (using before + /// conversion. + /// A DateTime value not before the epoch. + /// Number of whole milliseconds after epoch. + /// 'dateTime' is before the epoch. + public static long DateTimeToUnixMs(DateTime dateTime) { - if (dateTime.CompareTo(UnixEpoch) < 0) - throw new ArgumentException("DateTime value may not be before the epoch", "dateTime"); + DateTime utc = dateTime.ToUniversalTime(); + if (utc.CompareTo(UnixEpoch) < 0) + throw new ArgumentOutOfRangeException(nameof(dateTime), "DateTime value may not be before the epoch"); - return (dateTime.Ticks - UnixEpoch.Ticks) / TimeSpan.TicksPerMillisecond; + return (utc.Ticks - UnixEpoch.Ticks) / TimeSpan.TicksPerMillisecond; } - /// - /// Create a DateTime value from the number of milliseconds since the Unix epoch (1 Jan., 1970 UTC). - /// - /// Number of milliseconds since the epoch. - /// A UTC DateTime value - public static DateTime UnixMsToDateTime( - long unixMs) + /// + /// Create a UTC DateTime value from the number of milliseconds since the Unix epoch (1 Jan., 1970 UTC). + /// + /// Number of milliseconds since the epoch. + /// A UTC DateTime value + /// 'unixMs' is before 'MinUnixMs' or after 'MaxUnixMs'. + /// + public static DateTime UnixMsToDateTime(long unixMs) { - return new DateTime(unixMs * TimeSpan.TicksPerMillisecond + UnixEpoch.Ticks, DateTimeKind.Utc); + if (unixMs < MinUnixMs || unixMs > MaxUnixMs) + throw new ArgumentOutOfRangeException(nameof(unixMs)); + + return new DateTime(unixMs * TimeSpan.TicksPerMillisecond + UnixEpoch.Ticks, DateTimeKind.Utc); } /// -- cgit 1.5.1