From: <mar...@us...> - 2010-02-06 23:07:01
|
Revision: 16 http://cronoscontrol.svn.sourceforge.net/cronoscontrol/?rev=16&view=rev Author: marioarce Date: 2010-02-06 23:06:55 +0000 (Sat, 06 Feb 2010) Log Message: ----------- Ticket #5 added DateTimeHelper.cs class that implements some methods for processing of dates and times Added Paths: ----------- source/trunk/CronosControl/Libraries/Common/Utils/DateTimeHelper.cs Added: source/trunk/CronosControl/Libraries/Common/Utils/DateTimeHelper.cs =================================================================== --- source/trunk/CronosControl/Libraries/Common/Utils/DateTimeHelper.cs (rev 0) +++ source/trunk/CronosControl/Libraries/Common/Utils/DateTimeHelper.cs 2010-02-06 23:06:55 UTC (rev 16) @@ -0,0 +1,138 @@ +//------------------------------------------------------------------------------ +// The contents of this file are subject to the GNU General Public License Version 3.0 ("License"); you may not use this file except in compliance with the License. +// You may obtain a copy of the License at http://www.cronoscontrol.net/license.html. +// +// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. +// See the License for the specific language governing rights and limitations under the License. +// +// The Original Code is CronosControl. +// The Initial Developer of the Original Code is WebImageConsulting http://www.wicnow.com/. +// All Rights Reserved. +// +// Contributor(s): Mario Alberto Arce, _______. +//------------------------------------------------------------------------------ + +using System; +using System.Linq; +using System.Text; +using System.Collections.Generic; +using System.Collections.ObjectModel; + +namespace CronosControl.Common.Utils +{ + /// <summary> + /// Represents a datetime helper + /// </summary> + public partial class DateTimeHelper + { + #region Methods + /// <summary> + /// Retrieves a System.TimeZoneInfo object from the registry based on its identifier. + /// </summary> + /// <param name="id">The time zone identifier, which corresponds to the System.TimeZoneInfo.Id property.</param> + /// <returns>A System.TimeZoneInfo object whose identifier is the value of the id parameter.</returns> + public static TimeZoneInfo FindTimeZoneById(string id) + { + return TimeZoneInfo.FindSystemTimeZoneById(id); + } + + /// <summary> + /// Returns a sorted collection of all the time zones + /// </summary> + /// <returns>A read-only collection of System.TimeZoneInfo objects.</returns> + public static ReadOnlyCollection<TimeZoneInfo> GetSystemTimeZones() + { + return TimeZoneInfo.GetSystemTimeZones(); + } + + /// <summary> + /// Converts the date and time to current user date and time + /// </summary> + /// <param name="dt">The date and time (respesents local system time or UTC time) to convert.</param> + /// <returns>A DateTime value that represents time that corresponds to the dateTime parameter in customer time zone.</returns> + public static DateTime ConvertToUserTime(DateTime dt) + { + TimeZoneInfo currentUserTimeZoneInfo = DateTimeHelper.CurrentTimeZone; + return TimeZoneInfo.ConvertTime(dt, currentUserTimeZoneInfo); + } + + /// <summary> + /// Converts the date and time to current user date and time + /// </summary> + /// <param name="dt">The date and time to convert.</param> + /// <param name="sourceTimeZone">The time zone of dateTime.</param> + /// <returns>A DateTime value that represents time that corresponds to the dateTime parameter in customer time zone.</returns> + public static DateTime ConvertToUserTime(DateTime dt, TimeZoneInfo sourceTimeZone) + { + TimeZoneInfo currentUserTimeZoneInfo = DateTimeHelper.CurrentTimeZone; + return TimeZoneInfo.ConvertTime(dt, sourceTimeZone, currentUserTimeZoneInfo); + } + + /// <summary> + /// Converts the date and time to Coordinated Universal Time (UTC) + /// </summary> + /// <param name="dt">The date and time (respesents local system time or UTC time) to convert.</param> + /// <returns>A DateTime value that represents the Coordinated Universal Time (UTC) that corresponds to the dateTime parameter. The DateTime value's Kind property is always set to DateTimeKind.Utc.</returns> + public static DateTime ConvertToUtcTime(DateTime dt) + { + return TimeZoneInfo.ConvertTimeToUtc(dt); + } + + /// <summary> + /// Converts the date and time to Coordinated Universal Time (UTC) + /// </summary> + /// <param name="dt">The date and time to convert.</param> + /// <param name="sourceTimeZone">The time zone of dateTime.</param> + /// <returns>A DateTime value that represents the Coordinated Universal Time (UTC) that corresponds to the dateTime parameter. The DateTime value's Kind property is always set to DateTimeKind.Utc.</returns> + public static DateTime ConvertToUtcTime(DateTime dt, TimeZoneInfo sourceTimeZone) + { + return TimeZoneInfo.ConvertTimeToUtc(dt, sourceTimeZone); + } + #endregion + + #region Properties + /// <summary> + /// Gets or sets a default store time zone + /// </summary> + private static TimeZoneInfo _defaultStoreTimeZone = null; + public static TimeZoneInfo DefaultStoreTimeZone + { + get + { + if (_defaultStoreTimeZone == null) + _defaultStoreTimeZone = TimeZoneInfo.Local; + return _defaultStoreTimeZone; + } + set + { + if (value != null) + { + _defaultStoreTimeZone = value; + } + } + } + + /// <summary> + /// Gets or sets the current user time zone + /// </summary> + private static TimeZoneInfo _currentTimeZone = null; + public static TimeZoneInfo CurrentTimeZone + { + get + { + if (_currentTimeZone == null) + _currentTimeZone = DateTimeHelper.DefaultStoreTimeZone; + + return _currentTimeZone; + } + set + { + if (value != null) + { + _currentTimeZone = value; + } + } + } + #endregion + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |