// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using System;
+using System.Threading;
+using System.Globalization;
+using System.Runtime;
+using System.Runtime.InteropServices;
+using System.Runtime.CompilerServices;
+using System.Runtime.Serialization;
+using System.Runtime.Versioning;
+using System.Security;
+using System.Diagnostics.Contracts;
+using CultureInfo = System.Globalization.CultureInfo;
+using Calendar = System.Globalization.Calendar;
+
namespace System
{
- using System;
- using System.Threading;
- using System.Globalization;
- using System.Runtime;
- using System.Runtime.InteropServices;
- using System.Runtime.CompilerServices;
- using System.Runtime.Serialization;
- using System.Runtime.Versioning;
- using System.Security;
- using System.Diagnostics.Contracts;
- using CultureInfo = System.Globalization.CultureInfo;
- using Calendar = System.Globalization.Calendar;
// This value type represents a date and time. Every DateTime
// object has a private field (Ticks) of type Int64 that stores the
//
[StructLayout(LayoutKind.Auto)]
[Serializable]
- public struct DateTime : IComparable, IFormattable, IConvertible, IComparable<DateTime>, IEquatable<DateTime>, ISerializable
+ public partial struct DateTime : IComparable, IFormattable, IConvertible, IComparable<DateTime>, IEquatable<DateTime>, ISerializable
{
// Number of 100ns ticks per time unit
private const long TicksPerMillisecond = 10000;
internal const long MaxTicks = DaysTo10000 * TicksPerDay - 1;
private const long MaxMillis = (long)DaysTo10000 * MillisPerDay;
+ private const long TicksTo1970 = DaysTo1970 * TicksPerDay;
private const long FileTimeOffset = DaysTo1601 * TicksPerDay;
private const long DoubleDateOffset = DaysTo1899 * TicksPerDay;
// The minimum OA date is 0100/01/01 (Note it's year 100).
private const Int32 KindShift = 62;
private const String TicksField = "ticks";
- private const String DateDataField = "dateData";
+ private const String DateDataField = "_dateData";
// The data is stored as an unsigned 64-bit integeter
// Bits 01-62: The value of 100-nanosecond ticks where 0 represents 1/1/0001 12:00am, up until the value
// savings time hour and it is in daylight savings time. This allows distinction of these
// otherwise ambiguous local times and prevents data loss when round tripping from Local to
// UTC time.
- private UInt64 dateData;
+ private UInt64 _dateData;
// Constructs a DateTime from a tick count. The ticks
// argument specifies the date as the number of 100-nanosecond intervals
if (ticks < MinTicks || ticks > MaxTicks)
throw new ArgumentOutOfRangeException(nameof(ticks), SR.ArgumentOutOfRange_DateTimeBadTicks);
Contract.EndContractBlock();
- dateData = (UInt64)ticks;
+ _dateData = (UInt64)ticks;
}
private DateTime(UInt64 dateData)
{
- this.dateData = dateData;
+ this._dateData = dateData;
}
public DateTime(long ticks, DateTimeKind kind)
throw new ArgumentException(SR.Argument_InvalidDateTimeKind, nameof(kind));
}
Contract.EndContractBlock();
- dateData = ((UInt64)ticks | ((UInt64)kind << KindShift));
+ _dateData = ((UInt64)ticks | ((UInt64)kind << KindShift));
}
internal DateTime(long ticks, DateTimeKind kind, Boolean isAmbiguousDst)
{
throw new ArgumentOutOfRangeException(nameof(ticks), SR.ArgumentOutOfRange_DateTimeBadTicks);
}
- Contract.Requires(kind == DateTimeKind.Local, "Internal Constructor is for local times only");
+ Debug.Assert(kind == DateTimeKind.Local, "Internal Constructor is for local times only");
Contract.EndContractBlock();
- dateData = ((UInt64)ticks | (isAmbiguousDst ? KindLocalAmbiguousDst : KindLocal));
+ _dateData = ((UInt64)ticks | (isAmbiguousDst ? KindLocalAmbiguousDst : KindLocal));
}
// Constructs a DateTime from a given year, month, and day. The
//
public DateTime(int year, int month, int day)
{
- dateData = (UInt64)DateToTicks(year, month, day);
+ _dateData = (UInt64)DateToTicks(year, month, day);
}
// Constructs a DateTime from a given year, month, and day for
//
public DateTime(int year, int month, int day, int hour, int minute, int second)
{
- dateData = (UInt64)(DateToTicks(year, month, day) + TimeToTicks(hour, minute, second));
+ _dateData = (UInt64)(DateToTicks(year, month, day) + TimeToTicks(hour, minute, second));
}
public DateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind)
}
Contract.EndContractBlock();
Int64 ticks = DateToTicks(year, month, day) + TimeToTicks(hour, minute, second);
- dateData = ((UInt64)ticks | ((UInt64)kind << KindShift));
+ _dateData = ((UInt64)ticks | ((UInt64)kind << KindShift));
}
// Constructs a DateTime from a given year, month, day, hour,
if (calendar == null)
throw new ArgumentNullException(nameof(calendar));
Contract.EndContractBlock();
- dateData = (UInt64)calendar.ToDateTime(year, month, day, hour, minute, second, 0).Ticks;
+ _dateData = (UInt64)calendar.ToDateTime(year, month, day, hour, minute, second, 0).Ticks;
}
// Constructs a DateTime from a given year, month, day, hour,
ticks += millisecond * TicksPerMillisecond;
if (ticks < MinTicks || ticks > MaxTicks)
throw new ArgumentException(SR.Arg_DateTimeRange);
- dateData = (UInt64)ticks;
+ _dateData = (UInt64)ticks;
}
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind)
ticks += millisecond * TicksPerMillisecond;
if (ticks < MinTicks || ticks > MaxTicks)
throw new ArgumentException(SR.Arg_DateTimeRange);
- dateData = ((UInt64)ticks | ((UInt64)kind << KindShift));
+ _dateData = ((UInt64)ticks | ((UInt64)kind << KindShift));
}
// Constructs a DateTime from a given year, month, day, hour,
ticks += millisecond * TicksPerMillisecond;
if (ticks < MinTicks || ticks > MaxTicks)
throw new ArgumentException(SR.Arg_DateTimeRange);
- dateData = (UInt64)ticks;
+ _dateData = (UInt64)ticks;
}
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar, DateTimeKind kind)
ticks += millisecond * TicksPerMillisecond;
if (ticks < MinTicks || ticks > MaxTicks)
throw new ArgumentException(SR.Arg_DateTimeRange);
- dateData = ((UInt64)ticks | ((UInt64)kind << KindShift));
+ _dateData = ((UInt64)ticks | ((UInt64)kind << KindShift));
}
private DateTime(SerializationInfo info, StreamingContext context)
}
if (foundDateData)
{
- dateData = serializedDateData;
+ _dateData = serializedDateData;
}
else if (foundTicks)
{
- dateData = (UInt64)serializedTicks;
+ _dateData = (UInt64)serializedTicks;
}
else
{
{
get
{
- return (Int64)(dateData & TicksMask);
+ return (Int64)(_dateData & TicksMask);
}
}
{
get
{
- return (dateData & FlagsMask);
+ return (_dateData & FlagsMask);
}
}
// Serialize both the old and the new format
info.AddValue(TicksField, InternalTicks);
- info.AddValue(DateDataField, dateData);
+ info.AddValue(DateDataField, _dateData);
}
public Boolean IsDaylightSavingTime()
}
else
{
- return (Int64)dateData;
+ return (Int64)_dateData;
}
}
}
}
- public static DateTime UtcNow
- {
- get
- {
- Contract.Ensures(Contract.Result<DateTime>().Kind == DateTimeKind.Utc);
- // following code is tuned for speed. Don't change it without running benchmark.
- long ticks = 0;
- ticks = GetSystemTimeAsFileTime();
-
- return new DateTime(((UInt64)(ticks + FileTimeOffset)) | KindUtc);
- }
- }
-
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- internal static extern long GetSystemTimeAsFileTime();
-
-
-
// Returns the second part of this DateTime. The returned value is
// an integer between 0 and 59.
//