.NET - System.DateTimeOffset (表示一个时间点)

创建时间:
2016-04-21 09:47
最近更新:
2018-06-15 13:31

Resource - MSDN

  1. 日期、时间和时区 - Entrance
  2. 在 DateTime、DateTimeOffset、TimeSpan 和 TimeZoneInfo 之间进行选择 - DateTimeOffset 结构表示日期和时间值,以及指示此值与 UTC 的差异程度的偏移量。因此,此值始终明确地标识单个时间点。 DateTimeOffset 类型包括 DateTime 类型的所有功能以及时区感知功能。 DateTimeOffset 值的使用频率比 DateTime 值的更高。因此,在应用程序开发中应考虑使用 DateTimeOffset 作为默认日期和时间类型。
  3. 在 DateTime 与 DateTimeOffset 之间进行转换
  1. DateTimeOffset 结构 - 表示一个时间点,通常以相对于协调世界时 (UTC) 的日期和时间来表示。
  2. DateTimeOffset 结构
  3. DateTimeOffset 结构

Resource

  1. DateTime 还是 DateTimeOffset? Now 还是 UtcNow?
  2. .NET 类库中的 DateTimeOffset 用途何在
  3. C# 中 DateTime 的缺陷与代替品 DateTimeOffset

Resource - SQL

  1. datetimeoffset (Transact-SQL)

源码分析

  • DateTimeOffset 的大多数属性与方法是对 DateTime 的相应属性与方法的包装。

DateTimeOffsetDateTime 的根本区别

public TimeSpan     Offset { get { ... } } 属性,DateTime 类 没有、 DateTimeOffset 类 才有。
public DateTimeKind Kind   { get { ... } } 属性,DateTime 类 才有、 DateTimeOffset 类 没有。

DateTimeOffset 结构包括一个 DateTime 值以及一个 Offset 属性,该属性用于确定当前 DateTimeOffset 实例的日期和时间与协调世界时 (UTC) 之间的差值。因为 DateTimeOffset 结构确切地定义了相对于 UTC 的日期和时间,所以它 不像 DateTime 那样 包括 Kind 成员。它采用 UTC 范围在公元 (基督纪元) 0001 年 1 月 1 日午夜 12:00:00 到公元 9999 年 12 月 31 日晚上 11:59:59 之间的值来表示日期和时间。(公元)。

-- DateTimeOffset 结构

Source Code of System.DateTimeOffset from JetBrains dotPeek 1.0 at 2018-06-12

Complete Copy:

// Type: System.DateTimeOffset
// Assembly: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll

using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Security;

namespace System
{
  [__DynamicallyInvokable]
  [Serializable]
  [StructLayout(LayoutKind.Auto)]
  public struct DateTimeOffset : IComparable, IFormattable, ISerializable, IDeserializationCallback, IComparable<DateTimeOffset>, IEquatable<DateTimeOffset>
  {
    [__DynamicallyInvokable]
    public static readonly DateTimeOffset MinValue = new DateTimeOffset(0L, TimeSpan.Zero);
    [__DynamicallyInvokable]
    public static readonly DateTimeOffset MaxValue = new DateTimeOffset(3155378975999999999L, TimeSpan.Zero);
    internal const long MaxOffset = 504000000000L;
    internal const long MinOffset = -504000000000L;
    private const long UnixEpochTicks = 621355968000000000L;
    private const long UnixEpochSeconds = 62135596800L;
    private const long UnixEpochMilliseconds = 62135596800000L;
    private DateTime m_dateTime;
    private short m_offsetMinutes;

    [__DynamicallyInvokable]
    public static DateTimeOffset Now
    {
      [__DynamicallyInvokable] get
      {
        return new DateTimeOffset(DateTime.Now);
      }
    }

    [__DynamicallyInvokable]
    public static DateTimeOffset UtcNow
    {
      [__DynamicallyInvokable] get
      {
        return new DateTimeOffset(DateTime.UtcNow);
      }
    }

    [__DynamicallyInvokable]
    public DateTime DateTime
    {
      [__DynamicallyInvokable] get
      {
        return this.ClockDateTime;
      }
    }

    [__DynamicallyInvokable]
    public DateTime UtcDateTime
    {
      [__DynamicallyInvokable] get
      {
        return DateTime.SpecifyKind(this.m_dateTime, DateTimeKind.Utc);
      }
    }

    [__DynamicallyInvokable]
    public DateTime LocalDateTime
    {
      [__DynamicallyInvokable] get
      {
        return this.UtcDateTime.ToLocalTime();
      }
    }

