Exemplo n.º 1
0
    private static void CompareExactly()
    {
        // <Snippet12>
        DateTimeOffset instanceTime = new DateTimeOffset(2007, 10, 31, 0, 0, 0,
                                                         DateTimeOffset.Now.Offset);

        DateTimeOffset otherTime = instanceTime;

        Console.WriteLine("{0} = {1}: {2}",
                          instanceTime, otherTime,
                          instanceTime.EqualsExact(otherTime));

        otherTime = new DateTimeOffset(instanceTime.DateTime,
                                       TimeSpan.FromHours(instanceTime.Offset.Hours + 1));
        Console.WriteLine("{0} = {1}: {2}",
                          instanceTime, otherTime,
                          instanceTime.EqualsExact(otherTime));

        otherTime = new DateTimeOffset(instanceTime.DateTime + TimeSpan.FromHours(1),
                                       TimeSpan.FromHours(instanceTime.Offset.Hours + 1));
        Console.WriteLine("{0} = {1}: {2}",
                          instanceTime, otherTime,
                          instanceTime.EqualsExact(otherTime));
        // The example produces the following output:
        //       10/31/2007 12:00:00 AM -07:00 = 10/31/2007 12:00:00 AM -07:00: True
        //       10/31/2007 12:00:00 AM -07:00 = 10/31/2007 12:00:00 AM -06:00: False
        //       10/31/2007 12:00:00 AM -07:00 = 10/31/2007 1:00:00 AM -06:00: False
        // </Snippet12>
    }
Exemplo n.º 2
0
    void DateComparisions()
    {
        DateTime a = new DateTime(year: 2017, month: 3, day: 24, hour: 0, minute: 0, second: 0, kind: DateTimeKind.Utc);
        DateTime b = new DateTime(year: 2017, month: 3, day: 24, hour: 0, minute: 0, second: 0, kind: DateTimeKind.Local);
        DateTime c = b.ToUniversalTime();

        Console.WriteLine(a);
        Console.WriteLine(b);
        Console.WriteLine(c);
        // Compares the ticks, not the kinds
        Console.WriteLine(a == b); // Different times due to the timezone, but true...
        Console.WriteLine(b == c); // Same point in time, but false...


        // This works
        Console.WriteLine(a.ToUniversalTime() == b.ToUniversalTime()); // false
        Console.WriteLine(b.ToUniversalTime() == c.ToUniversalTime()); // true

        DateTimeOffset aOffset = new DateTimeOffset(year: 2017, month: 3, day: 24, hour: 0, minute: 0, second: 0, offset: TimeSpan.FromHours(0));
        DateTimeOffset bOffset = new DateTimeOffset(year: 2017, month: 3, day: 24, hour: 0, minute: 0, second: 0, offset: TimeSpan.FromHours(8.5));
        DateTimeOffset cOffset = bOffset.ToUniversalTime();

        // This also compares as expected
        Console.WriteLine(aOffset == bOffset);           // false
        Console.WriteLine(bOffset == cOffset);           // true

        Console.WriteLine(bOffset.EqualsExact(cOffset)); // false b/c time zones are different
    }
 private static void ShowDateAndTimeInfo(DateTimeOffset newTime)
 {
     Console.WriteLine("{0} converts to {1}", sourceTime, newTime);
     Console.WriteLine("{0} and {1} are equal: {2}",
                       sourceTime, newTime, sourceTime.Equals(newTime));
     Console.WriteLine("{0} and {1} are identical: {2}",
                       sourceTime, newTime,
                       sourceTime.EqualsExact(newTime));
     Console.WriteLine();
 }
Exemplo n.º 4
0
        public void TestLocalToUniversalToLocal()
        {
            DateTime       localTime   = new DateTime(2017, 5, 1, 12, 0, 0);
            DateTimeOffset localOffset = new DateTimeOffset(localTime);
            DateTimeOffset zeroOffset  = localOffset.ToOffset(TimeSpan.Zero);

            Assert.AreEqual(localOffset, zeroOffset, "local offset equals zero offset");
            Assert.IsFalse(localOffset.EqualsExact(zeroOffset), "local offset exactly equals zero offset");

            DateTimeOffset universalOffset = localOffset.ToUniversalTime();
            DateTime       universalTime   = universalOffset.UtcDateTime;

            Assert.AreNotEqual(localTime, universalTime, "local time does not equal universal time");
            Assert.AreEqual(zeroOffset, universalOffset, "zero offset equals universal offset");
            Assert.IsTrue(zeroOffset.EqualsExact(universalOffset), "zero offset exactly equals universal offset");

            universalOffset = new DateTimeOffset(universalTime);
            DateTimeOffset localAgainOffset = universalOffset.ToLocalTime();
            DateTime       localAgainTime   = localAgainOffset.LocalDateTime;

            Assert.AreEqual(localTime, localAgainTime, "local time equals local again time");
            Assert.AreEqual(localOffset, localAgainOffset, "local offset equals local again offset");
            Assert.IsTrue(localOffset.EqualsExact(localAgainOffset), "local offset exactly equals local again offset");
        }
