//****************************************************************************************************** // DateTimeExtensions.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 02/23/2003 - J. Ritchie Carroll // Generated original version of source code. // 12/21/2005 - J. Ritchie Carroll // Migrated 2.0 version of source code from 1.1 source (GSF.Shared.DateTime). // 08/31/2007 - Darrell Zuercher // Edited code comments. // 09/08/2008 - J. Ritchie Carroll // Converted to C# extensions. // 02/16/2009 - Josh L. Patterson // Edited Code Comments. // 09/14/2009 - Stephen C. Wills // Added new header and license agreement. // 01/06/2010 - Andrew K. Hill // Modified the following methods per unit testing: // LocalTimeTo(DateTime, string) // LocalTimeTo(DateTime, TimeZoneInfo) // UniversalTimeTo(DateTime, string) // UniversalTimeTo(DateTime, TimeZoneInfo) // TimeZoneToTimeZone(DateTime, string, string) // TimeZoneToTimeZone(DateTime, TimeZoneInfo, TimeZoneInfo) // 12/14/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; using System.Globalization; namespace GSF { /// /// Defines extension functions related to Date/Time manipulation. /// public static class DateTimeExtensions { /// Determines if the specified UTC time is valid, by comparing it to the system clock. /// UTC time to test for validity. /// The allowed lag time, in seconds, before assuming time is too old to be valid. /// The allowed lead time, in seconds, before assuming time is too advanced to be /// valid. /// True, if time is within the specified range. /// /// Time is considered valid if it exists within the specified lag time/lead time range of current /// time. /// Note that lag time and lead time must be greater than zero, but can be set to sub-second /// intervals. /// /// LagTime and LeadTime must be greater than zero, but can /// be less than one. public static bool UtcTimeIsValid(this DateTime utcTime, double lagTime, double leadTime) { return ((Ticks)utcTime).UtcTimeIsValid(lagTime, leadTime); } /// Determines if the specified local time is valid, by comparing it to the system clock. /// Time to test for validity. /// The allowed lag time, in seconds, before assuming time is too old to be valid. /// The allowed lead time, in seconds, before assuming time is too advanced to be /// valid. /// True, if time is within the specified range. /// /// Time is considered valid if it exists within the specified lag time/lead time range of current /// time. /// Note that lag time and lead time must be greater than zero, but can be set to sub-second /// intervals. /// /// LagTime and LeadTime must be greater than zero, but can /// be less than one. public static bool LocalTimeIsValid(this DateTime localTime, double lagTime, double leadTime) { return ((Ticks)localTime).LocalTimeIsValid(lagTime, leadTime); } /// Determines if time is valid, by comparing it to the specified current time. /// Time to test for validity. /// Specified current time (e.g., could be Date.Now or Date.UtcNow). /// The allowed lag time, in seconds, before assuming time is too old to be valid. /// The allowed lead time, in seconds, before assuming time is too advanced to be /// valid. /// True, if time is within the specified range. /// /// Time is considered valid if it exists within the specified lag time/lead time range of current /// time. /// Note that lag time and lead time must be greater than zero, but can be set to sub-second /// intervals. /// /// LagTime and LeadTime must be greater than zero, but can /// be less than one. public static bool TimeIsValid(this DateTime testTime, DateTime currentTime, double lagTime, double leadTime) { return ((Ticks)testTime).TimeIsValid(currentTime, lagTime, leadTime); } /// Gets the distance, in , beyond the top of the second. /// Timestamp to evaluate. /// Timestamp's tick distance from the top of the second. public static Ticks DistanceBeyondSecond(this DateTime timestamp) { return ((Ticks)timestamp).DistanceBeyondSecond(); } /// Creates a baselined timestamp which begins at the specified time interval. /// Timestamp to baseline. /// /// to which should be baselined. /// /// /// A new value that represents a baselined timestamp that begins at the /// specified . /// /// /// Baselining to the would return the /// value starting at zero milliseconds.
/// Baselining to the would return the /// value starting at zero seconds and milliseconds.
/// Baselining to the would return the /// value starting at zero minutes, seconds and milliseconds.
/// Baselining to the would return the /// value starting at zero hours, minutes, seconds and milliseconds.
/// Baselining to the would return the /// value starting at day one, zero hours, minutes, seconds and milliseconds.
/// Baselining to the would return the /// value starting at month one, day one, zero hours, minutes, seconds and milliseconds. ///
public static DateTime BaselinedTimestamp(this DateTime timestamp, BaselineTimeInterval interval) { return ((Ticks)timestamp).BaselinedTimestamp(interval); } /// Converts given local time to Eastern time. /// Timestamp in local time to be converted to Eastern time. /// /// Timestamp in Eastern time. /// public static DateTime LocalTimeToEasternTime(this DateTime timestamp) { return TimeZoneInfo.ConvertTime(timestamp, TimeZoneInfo.Local, USTimeZones.Eastern); } /// Converts given local time to Central time. /// Timestamp in local time to be converted to Central time. /// /// Timestamp in Central time. /// public static DateTime LocalTimeToCentralTime(this DateTime timestamp) { return TimeZoneInfo.ConvertTime(timestamp, TimeZoneInfo.Local, USTimeZones.Central); } /// Converts given local time to Mountain time. /// Timestamp in local time to be converted to Mountain time. /// /// Timestamp in Mountain time. /// public static DateTime LocalTimeToMountainTime(this DateTime timestamp) { return TimeZoneInfo.ConvertTime(timestamp, TimeZoneInfo.Local, USTimeZones.Mountain); } /// Converts given local time to Pacific time. /// Timestamp in local time to be converted to Pacific time. /// /// Timestamp in Pacific time. /// public static DateTime LocalTimeToPacificTime(this DateTime timestamp) { return TimeZoneInfo.ConvertTime(timestamp, TimeZoneInfo.Local, USTimeZones.Pacific); } /// Converts given local time to Universally Coordinated Time (a.k.a., Greenwich Meridian Time). /// This function is only provided for the sake of completeness. All it does is call the /// "ToUniversalTime" property on the given timestamp. /// Timestamp in local time to be converted to Universal time. /// /// Timestamp in UniversalTime (a.k.a., GMT). /// public static DateTime LocalTimeToUniversalTime(this DateTime timestamp) { return timestamp.ToUniversalTime(); } /// Converts given local time to time in specified time zone. /// Timestamp in local time to be converted to time in specified time zone. /// Standard name of desired end time zone for given /// timestamp. /// /// Timestamp in specified time zone. /// public static DateTime LocalTimeTo(this DateTime timestamp, string destinationTimeZoneStandardName) { if ((object)destinationTimeZoneStandardName == null) throw new ArgumentNullException("destinationTimeZoneStandardName"); return TimeZoneInfo.ConvertTime(timestamp, TimeZoneInfo.Local, TimeZoneInfo.FindSystemTimeZoneById(destinationTimeZoneStandardName)); } /// Converts given local time to time in specified time zone. /// Timestamp in local time to be converted to time in specified time zone. /// Desired end time zone for given timestamp. /// /// Timestamp in specified time zone. /// public static DateTime LocalTimeTo(this DateTime timestamp, TimeZoneInfo destinationTimeZone) { if ((object)destinationTimeZone == null) throw new ArgumentNullException("destinationTimeZone"); return TimeZoneInfo.ConvertTime(timestamp, TimeZoneInfo.Local, destinationTimeZone); } /// /// Converts the specified Universally Coordinated Time timestamp to Eastern time timestamp. /// /// The Universally Coordinated Time timestamp that is to be converted. /// The timestamp in Eastern time. public static DateTime UniversalTimeToEasternTime(this DateTime universalTimestamp) { return TimeZoneInfo.ConvertTime(universalTimestamp, TimeZoneInfo.Utc, USTimeZones.Eastern); } /// /// Converts the specified Universally Coordinated Time timestamp to Central time timestamp. /// /// The Universally Coordinated Time timestamp that is to be converted. /// The timestamp in Central time. public static DateTime UniversalTimeToCentralTime(this DateTime universalTimestamp) { return TimeZoneInfo.ConvertTime(universalTimestamp, TimeZoneInfo.Utc, USTimeZones.Central); } /// /// Converts the specified Universally Coordinated Time timestamp to Mountain time timestamp. /// /// The Universally Coordinated Time timestamp that is to be converted. /// The timestamp in Mountain time. public static DateTime UniversalTimeToMountainTime(this DateTime universalTimestamp) { return TimeZoneInfo.ConvertTime(universalTimestamp, TimeZoneInfo.Utc, USTimeZones.Mountain); } /// /// Converts the specified Universally Coordinated Time timestamp to Pacific time timestamp. /// /// The Universally Coordinated Time timestamp that is to be converted. /// The timestamp in Pacific time. public static DateTime UniversalTimeToPacificTime(this DateTime universalTimestamp) { return TimeZoneInfo.ConvertTime(universalTimestamp, TimeZoneInfo.Utc, USTimeZones.Pacific); } /// /// Converts the specified Universally Coordinated Time timestamp to timestamp in specified time zone. /// /// The Universally Coordinated Time timestamp that is to be converted. /// The time zone standard name to which the Universally /// Coordinated Time timestamp is to be converted to. /// The timestamp in the specified time zone. public static DateTime UniversalTimeTo(this DateTime universalTimestamp, string destinationTimeZoneStandardName) { if ((object)destinationTimeZoneStandardName == null) throw new ArgumentNullException("destinationTimeZoneStandardName"); return TimeZoneInfo.ConvertTime(universalTimestamp, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById(destinationTimeZoneStandardName)); } /// /// Converts the specified Universally Coordinated Time timestamp to timestamp in specified time zone. /// /// The Universally Coordinated Time timestamp that is to be converted. /// The time zone to which the Universally Coordinated Time timestamp /// is to be converted to. /// The timestamp in the specified time zone. public static DateTime UniversalTimeTo(this DateTime universalTimestamp, TimeZoneInfo destinationTimeZone) { if ((object)destinationTimeZone == null) throw new ArgumentNullException("destinationTimeZone"); return TimeZoneInfo.ConvertTime(universalTimestamp, TimeZoneInfo.Utc, destinationTimeZone); } /// Converts given timestamp from one time zone to another using standard names for time zones. /// Timestamp in source time zone to be converted to time in destination time zone. /// Standard name of time zone for given source timestamp. /// Standard name of desired end time zone for given source /// timestamp. /// /// Timestamp in destination time zone. /// public static DateTime TimeZoneToTimeZone(this DateTime timestamp, string sourceTimeZoneStandardName, string destinationTimeZoneStandardName) { if ((object)sourceTimeZoneStandardName == null) throw new ArgumentNullException("sourceTimeZoneStandardName"); if ((object)destinationTimeZoneStandardName == null) throw new ArgumentNullException("destinationTimeZoneStandardName"); return TimeZoneInfo.ConvertTime(timestamp, TimeZoneInfo.FindSystemTimeZoneById(sourceTimeZoneStandardName), TimeZoneInfo.FindSystemTimeZoneById(destinationTimeZoneStandardName)); } /// Converts given timestamp from one time zone to another. /// Timestamp in source time zone to be converted to time in destination time /// zone. /// Time zone for given source timestamp. /// Desired end time zone for given source timestamp. /// /// Timestamp in destination time zone. /// public static DateTime TimeZoneToTimeZone(this DateTime timestamp, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone) { if ((object)sourceTimeZone == null) throw new ArgumentNullException("sourceTimeZone"); if ((object)destinationTimeZone == null) throw new ArgumentNullException("destinationTimeZone"); return TimeZoneInfo.ConvertTime(timestamp, sourceTimeZone, destinationTimeZone); } /// Gets the abbreviated month name for month of the timestamp. /// Timestamp from which month name is extracted. /// String representation of the month name based on public static string AbbreviatedMonthName(this DateTime timestamp) { return DateTimeFormatInfo.CurrentInfo.GetAbbreviatedMonthName(timestamp.Month); } /// Gets the full month name for month of the timestamp. /// Timestamp from which month name is extracted. /// String representation of the month name based on public static string MonthName(this DateTime timestamp) { return DateTimeFormatInfo.CurrentInfo.GetMonthName(timestamp.Month); } /// Gets the abbreviated weekday name for weekday of the timestamp. /// Timestamp from which weekday name is extracted. /// String representation of the weekday name based on public static string AbbreviatedWeekdayName(this DateTime timestamp) { return DateTimeFormatInfo.CurrentInfo.GetAbbreviatedDayName(timestamp.DayOfWeek); } /// Gets the shortest weekday name for weekday of the timestamp. /// Timestamp from which weekday name is extracted. /// String representation of the short weekday name based on public static string ShortWeekdayName(this DateTime timestamp) { return DateTimeFormatInfo.CurrentInfo.GetShortestDayName(timestamp.DayOfWeek); } /// Gets the full weekday name for weekday of the timestamp. /// Timestamp from which weekday name is extracted. /// String representation of the weekday name based on public static string WeekdayName(this DateTime timestamp) { return DateTimeFormatInfo.CurrentInfo.GetDayName(timestamp.DayOfWeek); } } }