Exemple #1
0
        public void TestTimezoneOffset()
        {
            int year = Rand.GetInt(2, 3000);
            int mon = Rand.GetInt(1, 12);
            int day = Rand.GetInt(1, DateTime.DaysInMonth(year, mon));
            int hour = Rand.GetInt(0, 23);
            int min = Rand.GetInt(0, 59);
            int sec = Rand.GetInt(0, 59);

            double tz = Rand.GetDouble(3, UTCDate.TIMEZONE_MIN, UTCDate.TIMEZONE_MAX);

            UTCDate udt = new UTCDate(tz, null, year, mon, day, hour, min, sec);
            DateTime dt = new DateTime(year, mon, day, hour, min, sec);

            dt = dt.AddHours(-tz); // resolve tz offset

            Assert.True(udt.ExtractUTC().CompareTo(dt) == 0);
            Assert.True(udt.ExtractUTC().Kind == DateTimeKind.Utc);
            Assert.True(udt.ExtractStandard().Kind == DateTimeKind.Local);
            Assert.True(udt.ExtractLocal().Kind == DateTimeKind.Local);
        }
Exemple #2
0
        /**
         * Traverse year and verify dst status.
         */
        private void TestDST(int tz, DaylightTime dst)
        {
            DateTime lower = dst.Start;
            TimeSpan dst_span = dst.Delta;

            int day = 15;
            int hour = 12;
            int min = 0;
            int sec = 0;

            for (int i=UTCDate.MONTH_MIN; i<=UTCDate.MONTH_MAX; i++) {
                // compute dates using UTCDate
                UTCDate udt = new UTCDate(tz, dst, lower.Year, i, day, hour, min, sec);
                DateTime dt_utc = udt.ExtractUTC();
                DateTime dt_std = udt.ExtractStandard();
                DateTime dt_loc = udt.ExtractLocal();

                // compute dates manually

                // utc time -> resolve timezone offset and dst
                DateTime dt_utc2 = new DateTime(lower.Year, i, day, hour, min, sec,
                                                DateTimeKind.Utc);
                dt_utc2 = dt_utc2.AddHours(-tz); // resolve tz offset
                if (udt.IsDST) dt_utc2 = dt_utc2.Add(-dst_span); // resolve dst

                // standard time -> resolve dst only
                DateTime dt_std2 = new DateTime(lower.Year, i, day, hour, min, sec,
                                                DateTimeKind.Local);
                if (udt.IsDST) dt_std2 = dt_std2.Add(-dst_span); // resolve dst

                // standard time -> given as input
                DateTime dt_loc2 = new DateTime(lower.Year, i, day, hour, min, sec,
                                                DateTimeKind.Local);

                Assert.True(dt_utc.CompareTo(dt_utc2) == 0);
                Assert.True(dt_std.CompareTo(dt_std2) == 0);
                Assert.True(dt_loc.CompareTo(dt_loc2) == 0);
            }
        }