Exemplo n.º 5
0
        public void CompareTwoDateInDiffTZ()
        {
            DateTimeOffset dt1 = new DateTimeOffset(2007, 12, 16, 15, 06, 00, new TimeSpan(1, 0, 0));
            DateTimeOffset dt2 = new DateTimeOffset(2007, 12, 16, 9, 06, 00, new TimeSpan(-5, 0, 0));
            DateTimeOffset dt3 = new DateTimeOffset(2007, 12, 16, 14, 06, 00, new TimeSpan(1, 0, 0));
            object         o   = dt1;

            Assert.IsTrue(dt1.CompareTo(dt2) == 0);
            Assert.IsTrue(DateTimeOffset.Compare(dt1, dt2) == 0);
            Assert.IsTrue(dt1 == dt2);
            Assert.IsTrue(dt1.Equals(dt2));
            Assert.IsFalse(dt1 == dt3);
            Assert.IsTrue(dt1 != dt3);
            Assert.IsFalse(dt1.EqualsExact(dt2));
            Assert.IsTrue(dt1.CompareTo(dt3) > 0);
            Assert.IsTrue(((IComparable)dt1).CompareTo(o) == 0);
        }
Exemplo n.º 6
0
        public static void SetPropertyValue <T>(this T obj, ref DateTimeOffset field, DateTimeOffset value
                                                , PropertyChangedEventHandler onPropertyChanged, [CallerMemberName] String propertyName = "")
            where T : INotifyPropertyChanged
        {
            if (field.EqualsExact(value) == true)
            {
                return;
            }

            field = value;
            var eh = onPropertyChanged;

            if (eh != null)
            {
                eh(obj, new PropertyChangedEventArgs(propertyName));
            }
        }
Exemplo n.º 7
0
    private static void ConvertToUniversal()
    {
        // <Snippet16>
        DateTimeOffset localTime, otherTime, universalTime;

        // Define local time in local time zone
        localTime = new DateTimeOffset(new DateTime(2007, 6, 15, 12, 0, 0));
        Console.WriteLine("Local time: {0}", localTime);
        Console.WriteLine();

        // Convert local time to offset 0 and assign to otherTime
        otherTime = localTime.ToOffset(TimeSpan.Zero);
        Console.WriteLine("Other time: {0}", otherTime);
        Console.WriteLine("{0} = {1}: {2}",
                          localTime, otherTime,
                          localTime.Equals(otherTime));
        Console.WriteLine("{0} exactly equals {1}: {2}",
                          localTime, otherTime,
                          localTime.EqualsExact(otherTime));
        Console.WriteLine();

        // Convert other time to UTC
        universalTime = localTime.ToUniversalTime();
        Console.WriteLine("Universal time: {0}", universalTime);
        Console.WriteLine("{0} = {1}: {2}",
                          otherTime, universalTime,
                          universalTime.Equals(otherTime));
        Console.WriteLine("{0} exactly equals {1}: {2}",
                          otherTime, universalTime,
                          universalTime.EqualsExact(otherTime));
        Console.WriteLine();
        // The example produces the following output to the console:
        //    Local time: 6/15/2007 12:00:00 PM -07:00
        //
        //    Other time: 6/15/2007 7:00:00 PM +00:00
        //    6/15/2007 12:00:00 PM -07:00 = 6/15/2007 7:00:00 PM +00:00: True
        //    6/15/2007 12:00:00 PM -07:00 exactly equals 6/15/2007 7:00:00 PM +00:00: False
        //
        //    Universal time: 6/15/2007 7:00:00 PM +00:00
        //    6/15/2007 7:00:00 PM +00:00 = 6/15/2007 7:00:00 PM +00:00: True
        //    6/15/2007 7:00:00 PM +00:00 exactly equals 6/15/2007 7:00:00 PM +00:00: True
        // </Snippet16>
    }