    private DateTime ClockDateTime
    {
      get
      {
        return new DateTime((this.m_dateTime + this.Offset).Ticks, DateTimeKind.Unspecified);
      }
    }

    [__DynamicallyInvokable]
    public DateTime Date
    {
      [__DynamicallyInvokable] get
      {
        return this.ClockDateTime.Date;
      }
    }

    [__DynamicallyInvokable]
    public int Day
    {
      [__DynamicallyInvokable] get
      {
        return this.ClockDateTime.Day;
      }
    }

    [__DynamicallyInvokable]
    public DayOfWeek DayOfWeek
    {
      [__DynamicallyInvokable] get
      {
        return this.ClockDateTime.DayOfWeek;
      }
    }

    [__DynamicallyInvokable]
    public int DayOfYear
    {
      [__DynamicallyInvokable] get
      {
        return this.ClockDateTime.DayOfYear;
      }
    }

    [__DynamicallyInvokable]
    public int Hour
    {
      [__DynamicallyInvokable] get
      {
        return this.ClockDateTime.Hour;
      }
    }

    [__DynamicallyInvokable]
    public int Millisecond
    {
      [__DynamicallyInvokable] get
      {
        return this.ClockDateTime.Millisecond;
      }
    }

    [__DynamicallyInvokable]
    public int Minute
    {
      [__DynamicallyInvokable] get
      {
        return this.ClockDateTime.Minute;
      }
    }

    [__DynamicallyInvokable]
    public int Month
    {
      [__DynamicallyInvokable] get
      {
        return this.ClockDateTime.Month;
      }
    }

    [__DynamicallyInvokable]
    public TimeSpan Offset
    {
      [__DynamicallyInvokable] get
      {
        return new TimeSpan(0, (int) this.m_offsetMinutes, 0);
      }
    }

    [__DynamicallyInvokable]
    public int Second
    {
      [__DynamicallyInvokable] get
      {
        return this.ClockDateTime.Second;
      }
    }

    [__DynamicallyInvokable]
    public long Ticks
    {
      [__DynamicallyInvokable] get
      {
        return this.ClockDateTime.Ticks;
      }
    }

    [__DynamicallyInvokable]
    public long UtcTicks
    {
      [__DynamicallyInvokable] get
      {
        return this.UtcDateTime.Ticks;
      }
    }

    [__DynamicallyInvokable]
    public TimeSpan TimeOfDay
    {
      [__DynamicallyInvokable] get
      {
        return this.ClockDateTime.TimeOfDay;
      }
    }

    [__DynamicallyInvokable]
    public int Year
    {
      [__DynamicallyInvokable] get
      {
        return this.ClockDateTime.Year;
      }
    }

    static DateTimeOffset()
    {
    }

    [__DynamicallyInvokable]
    public DateTimeOffset(long ticks, TimeSpan offset)
    {
      this.m_offsetMinutes = DateTimeOffset.ValidateOffset(offset);
      this.m_dateTime = DateTimeOffset.ValidateDate(new DateTime(ticks), offset);
    }

    [__DynamicallyInvokable]
    public DateTimeOffset(DateTime dateTime)
    {
      TimeSpan offset = dateTime.Kind == DateTimeKind.Utc ? new TimeSpan(0L) : TimeZoneInfo.GetLocalUtcOffset(dateTime, TimeZoneInfoOptions.NoThrowOnInvalidTime);
      this.m_offsetMinutes = DateTimeOffset.ValidateOffset(offset);
      this.m_dateTime = DateTimeOffset.ValidateDate(dateTime, offset);
    }

    [__DynamicallyInvokable]
    public DateTimeOffset(DateTime dateTime, TimeSpan offset)
    {
      if (dateTime.Kind == DateTimeKind.Local)
      {
        if (offset != TimeZoneInfo.GetLocalUtcOffset(dateTime, TimeZoneInfoOptions.NoThrowOnInvalidTime))
          throw new ArgumentException(Environment.GetResourceString("Argument_OffsetLocalMismatch"), "offset");
      }
      else if (dateTime.Kind == DateTimeKind.Utc && offset != TimeSpan.Zero)
        throw new ArgumentException(Environment.GetResourceString("Argument_OffsetUtcMismatch"), "offset");
      this.m_offsetMinutes = DateTimeOffset.ValidateOffset(offset);
      this.m_dateTime = DateTimeOffset.ValidateDate(dateTime, offset);
    }

