Click here to Skip to main content
15,895,740 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 System;
using System.Data.SqlTypes;
using Enum=System.Enum;

namespace DateTimeZone
{
	/// <summary>
	/// Convenience class for storing values needed to convey time zone information.
	/// </summary>
	[Serializable]
	public struct DaylightSavingTime : INullable
	{
		public readonly string DayOccurrenceInMonth;
		public readonly int Month;
		public readonly string DayOfWeek;
		public readonly int Hour;
		public readonly int Minutes;
	    private bool isNull;

	    /// <summary>
		/// Default constructor that fully initializes the instance.
		/// </summary>
		/// <param name="occurrence"></param>
		/// <param name="month"></param>
		/// <param name="day"></param>
		/// <param name="hour"></param>
		/// <param name="minutes"></param>
		public DaylightSavingTime(DayOccurrenceInMonth occurrence, int month, DayOfWeek day, int hour, int minutes)
		{
			DayOccurrenceInMonth = Enum.GetName(typeof(DayOccurrenceInMonth), occurrence);
			DayOfWeek = Enum.GetName(typeof(DayOfWeek), day);
			Month = month;
			Hour = hour;
			Minutes = minutes;
	        isNull = false;
		}

        /// <summary>
        /// Returns a nullable instance.
        /// </summary>
	    public static DaylightSavingTime Null
	    {
	        get
	        {
	            DaylightSavingTime dst = new DaylightSavingTime {isNull = true};
	            return dst;
	        }
	    }

		/// <summary>
		/// Convenience method for accessing the stored DayOccurrenceInMonth value as an enum.
		/// </summary>
		/// <returns>The DayOccurrenceInMonth enum value.</returns>
		public DayOccurrenceInMonth GetDayOccurrenceInMonth()
		{
			return (DayOccurrenceInMonth)Enum.Parse(typeof(DayOccurrenceInMonth), DayOccurrenceInMonth);
		}

		/// <summary>
		/// Convenience method for accessing the stored DayOfWeek value as an enum.
		/// </summary>
		/// <returns>The DayOfWeek enum value.</returns>
		public DayOfWeek GetDayOfWeek()
		{
			return (DayOfWeek)Enum.Parse(typeof(DayOfWeek), DayOfWeek);
		}

	    /// <summary>
	    /// Indicates whether a structure is null. This property is read-only.
	    /// </summary>
	    /// <returns>
	    /// <see cref="T:System.Data.SqlTypes.SqlBoolean"></see>true if the value of this object is null. Otherwise, false.
	    /// </returns>
	    public bool IsNull
	    {
            get { return isNull; }
	    }
	}
}

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