Exemplo n.º 8
0
		public void CompareTwoDateInDiffTZ ()
		{
			DateTimeOffset dt1 = new DateTimeOffset (2007, 12, 16, 15, 06, 00, new TimeSpan (1, 0, 0));
			DateTimeOffset dt2 = new DateTimeOffset (2007, 12, 16, 9, 06, 00, new TimeSpan (-5, 0, 0));
			DateTimeOffset dt3 = new DateTimeOffset (2007, 12, 16, 14, 06, 00, new TimeSpan (1, 0, 0));
			object o = dt1;
			Assert.IsTrue (dt1.CompareTo (dt2) == 0);
			Assert.IsTrue (DateTimeOffset.Compare (dt1, dt2) == 0);
			Assert.IsTrue (dt1 == dt2);
			Assert.IsTrue (dt1.Equals (dt2));
			Assert.IsFalse (dt1 == dt3);
			Assert.IsTrue (dt1 != dt3);
			Assert.IsFalse (dt1.EqualsExact (dt2));
			Assert.IsTrue (dt1.CompareTo (dt3) > 0);
			Assert.IsTrue (((IComparable)dt1).CompareTo (o) == 0);
		}
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            var now = DateTime.Now;

            Console.WriteLine($"1.1 --> {now}");
            Console.WriteLine($"1.2 --> {TimeZoneInfo.ConvertTimeToUtc(now)}");
            Console.WriteLine($"1.3 --> {now.ToUniversalTime()}");

            var          dateTime = new DateTime(2019, 8, 29, 13, 55, 12);
            var          zoneId   = "W. Australia Standard Time";
            TimeZoneInfo zone     = TimeZoneInfo.FindSystemTimeZoneById(zoneId);

            Console.WriteLine($"2 --> {TimeZoneInfo.ConvertTimeToUtc(dateTime, zone)}");

            var dt1        = DateTime.Now;
            var dt2        = DateTime.UtcNow;
            var difference = dt1 - dt2;

            Console.WriteLine($"{dt1} - {dt2} = {difference}");

            var dti1        = DateTimeOffset.Now;
            var dti2        = DateTimeOffset.UtcNow;
            var difference2 = dti1 - dti2;

            Console.WriteLine($"{dti1} - {dti2} = {difference2}");

            var localTime = new DateTimeOffset(DateTime.Now);

            Console.WriteLine($"3 -> LOCAL TIME = {localTime}");

            var otherTime = localTime.ToOffset(TimeSpan.Zero);

            Console.WriteLine($"3 -> OTHER TIME = {otherTime}");

            Console.WriteLine($"3 -> EQUALS: {localTime.Equals(otherTime)}");
            Console.WriteLine($"3 -> EQUALS EXACT: {localTime.EqualsExact(otherTime)}");

            // obliczanie roznicy czasu pomiedzy klientem a serwerem
            TimeSpan serverOffset = TimeZoneInfo.Local.GetUtcOffset(DateTimeOffset.Now);

            Console.WriteLine($"4 --> {serverOffset}");

            string         clientString = "6/9/2019 1:00:05 -5:00";
            string         format       = @"M/d/yyyy H:m:s zzz";
            DateTimeOffset clientTime   = DateTimeOffset.ParseExact(clientString, format, CultureInfo.InvariantCulture);

            Console.WriteLine($"4 --> {clientTime}");

            DateTimeOffset serverTime = clientTime.ToOffset(serverOffset);

            Console.WriteLine($"4 --> SERVER TIME: {serverTime}");

            var t1   = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
            var t2   = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
            var noww = DateTimeOffset.Now;

            TimeSpan t1Offset    = t1.GetUtcOffset(noww);
            TimeSpan t2Offset    = t2.GetUtcOffset(noww);
            TimeSpan difference3 = t1Offset - t2Offset;

            Console.WriteLine($"5 --> {difference3}");
        }
