Click here to Skip to main content
15,891,473 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.3K   466   37  
An implementation that wraps DateTime to allow for keeping track of TimeZone state
/*
 * Created by: Steinar Dragsnes
 * Created: 21.11.2008
 */

using System;

namespace DateTimeZone
{
	
	/// <summary>
	/// Contract for a simple time zone representation.
	/// </summary>
	public interface ITimeZone
	{
		/// <summary>
		/// This time zone's offset from GMT.
		/// </summary>
		double Offset { get; }

		/// <summary>
		/// Calculated hour offset derived from Offset.
		/// </summary>
		int HourOffset { get; }

		/// <summary>
		/// Calculated minute offset derived from Offset.
		/// </summary>
		int MinuteOffset { get; }

		/// <summary>
		/// The Id.
		/// </summary>
		string CanonicalID { get; }

		/// <summary>
		/// Location alias.
		/// </summary>
		string Aliases { get; }

		/// <summary>
		/// The definition of when a summer time zone change occurs.
		/// </summary>
		DaylightSavingTime SummerChange { get; }

		/// <summary>
		/// The definition of when a revertion to a standard time zone change occurs.
		/// </summary>
		DaylightSavingTime WinterChange { get; }

		///<summary>
		///Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
		///</summary>
		///
		///<returns>
		///A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
		///</returns>
		///<filterpriority>2</filterpriority>
		string ToString();

		/// <summary>
		/// Printing the content of this
		/// </summary>
		/// <returns>A string formatted to print the c# code needed to define the TimeZone based on the values in this class</returns>
		string PrintValues();

		/// <summary>
		/// A convenience method that checks if the newly converted date for a time zone is within that
		/// time zone's definted DST rules.
		/// </summary>
		/// <param name="time"></param>
		/// <returns></returns>
		bool IsDST(System.DateTime time);

		/// <summary>
		/// Convenience getter for checking if a time zone has DST.
		/// </summary>
		bool HasDST { get; }

	    /// <summary>
	    /// Convenience method for calculating the point in time when the winter time starts.
	    /// </summary>
	    /// <param name="year">For which year to calculate the winter change.</param>
	    /// <returns>A point in time representing the winter change.</returns>
	    DateTime GetDSTEnd(int year);

	    /// <summary>
	    /// Convenience method for calculating the point in time when the summer time starts.
	    /// </summary>
	    /// <param name="year">For which year to calculate the summer change.</param>
	    /// <returns>A point in time representing the summer change.</returns>
	    DateTime GetDSTStart(int year);
	}
}

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