    [__DynamicallyInvokable]
    public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, TimeSpan offset)
    {
      this.m_offsetMinutes = DateTimeOffset.ValidateOffset(offset);
      this.m_dateTime = DateTimeOffset.ValidateDate(new DateTime(year, month, day, hour, minute, second), offset);
    }

    [__DynamicallyInvokable]
    public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, TimeSpan offset)
    {
      this.m_offsetMinutes = DateTimeOffset.ValidateOffset(offset);
      this.m_dateTime = DateTimeOffset.ValidateDate(new DateTime(year, month, day, hour, minute, second, millisecond), offset);
    }

    public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar, TimeSpan offset)
    {
      this.m_offsetMinutes = DateTimeOffset.ValidateOffset(offset);
      this.m_dateTime = DateTimeOffset.ValidateDate(new DateTime(year, month, day, hour, minute, second, millisecond, calendar), offset);
    }

    private DateTimeOffset(SerializationInfo info, StreamingContext context)
    {
      if (info == null)
        throw new ArgumentNullException("info");
      this.m_dateTime = (DateTime) info.GetValue("DateTime", typeof (DateTime));
      this.m_offsetMinutes = (short) info.GetValue("OffsetMinutes", typeof (short));
    }

    [__DynamicallyInvokable]
    public static implicit operator DateTimeOffset(DateTime dateTime)
    {
      return new DateTimeOffset(dateTime);
    }

    [__DynamicallyInvokable]
    public static DateTimeOffset operator +(DateTimeOffset dateTimeOffset, TimeSpan timeSpan)
    {
      return new DateTimeOffset(dateTimeOffset.ClockDateTime + timeSpan, dateTimeOffset.Offset);
    }

    [__DynamicallyInvokable]
    public static DateTimeOffset operator -(DateTimeOffset dateTimeOffset, TimeSpan timeSpan)
    {
      return new DateTimeOffset(dateTimeOffset.ClockDateTime - timeSpan, dateTimeOffset.Offset);
    }

    [__DynamicallyInvokable]
    public static TimeSpan operator -(DateTimeOffset left, DateTimeOffset right)
    {
      return left.UtcDateTime - right.UtcDateTime;
    }

    [__DynamicallyInvokable]
    public static bool operator ==(DateTimeOffset left, DateTimeOffset right)
    {
      return left.UtcDateTime == right.UtcDateTime;
    }

    [__DynamicallyInvokable]
    public static bool operator !=(DateTimeOffset left, DateTimeOffset right)
    {
      return left.UtcDateTime != right.UtcDateTime;
    }

    [__DynamicallyInvokable]
    public static bool operator <(DateTimeOffset left, DateTimeOffset right)
    {
      return left.UtcDateTime < right.UtcDateTime;
    }

    [__DynamicallyInvokable]
    public static bool operator <=(DateTimeOffset left, DateTimeOffset right)
    {
      return left.UtcDateTime <= right.UtcDateTime;
    }

    [__DynamicallyInvokable]
    public static bool operator >(DateTimeOffset left, DateTimeOffset right)
    {
      return left.UtcDateTime > right.UtcDateTime;
    }

    [__DynamicallyInvokable]
    public static bool operator >=(DateTimeOffset left, DateTimeOffset right)
    {
      return left.UtcDateTime >= right.UtcDateTime;
    }

    [__DynamicallyInvokable]
    public DateTimeOffset ToOffset(TimeSpan offset)
    {
      return new DateTimeOffset((this.m_dateTime + offset).Ticks, offset);
    }

    [__DynamicallyInvokable]
    public DateTimeOffset Add(TimeSpan timeSpan)
    {
      return new DateTimeOffset(this.ClockDateTime.Add(timeSpan), this.Offset);
    }

    [__DynamicallyInvokable]
    public DateTimeOffset AddDays(double days)
    {
      return new DateTimeOffset(this.ClockDateTime.AddDays(days), this.Offset);
    }

    [__DynamicallyInvokable]
    public DateTimeOffset AddHours(double hours)
    {
      return new DateTimeOffset(this.ClockDateTime.AddHours(hours), this.Offset);
    }

    [__DynamicallyInvokable]
    public DateTimeOffset AddMilliseconds(double milliseconds)
    {
      return new DateTimeOffset(this.ClockDateTime.AddMilliseconds(milliseconds), this.Offset);
    }

    [__DynamicallyInvokable]
    public DateTimeOffset AddMinutes(double minutes)
    {
      return new DateTimeOffset(this.ClockDateTime.AddMinutes(minutes), this.Offset);
    }

    [__DynamicallyInvokable]
    public DateTimeOffset AddMonths(int months)
    {
      return new DateTimeOffset(this.ClockDateTime.AddMonths(months), this.Offset);
    }

    [__DynamicallyInvokable]
    public DateTimeOffset AddSeconds(double seconds)
    {
      return new DateTimeOffset(this.ClockDateTime.AddSeconds(seconds), this.Offset);
    }

    [__DynamicallyInvokable]
    public DateTimeOffset AddTicks(long ticks)
    {
      return new DateTimeOffset(this.ClockDateTime.AddTicks(ticks), this.Offset);
    }

    [__DynamicallyInvokable]
    public DateTimeOffset AddYears(int years)
    {
      return new DateTimeOffset(this.ClockDateTime.AddYears(years), this.Offset);
    }

    [__DynamicallyInvokable]
    public static int Compare(DateTimeOffset first, DateTimeOffset second)
    {
      return DateTime.Compare(first.UtcDateTime, second.UtcDateTime);
    }

    [__DynamicallyInvokable]
    int IComparable.CompareTo(object obj)
    {
      if (obj == null)
        return 1;
      if (!(obj is DateTimeOffset))
        throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDateTimeOffset"));
      DateTime utcDateTime1 = ((DateTimeOffset) obj).UtcDateTime;
      DateTime utcDateTime2 = this.UtcDateTime;
      if (utcDateTime2 > utcDateTime1)
        return 1;
      return utcDateTime2 < utcDateTime1 ? -1 : 0;
    }

    [__DynamicallyInvokable]
    public int CompareTo(DateTimeOffset other)
    {
      DateTime utcDateTime1 = other.UtcDateTime;
      DateTime utcDateTime2 = this.UtcDateTime;
      if (utcDateTime2 > utcDateTime1)
        return 1;
      return utcDateTime2 < utcDateTime1 ? -1 : 0;
    }

    [__DynamicallyInvokable]
    public override bool Equals(object obj)
    {
      if (obj is DateTimeOffset)
        return this.UtcDateTime.Equals(((DateTimeOffset) obj).UtcDateTime);
      else
        return false;
    }

    [__DynamicallyInvokable]
    public bool Equals(DateTimeOffset other)
    {
      return this.UtcDateTime.Equals(other.UtcDateTime);
    }

    [__DynamicallyInvokable]
    public bool EqualsExact(DateTimeOffset other)
    {
      if (this.ClockDateTime == other.ClockDateTime && this.Offset == other.Offset)
        return this.ClockDateTime.Kind == other.ClockDateTime.Kind;
      else
        return false;
    }

    [__DynamicallyInvokable]
    public static bool Equals(DateTimeOffset first, DateTimeOffset second)
    {
      return DateTime.Equals(first.UtcDateTime, second.UtcDateTime);
    }

    [__DynamicallyInvokable]
    public static DateTimeOffset FromFileTime(long fileTime)
    {
      return new DateTimeOffset(DateTime.FromFileTime(fileTime));
    }

    [__DynamicallyInvokable]
    public static DateTimeOffset FromUnixTimeSeconds(long seconds)
    {
      if (seconds < -62135596800L || seconds > 253402300799L)
        throw new ArgumentOutOfRangeException("seconds", string.Format(Environment.GetResourceString("ArgumentOutOfRange_Range"), (object) -62135596800, (object) 253402300799));
      else
        return new DateTimeOffset(seconds * 10000000L + 621355968000000000L, TimeSpan.Zero);
    }

    [__DynamicallyInvokable]
    public static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds)
    {
      if (milliseconds < -62135596800000L || milliseconds > 253402300799999L)
        throw new ArgumentOutOfRangeException("milliseconds", string.Format(Environment.GetResourceString("ArgumentOutOfRange_Range"), (object) -62135596800000, (object) 253402300799999));
      else
        return new DateTimeOffset(milliseconds * 10000L + 621355968000000000L, TimeSpan.Zero);
    }

    void IDeserializationCallback.OnDeserialization(object sender)
    {
      try
      {
        this.m_offsetMinutes = DateTimeOffset.ValidateOffset(this.Offset);
        this.m_dateTime = DateTimeOffset.ValidateDate(this.ClockDateTime, this.Offset);
      }
      catch (ArgumentException ex)
      {
        throw new SerializationException(Environment.GetResourceString("Serialization_InvalidData"), (Exception) ex);
      }
    }

    [SecurityCritical]
    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
      if (info == null)
        throw new ArgumentNullException("info");
      info.AddValue("DateTime", this.m_dateTime);
      info.AddValue("OffsetMinutes", this.m_offsetMinutes);
    }

    [__DynamicallyInvokable]
    public override int GetHashCode()
    {
      return this.UtcDateTime.GetHashCode();
    }

    [__DynamicallyInvokable]
    public static DateTimeOffset Parse(string input)
    {
      TimeSpan offset;
      return new DateTimeOffset(DateTimeParse.Parse(input, DateTimeFormatInfo.CurrentInfo, DateTimeStyles.None, out offset).Ticks, offset);
    }

    [__DynamicallyInvokable]
    public static DateTimeOffset Parse(string input, IFormatProvider formatProvider)
    {
      return DateTimeOffset.Parse(input, formatProvider, DateTimeStyles.None);
    }

    [__DynamicallyInvokable]
    public static DateTimeOffset Parse(string input, IFormatProvider formatProvider, DateTimeStyles styles)
    {
      styles = DateTimeOffset.ValidateStyles(styles, "styles");
      TimeSpan offset;
      return new DateTimeOffset(DateTimeParse.Parse(input, DateTimeFormatInfo.GetInstance(formatProvider), styles, out offset).Ticks, offset);
    }

    [__DynamicallyInvokable]
    public static DateTimeOffset ParseExact(string input, string format, IFormatProvider formatProvider)
    {
      return DateTimeOffset.ParseExact(input, format, formatProvider, DateTimeStyles.None);
    }

    [__DynamicallyInvokable]
    public static DateTimeOffset ParseExact(string input, string format, IFormatProvider formatProvider, DateTimeStyles styles)
    {
      styles = DateTimeOffset.ValidateStyles(styles, "styles");
      TimeSpan offset;
      return new DateTimeOffset(DateTimeParse.ParseExact(input, format, DateTimeFormatInfo.GetInstance(formatProvider), styles, out offset).Ticks, offset);
    }

    [__DynamicallyInvokable]
    public static DateTimeOffset ParseExact(string input, string[] formats, IFormatProvider formatProvider, DateTimeStyles styles)
    {
      styles = DateTimeOffset.ValidateStyles(styles, "styles");
      TimeSpan offset;
      return new DateTimeOffset(DateTimeParse.ParseExactMultiple(input, formats, DateTimeFormatInfo.GetInstance(formatProvider), styles, out offset).Ticks, offset);
    }

    [__DynamicallyInvokable]
    public TimeSpan Subtract(DateTimeOffset value)
    {
      return this.UtcDateTime.Subtract(value.UtcDateTime);
    }

    [__DynamicallyInvokable]
    public DateTimeOffset Subtract(TimeSpan value)
    {
      return new DateTimeOffset(this.ClockDateTime.Subtract(value), this.Offset);
    }

    [__DynamicallyInvokable]
    public long ToFileTime()
    {
      return this.UtcDateTime.ToFileTime();
    }

    [__DynamicallyInvokable]
    public long ToUnixTimeSeconds()
    {
      return this.UtcDateTime.Ticks / 10000000L - 62135596800L;
    }

    [__DynamicallyInvokable]
    public long ToUnixTimeMilliseconds()
    {
      return this.UtcDateTime.Ticks / 10000L - 62135596800000L;
    }

    [__DynamicallyInvokable]
    public DateTimeOffset ToLocalTime()
    {
      return this.ToLocalTime(false);
    }

    internal DateTimeOffset ToLocalTime(bool throwOnOverflow)
    {
      return new DateTimeOffset(this.UtcDateTime.ToLocalTime(throwOnOverflow));
    }

    [__DynamicallyInvokable]
    public override string ToString()
    {
      return DateTimeFormat.Format(this.ClockDateTime, (string) null, DateTimeFormatInfo.CurrentInfo, this.Offset);
    }

    [__DynamicallyInvokable]
    public string ToString(string format)
    {
      return DateTimeFormat.Format(this.ClockDateTime, format, DateTimeFormatInfo.CurrentInfo, this.Offset);
    }

    [__DynamicallyInvokable]
    public string ToString(IFormatProvider formatProvider)
    {
      return DateTimeFormat.Format(this.ClockDateTime, (string) null, DateTimeFormatInfo.GetInstance(formatProvider), this.Offset);
    }

    [__DynamicallyInvokable]
    public string ToString(string format, IFormatProvider formatProvider)
    {
      return DateTimeFormat.Format(this.ClockDateTime, format, DateTimeFormatInfo.GetInstance(formatProvider), this.Offset);
    }

    [__DynamicallyInvokable]
    public DateTimeOffset ToUniversalTime()
    {
      return new DateTimeOffset(this.UtcDateTime);
    }

    [__DynamicallyInvokable]
    public static bool TryParse(string input, out DateTimeOffset result)
    {
      DateTime result1;
      TimeSpan offset;
      int num = DateTimeParse.TryParse(input, DateTimeFormatInfo.CurrentInfo, DateTimeStyles.None, out result1, out offset) ? 1 : 0;
      result = new DateTimeOffset(result1.Ticks, offset);
      return num != 0;
    }

    [__DynamicallyInvokable]
    public static bool TryParse(string input, IFormatProvider formatProvider, DateTimeStyles styles, out DateTimeOffset result)
    {
      styles = DateTimeOffset.ValidateStyles(styles, "styles");
      DateTime result1;
      TimeSpan offset;
      int num = DateTimeParse.TryParse(input, DateTimeFormatInfo.GetInstance(formatProvider), styles, out result1, out offset) ? 1 : 0;
      result = new DateTimeOffset(result1.Ticks, offset);
      return num != 0;
    }

    [__DynamicallyInvokable]
    public static bool TryParseExact(string input, string format, IFormatProvider formatProvider, DateTimeStyles styles, out DateTimeOffset result)
    {
      styles = DateTimeOffset.ValidateStyles(styles, "styles");
      DateTime result1;
      TimeSpan offset;
      int num = DateTimeParse.TryParseExact(input, format, DateTimeFormatInfo.GetInstance(formatProvider), styles, out result1, out offset) ? 1 : 0;
      result = new DateTimeOffset(result1.Ticks, offset);
      return num != 0;
    }

    [__DynamicallyInvokable]
    public static bool TryParseExact(string input, string[] formats, IFormatProvider formatProvider, DateTimeStyles styles, out DateTimeOffset result)
    {
      styles = DateTimeOffset.ValidateStyles(styles, "styles");
      DateTime result1;
      TimeSpan offset;
      int num = DateTimeParse.TryParseExactMultiple(input, formats, DateTimeFormatInfo.GetInstance(formatProvider), styles, out result1, out offset) ? 1 : 0;
      result = new DateTimeOffset(result1.Ticks, offset);
      return num != 0;
    }

    private static short ValidateOffset(TimeSpan offset)
    {
      long ticks = offset.Ticks;
      if (ticks % 600000000L != 0L)
        throw new ArgumentException(Environment.GetResourceString("Argument_OffsetPrecision"), "offset");
      if (ticks < -504000000000L || ticks > 504000000000L)
        throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("Argument_OffsetOutOfRange"));
      else
        return (short) (offset.Ticks / 600000000L);
    }

    private static DateTime ValidateDate(DateTime dateTime, TimeSpan offset)
    {
      long ticks = dateTime.Ticks - offset.Ticks;
      if (ticks < 0L || ticks > 3155378975999999999L)
        throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("Argument_UTCOutOfRange"));
      else
        return new DateTime(ticks, DateTimeKind.Unspecified);
    }

    private static DateTimeStyles ValidateStyles(DateTimeStyles style, string parameterName)
    {
      if ((style & ~(DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.NoCurrentDateDefault | DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeLocal | DateTimeStyles.AssumeUniversal | DateTimeStyles.RoundtripKind)) != DateTimeStyles.None)
        throw new ArgumentException(Environment.GetResourceString("Argument_InvalidDateTimeStyles"), parameterName);
      if ((style & DateTimeStyles.AssumeLocal) != DateTimeStyles.None && (style & DateTimeStyles.AssumeUniversal) != DateTimeStyles.None)
        throw new ArgumentException(Environment.GetResourceString("Argument_ConflictingDateTimeStyles"), parameterName);
      if ((style & DateTimeStyles.NoCurrentDateDefault) != DateTimeStyles.None)
        throw new ArgumentException(Environment.GetResourceString("Argument_DateTimeOffsetInvalidDateTimeStyles"), parameterName);
      style &= ~DateTimeStyles.RoundtripKind;
      style &= ~DateTimeStyles.AssumeLocal;
      return style;
    }
  }
}