Exemplo n.º 10
0
        public static void Equals(DateTimeOffset dateTimeOffset1, object obj, bool expectedEquals, bool expectedEqualsExact)
        {
            Assert.Equal(expectedEquals, dateTimeOffset1.Equals(obj));
            if (obj is DateTimeOffset)
            {
                DateTimeOffset dateTimeOffset2 = (DateTimeOffset)obj;
                Assert.Equal(expectedEquals, dateTimeOffset1.Equals(dateTimeOffset2));
                Assert.Equal(expectedEquals, DateTimeOffset.Equals(dateTimeOffset1, dateTimeOffset2));

                Assert.Equal(expectedEquals, dateTimeOffset1.GetHashCode().Equals(dateTimeOffset2.GetHashCode()));
                Assert.Equal(expectedEqualsExact, dateTimeOffset1.EqualsExact(dateTimeOffset2));

                Assert.Equal(expectedEquals, dateTimeOffset1 == dateTimeOffset2);
                Assert.Equal(!expectedEquals, dateTimeOffset1 != dateTimeOffset2);
            }
        }
Exemplo n.º 11
0
        public static void ExecuteExample()
        {
            var x = DateTime.Now;
            var y = DateTime.UtcNow;
            var z = DateTimeOffset.Now;
            var w = DateTimeOffset.UtcNow;
            DateTime dt = new DateTime(2000, 2, 3,
                10, 20, 30);

            Console.WriteLine(dt.Year);         // 2000
            Console.WriteLine(dt.Month);        // 2
            Console.WriteLine(dt.Day);          // 3
            Console.WriteLine(dt.DayOfWeek);    // Thursday
            Console.WriteLine(dt.DayOfYear);    // 34

            Console.WriteLine(dt.Hour);         // 10
            Console.WriteLine(dt.Minute);       // 20
            Console.WriteLine(dt.Second);       // 30
            Console.WriteLine(dt.Millisecond);  // 0
            Console.WriteLine(dt.Ticks);        // 630851700300000000
            Console.WriteLine(dt.TimeOfDay);    // 10:20:30  (returns a TimeSpan)

            TimeSpan ts = TimeSpan.FromMinutes(90);
            Console.WriteLine(dt.Add(ts));
            Console.WriteLine(dt + ts);             // same as above

            DateTime thisYear = new DateTime(2015, 1, 1);
            DateTime nextYear = thisYear.AddYears(1);
            TimeSpan oneYear = nextYear - thisYear;

            DateTime dt11 = new DateTime(2000, 1, 1, 10, 20, 30, DateTimeKind.Local);
            DateTime dt22 = new DateTime(2000, 1, 1, 10, 20, 30, DateTimeKind.Utc);
            Console.WriteLine(dt11 == dt22);          // True
            DateTime local = DateTime.Now;
            DateTime utc = local.ToUniversalTime();
            Console.WriteLine(local == utc);        // False

            DateTime d = new DateTime(2015, 12, 12);  // Unspecified
            DateTime utc2 = DateTime.SpecifyKind(d, DateTimeKind.Utc);
            Console.WriteLine(utc);            // 12/12/2015 12:00:00 AM

            DateTimeOffset local2 = DateTimeOffset.Now;
            DateTimeOffset utc3 = local.ToUniversalTime();

            Console.WriteLine(local2.Offset);   // -06:00:00 (in Central America)
            Console.WriteLine(utc3.Offset);     // 00:00:00

            Console.WriteLine(local2 == utc3);                 // True
            Console.WriteLine(local2.EqualsExact(utc3));      // False

            TimeZoneInfo zone = TimeZoneInfo.Local;
            Console.WriteLine(zone.StandardName);      // Pacific Standard Time
            Console.WriteLine(zone.DaylightName);      // Pacific Daylight Time
            DateTime dt1 = new DateTime(2019, 1, 1);   // DateTimeOffset works, too
            DateTime dt2 = new DateTime(2019, 6, 1);
            Console.WriteLine(zone.IsDaylightSavingTime(dt1));     // True
            Console.WriteLine(zone.IsDaylightSavingTime(dt2));     // False
            Console.WriteLine(zone.GetUtcOffset(dt1));             // -08:00:00
            Console.WriteLine(zone.GetUtcOffset(dt2));             // -07:00:00

            DateTime.Now.IsDaylightSavingTime();
        }
