Click here to Skip to main content
15,886,787 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.1K   466   37  
An implementation that wraps DateTime to allow for keeping track of TimeZone state
using System;

namespace DateTimeZone
{
	/// <summary>
	/// Various time constants stored here. Should consider moving this to
	/// the same namespace as DateUtil (Viz.Core.Util).
	/// </summary>
    public static class TimeConstants
    {
		/// <summary>Number of days in a year.</summary>
        public const int DAYS_IN_YEAR = 365;

		/// <summary>Number of days in a week.</summary>
        public const int DAYS_IN_WEEK = 7;

		/// <summary>Number of days in a week end.</summary>
		public const int DAYS_IN_WEEK_END = 2;

		/// <summary>Number of days in a work week.</summary>
		public const int DAYS_IN_WORK_WEEK = 5;

		/// <summary>The first day in a month is always day 1.</summary>
		public const int FIRST_DAY_IN_MONTH = 1;

		/// <summary>Number of hours in a day.</summary>
        public const int HOURS_IN_DAY = 24;

		/// <summary>Number of hours in a week.</summary>
        public const int HOURS_IN_WEEK = 168;

		/// <summary>Number of months in a year.</summary>
		public const int MONTHS_IN_YEAR = 12;

		/// <summary>Number of months in a half year.</summary>
		public const int MONTHS_IN_HALF_YEAR = 6;

		/// <summary>Number of months in a season.</summary>
		public const int MONTHS_IN_SEASON = 6;

		/// <summary>Constant defining the end day of a week</summary>
		public const VizWeek END_OF_WEEK = VizWeek.Sunday;

		/// <summary>Constant defining the start day of a week</summary>
		public const VizWeek START_OF_ISO_WEEK = VizWeek.Monday;

		/// <summary>Constant defining the first day in a week end</summary>
		public const VizWeek FIRST_DAY_IN_WEEK_END = VizWeek.Saturday;

		/// <summary>Constant defining the last day in a week end</summary>
		public const VizWeek LAST_DAY_IN_WEEK_END = VizWeek.Saturday;


		/// <summary>Min date.</summary>
		public static readonly DateTime MIN_DATE = new DateTime(1753, 1, 1);
		
		/// <summary>Min date.</summary>
		/// <remarks>
		/// Must set it to the second day as a time shift to another time zone might tip it to day one.
		/// And if 1st of January is used it might tip the year back to 1752 which in turn will make the
		/// SQL-server fail on persisting the date.
		/// </remarks>
		public static readonly UtcDateTime MIN_UTC_DATE = new UtcDateTime(1753, 1, 2);

		/// <summary>Max date in UTC.</summary>
		public static readonly UtcDateTime MAX_UTC_DATE = new UtcDateTime(DateTime.MaxValue);

		public static readonly LocalDateTime MAX_LDT_UTC_DATE = MAX_UTC_DATE.AddDaysToUtcDateTime(-1).AsLocalDateTime();

		/// <summary>Time span for one day.</summary>
		public static readonly TimeSpan ONE_DAY_TIME_SPAN = new TimeSpan(1, 0, 0, 0);

		/// <summary>Time span for one second.</summary>
		public static readonly TimeSpan ONE_SECOND_TIME_SPAN = new TimeSpan(0, 0, 0, 1);

		public static readonly string[] MONTH_NAMES = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
		public static readonly string[] MONTH_SHORT_NAMES = new string[] { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
		public static readonly string[] WEEK_DAY_NAMES = new string[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

    }
}

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