Click here to Skip to main content
15,894,955 members
Articles / Programming Languages / C#

A TimeZone Aware DateTime Implementation

Rate me:
Please Sign up or sign in to vote.
3.56/5 (10 votes)
24 Feb 2010CPOL6 min read 70.4K   466   37  
An implementation that wraps DateTime to allow for keeping track of TimeZone state
using NUnit.Framework;
using DateTimeZone;

namespace TestDateTimeZone
{
    /// <summary>
    /// Summary description for UtcDateTimeTest
    /// </summary>
    [TestFixture]
    public class UtcDateTimeTest
    {
        
        [Test]
		public void TestLocalDateTimeCreation()
		{
			//winter time test
			LocalDateTime anHour = new LocalDateTime(2009, 10, 1, 23, 0, 0, TimeZones.UTC, true);

			Assert.AreEqual(23, anHour.Hour);
		}

		[Test]
		public void TestUtcCreation()
		{
			//winter time test
			UtcDateTime anHour = new UtcDateTime(2009, 10, 1, 23, 0, 0);

			Assert.AreEqual(23, anHour.Hour);
		}


		[Test]
		public void TestUtcMin()
		{
			UtcDateTime oct1st2300 = new UtcDateTime(2009, 10, 1, 23, 0, 0);
			UtcDateTime oct1st2200 = new UtcDateTime(2009, 10, 1, 22, 0, 0);

			UtcDateTime minUtcDateTime = DateUtil.Min(oct1st2300, oct1st2200);
			Assert.IsTrue(minUtcDateTime.GetDateTime() == oct1st2200.GetDateTime());
		}

		[Test]
		public void TestUtcMax()
		{
			UtcDateTime oct1st2300 = new UtcDateTime(2009, 10, 1, 23, 0, 0);
			UtcDateTime oct1st2200 = new UtcDateTime(2009, 10, 1, 22, 0, 0);

			UtcDateTime maxUtcDateTime = DateUtil.Max(oct1st2300, oct1st2200);
			Assert.IsTrue(maxUtcDateTime.GetDateTime() == oct1st2300.GetDateTime());
		}

		[Test]
		public void TestStartOfDayInCETTimeZone()
		{
			UtcDateTime utc = new UtcDateTime(2009, 5,1,23,35,0);

			CetDateTime cetDateTime = new CetDateTime(utc.ToLocalDateTime(TimeZones.CET));
			CetDateTime startOfDay = (CetDateTime) cetDateTime.Date;

			Assert.IsTrue(startOfDay.Equals(new CetDateTime(2009, 5, 2, 0, 0, 0)));

		}

		[Test]
		public void TestEndOfDayInCETTimeZone()
		{
			UtcDateTime utc = new UtcDateTime(2009, 5, 1, 23, 35, 0);

			CetDateTime cetDateTime = new CetDateTime(utc.ToLocalDateTime(TimeZones.CET));
			LocalDateTime time = cetDateTime.Date.AddDays(1).AsLocalDateTime();
			
			CetDateTime endOfDay = new CetDateTime(time.GetDateTime());

			Assert.IsTrue(endOfDay.Equals(new CetDateTime(2009, 5, 3, 0, 0, 0)));

		}

		[Test]
		public void TestNull()
		{
			UtcDateTime nullUtc = UtcDateTime.Null;
			Assert.IsTrue(nullUtc.IsNull);
			Assert.AreEqual(TimeZones.UTC, nullUtc.TimeZone);
		}

		[Test]
		public void TestCompareTo()
		{
			UtcDateTime referenceDateTime = new UtcDateTime(2009, 11, 13, 12, 0, 0);

			// Compare the reference date time with another equal date time, CompareTo should then return 0
			UtcDateTime equalDateTime = new UtcDateTime(2009, 11, 13, 12, 0, 0);
			int compareToResult = referenceDateTime.CompareTo(equalDateTime);
			Assert.IsTrue(compareToResult == 0);

			// Compare the reference date time with a date time which is earlier, CompareTo should then return a positive value
			// because the reference date time is larger than the earlier date time.
			UtcDateTime earlierDateTime = new UtcDateTime(2009, 11, 13, 11, 0, 0);
			compareToResult = referenceDateTime.CompareTo(earlierDateTime);
			Assert.IsTrue(compareToResult > 0);

			// Compare the reference date time with a date time which is later, CompareTo should then return a negative value
			// because the reference date time is smaller than the later date time.
			UtcDateTime laterDateTime = new UtcDateTime(2009, 11, 13, 13, 0, 0);
			compareToResult = referenceDateTime.CompareTo(laterDateTime);
			Assert.IsTrue(compareToResult < 0);
		}
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Norway Norway
http://www.linkedin.com/in/steinardragsnes

Comments and Discussions