Exemplo n.º 12
0
 public bool EqualsExact(DateTimeOffset val)
 {
     return(value.EqualsExact(val));
 }
        public static void Show()
        {
            // DateTime and Time Zones
            {
                // When you compare two DateTime instances, only their ticks values are compared; their DateTimeKinds are ignored:

                DateTime dt1 = new DateTime(2000, 1, 1, 10, 20, 30, DateTimeKind.Local);
                DateTime dt2 = new DateTime(2000, 1, 1, 10, 20, 30, DateTimeKind.Utc);
                Console.WriteLine(dt1 == dt2);          // True

                DateTime local = DateTime.Now;
                DateTime utc   = local.ToUniversalTime();
                Console.WriteLine(local == utc);        // False

                // You can construct a DateTime that differs from another only in Kind with the static DateTime.SpecifyKind method:

                DateTime d    = new DateTime(2000, 12, 12); // Unspecified
                DateTime utc2 = DateTime.SpecifyKind(d, DateTimeKind.Utc);
                Console.WriteLine(utc2);                    // 12/12/2000 12:00:00 AM
            }

            // DateTimeOffset and Time Zones
            {
                // Comparisons look only at the (UTC) DateTime; the Offset is used primarily for formatting.

                DateTimeOffset local = DateTimeOffset.Now;
                DateTimeOffset utc   = local.ToUniversalTime(); // 只会影响 UTC 偏移量

                Console.WriteLine(local.Offset);                // -06:00:00 (in Central America)
                Console.WriteLine(utc.Offset);                  // 00:00:00

                Console.WriteLine(local == utc);                // True

                //To include the Offset in the comparison, you must use the EqualsExact method:

                Console.WriteLine(local.EqualsExact(utc));      // False
            }

            // TimeZone
            {
                // The static TimeZone.CurrentTimeZone method returns a TimeZone object based on the current local settings.
                TimeZone zone = TimeZone.CurrentTimeZone;
                zone.StandardName.Dump("StandardName");
                zone.DaylightName.Dump("DaylightName");

                // The IsDaylightSavingTime and GetUtcOffset methods work as follows:
                DateTime dt1 = new DateTime(2008, 1, 1);
                DateTime dt2 = new DateTime(2008, 6, 1);
                zone.IsDaylightSavingTime(dt1).Dump("IsDaylightSavingTime (January)");
                zone.IsDaylightSavingTime(dt2).Dump("IsDaylightSavingTime (June)");
                zone.GetUtcOffset(dt1).Dump("UTC Offset (January)");
                zone.GetUtcOffset(dt2).Dump("UTC Offset (June)");

                // The GetDaylightChanges method returns specific daylight saving information for a given year:
                DaylightTime day = zone.GetDaylightChanges(2010); // 返回指定年份的夏令时信息
                if (day == null)
                {
                    return;
                }
                day.Start.Dump("day.Start");
                day.End.Dump("day.End");
                day.Delta.Dump("day.Delta");
            }

            //TODO: 夏令时 的概念 暂时不了解
            // Daylight Saving and DateTime
            {
                // The IsDaylightSavingTime tells you whether a given local DateTime is subject to daylight saving.
                Console.WriteLine(DateTime.Now.IsDaylightSavingTime());     // True or False

                // UTC times always return false:
                Console.WriteLine(DateTime.UtcNow.IsDaylightSavingTime());  // Always False

                // The end of daylight saving presents a particular complication for algorithms that use local time.
                // The comments on the right show the results of running this in a daylight-saving-enabled zone:
                DaylightTime changes   = TimeZone.CurrentTimeZone.GetDaylightChanges(2010);
                TimeSpan     halfDelta = new TimeSpan(changes.Delta.Ticks / 2);
                DateTime     utc1      = changes.End.ToUniversalTime() - halfDelta;
                DateTime     utc2      = utc1 - changes.Delta;

                // Converting these variables to local times demonstrates why you should use UTC and not local time
                // if your code relies on time moving forward:
                DateTime loc1 = utc1.ToLocalTime(); // (Pacific Standard Time)
                DateTime loc2 = utc2.ToLocalTime();
                Console.WriteLine(loc1);            // 2/11/2010 1:30:00 AM
                Console.WriteLine(loc2);            // 2/11/2010 1:30:00 AM
                Console.WriteLine(loc1 == loc2);    // True

                // Despite loc1 and loc2 reporting as equal, they are different inside:
                Console.WriteLine(loc1.ToString("o"));  // 2010-11-02T02:30:00.0000000-08:00
                Console.WriteLine(loc2.ToString("o"));  // 2010-11-02T02:30:00.0000000-07:00

                // The extra bit ensures correct round-tripping between local and UTC times:
                Console.WriteLine(loc1.ToUniversalTime() == utc1);   // True
                Console.WriteLine(loc2.ToUniversalTime() == utc2);   // True
            }
        }