public void ConvertAEDTToUTCTest()
        {
            var dtLocal = DateTime.Parse(new DateTime(2014, 4, 30, 0, 0, 0).ToString());

            // convert AEDT to UTC
            // When AEST is DST, the difference between AEST and UTC is 11 hours
            // todo: need to find a way to create a AEDT localtime
            dtLocal = DateTime.Parse(new DateTime(2014, 3, 30, 0, 0, 0).ToString());
            var dtAEDTtoUTC = TimeHelper.ConvertToTimezone(dtLocal, TimeHelper.AEST, TimeHelper.UTC);

            // Assert.IsTrue(dtLocal.IsDaylightSavingTime());
            dtLocal.Should().Be(dtAEDTtoUTC.AddHours(11));

            // convert to GMT (non-Daylight saving time)
            // When AEST is DST and GMT is not DST, the difference between two is 11 hours
            var dtAEDSTtoGMT = TimeHelper.ConvertToTimezone(dtLocal, TimeHelper.AEST, TimeHelper.GMT);

            dtLocal.Should().Be(dtAEDSTtoGMT.AddHours(11));
        }
        public void ConvertAESTToUTCTest()
        {
            // convert AEST to UTC
            // When AEST is not DST
            var dtLocal     = DateTime.Parse(new DateTime(2014, 4, 30, 0, 0, 0).ToString());
            var dtAESTtoUTC = TimeHelper.ConvertToTimezone(dtLocal, TimeHelper.AEST, TimeHelper.UTC);

            dtLocal.IsDaylightSavingTime().Should().BeFalse();
            dtLocal.Should().Be(dtAESTtoUTC.AddHours(10));

            // use ToUniversalTime
            // When AEST is not DST and GMT is not DST, the difference between AEST and GMT is 10 hours
            dtAESTtoUTC = dtLocal.ToUniversalTime();
            dtLocal.Should().Be(dtAESTtoUTC.AddHours(_offset.Hours));

            // convert to GMT (Daylight saving time)
            // When AEST is not DST and GMT is DST, the difference between AEST and GMT is 9 hours
            var dtAESTtoGMT = TimeHelper.ConvertToTimezone(dtLocal, TimeZoneInfo.Local, TimeHelper.GMT);

            dtAESTtoGMT.IsDaylightSavingTime().Should().BeFalse();
            TimeHelper.GMT.IsDaylightSavingTime(dtAESTtoGMT).Should().BeTrue();
            dtLocal.Should().Be(dtAESTtoGMT.AddHours(_offset.Hours - 1));
        }
Example #3
0
        public void ConvertToTimezoneTest()
        {
            // convert local to utc
            DateTime dateTimeLocal        = DateTime.Now;
            var      offset               = TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow);
            bool     isDaylightSavingTime = dateTimeLocal.IsDaylightSavingTime();

            if (isDaylightSavingTime)
            {
                Assert.IsTrue(dateTimeLocal.AddHours(offset.Hours * -1) == dateTimeLocal.ToUniversalTime());
            }
            else
            {
                Assert.IsTrue(dateTimeLocal.AddHours(offset.Hours * -1) == dateTimeLocal.ToUniversalTime());
            }

            // convert undefined to local. ToLocalTime assume the unspecified time as UTC
            DateTime dateTimeUnspecified = DateTime.Parse(DateTime.Today.ToString());
            DateTime local = dateTimeUnspecified.ToLocalTime();

            if (offset.Hours != 0)
            {
                Assert.IsFalse(dateTimeUnspecified == local);
            }

            // convert AEST to UTC
            // When AEST is not DST
            DateTime dtLocal     = DateTime.Parse(new DateTime(2014, 4, 30, 0, 0, 0).ToString());
            DateTime dtAESTtoUTC = TimeHelper.ConvertToTimezone(dtLocal, TimeHelper.AEST, TimeHelper.UTC);

            Assert.IsFalse(dtLocal.IsDaylightSavingTime());
            Assert.IsTrue(dtLocal == dtAESTtoUTC.AddHours(10));

            // use ToUniversalTime
            // When AEST is not DST and GMT is not DST, the difference between AEST and GMT is 10 hours
            dtAESTtoUTC = dtLocal.ToUniversalTime();
            Assert.IsTrue(dtLocal == dtAESTtoUTC.AddHours(offset.Hours));

            // convert to GMT (Daylight saving time)
            // When AEST is not DST and GMT is DST, the difference between AEST and GMT is 9 hours
            DateTime dtAESTtoGMT = TimeHelper.ConvertToTimezone(dtLocal, TimeZoneInfo.Local, TimeHelper.GMT);

            Assert.IsFalse(dtAESTtoGMT.IsDaylightSavingTime());
            Assert.IsTrue(TimeHelper.GMT.IsDaylightSavingTime(dtAESTtoGMT));
            Assert.IsTrue(dtLocal == dtAESTtoGMT.AddHours(offset.Hours - 1));

            // convert AEDT to UTC
            // When AEST is DST, the difference between AEST and UTC is 11 hours
            // todo: need to find a way to create a AEDT localtime
            dtLocal = DateTime.Parse(new DateTime(2014, 3, 30, 0, 0, 0).ToString());
            DateTime dtAEDTtoUTC = TimeHelper.ConvertToTimezone(dtLocal, TimeHelper.AEST, TimeHelper.UTC);

            // Assert.IsTrue(dtLocal.IsDaylightSavingTime());
            Assert.IsTrue(dtLocal == dtAEDTtoUTC.AddHours(11));

            // convert to GMT (non-Daylight saving time)
            // When AEST is DST and GMT is not DST, the difference between two is 11 hours
            DateTime dtAEDSTtoGMT = TimeHelper.ConvertToTimezone(dtLocal, TimeHelper.AEST, TimeHelper.GMT);

            Assert.IsTrue(dtLocal == dtAEDSTtoGMT.AddHours(11));
        }