//******************************************************************************************************
// Ticks.cs - Gbtc
//
// Copyright © 2010, 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 Eclipse Public License -v 1.0 (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/eclipse-1.0.php
//
// 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:
// ----------------------------------------------------------------------------------------------------
// 11/12/2004 - J. Ritchie Carroll
// Initial version of source generated.
// 08/4/2009 - Josh L. Patterson
// Edited Code Comments.
// 09/14/2009 - Stephen C. Wills
// Added new header and license agreement.
// 10/8/2012 - Danyelle Gilliam
// Modified Header
//
//******************************************************************************************************
#region [ Contributor License Agreements ]
/**************************************************************************\
Copyright © 2009 - J. Ritchie Carroll
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\**************************************************************************/
#endregion
using GSF.Units;
using System;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
namespace GSF
{
#region [ Enumerations ]
///
/// Time intervals enumeration used by method.
///
public enum BaselineTimeInterval
{
/// Baseline timestamp to the second (i.e., starting at zero milliseconds).
Second,
/// Baseline timestamp to the minute (i.e., starting at zero seconds and milliseconds).
Minute,
/// Baseline timestamp to the hour (i.e., starting at zero minutes, seconds and milliseconds).
Hour,
/// Baseline timestamp to the day (i.e., starting at zero hours, minutes, seconds and milliseconds).
Day,
/// Baseline timestamp to the month (i.e., starting at day one, zero hours, minutes, seconds and milliseconds).
Month,
/// Baseline timestamp to the year (i.e., starting at month one, day one, zero hours, minutes, seconds and milliseconds).
Year
}
#endregion
///
/// Represents an instant in time, or time period, as a 64-bit signed integer with a value that is expressed as the number
/// of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001.
///
///
///
/// can represent an "instant in time" and therefore can be used exactly like a .
/// The difference between and is that is essentially a long
/// integer (i.e., an ) which represents the number of ticks that have elapsed since 12:00:00 midnight,
/// January 1, 0001 with each tick having a resolution of 100-nanoseconds. You would use in places where
/// you needed to directly represent time in high-resolution, i.e., time with subsecond accuracy, using an object that will
/// act like a long integer but handle time conversions. can also represent a "time period" (e.g., the
/// number of ticks elapsed since a process started) and thus can also be used like a ; when used in
/// this manner the method can be used to convert the value
/// into a handy textual representation of elapsed years, days, hours, minutes and seconds.
///
///
/// This class behaves just like an representing a time in ticks; it is implictly castable to and from
/// an and therefore can be generally used "as" an Int64 directly. It is also implicitly castable to and
/// from a , a , an and a .
///
///
[Serializable()]
public struct Ticks : IComparable, IFormattable, IConvertible, IComparable, IComparable, IComparable, IComparable, IEquatable, IEquatable, IEquatable, IEquatable
{
#region [ Members ]
// Nested Types
private struct TimeName
{
// Note that this is a structure so that elements may be used as an
// index into a string array without having to cast as (int)
public const int Year = 0;
public const int Years = 1;
public const int Day = 2;
public const int Days = 3;
public const int Hour = 4;
public const int Hours = 5;
public const int Minute = 6;
public const int Minutes = 7;
public const int Second = 8;
public const int Seconds = 9;
public const int LessThan60Seconds = 10;
public const int NoSeconds = 11;
}
// Constants
// Standard time names used by ToString method
private static string[] s_timeNames = new string[] { " Year", " Years", " Day", " Days", " Hour", " Hours", " Minute", " Minutes", " Second", " Seconds", "Less Than 60 Seconds", "0 Seconds" };
// Standard time names, without seconds, used by ToString method
private static string[] s_timeNamesNoSeconds = new string[] { " Year", " Years", " Day", " Days", " Hour", " Hours", " Minute", " Minutes", " Second", " Seconds", "Less Than 1 Minute", "0 Minutes" };
/// Number of 100-nanosecond ticks in one second.
public const long PerSecond = 10000000L;
/// Number of 100-nanosecond ticks in one millisecond.
public const long PerMillisecond = (long)(Ticks.PerSecond * SI.Milli);
/// Number of 100-nanosecond ticks in one microsecond.
public const long PerMicrosecond = (long)(Ticks.PerSecond * SI.Micro);
/// Number of 100-nanosecond ticks in one minute.
public const long PerMinute = 60L * Ticks.PerSecond;
/// Number of 100-nanosecond ticks in one hour.
public const long PerHour = 60L * Ticks.PerMinute;
/// Number of 100-nanosecond ticks in one day.
public const long PerDay = 24L * Ticks.PerHour;
// Fields
private long m_value; // Time value stored in ticks
#endregion
#region [ Constructors ]
///
/// Creates a new .
///
/// New time value in ticks.
public Ticks(long value)
{
m_value = value;
}
///
/// Creates a new .
///
/// New time value as a .
public Ticks(DateTime value)
{
m_value = value.Ticks;
}
///
/// Creates a new .
///
/// New time value as a .
public Ticks(TimeSpan value)
{
m_value = value.Ticks;
}
#endregion
#region [ Methods ]
///
/// Gets the value in equivalent number of seconds.
///
/// Value of in seconds.
///
/// If value represents an instant in time, returned value will represent the number of seconds
/// that have elapsed since 12:00:00 midnight, January 1, 0001.
///
public double ToSeconds()
{
return m_value / (double)Ticks.PerSecond;
}
///
/// Gets the value in equivalent number of milliseconds.
///
/// Value of in milliseconds.
///
/// If value represents an instant in time, returned value will represent the number of milliseconds
/// that have elapsed since 12:00:00 midnight, January 1, 0001.
///
public double ToMilliseconds()
{
return m_value / (double)Ticks.PerMillisecond;
}
///
/// Gets the value in equivalent number of microseconds.
///
/// Value of in microseconds.
///
/// If value represents an instant in time, returned value will represent the number of microseconds
/// that have elapsed since 12:00:00 midnight, January 1, 0001.
///
public double ToMicroseconds()
{
return m_value / (double)Ticks.PerMicrosecond;
}
///
/// Determines if time, represented by value in UTC time, is valid by comparing it to
/// the system clock.
///
/// 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 UTC time represented by value, is within the specified range.
///
/// Time, represented by value, is considered valid if it exists within the specified
/// and range of system clock time in UTC. Note
/// that and must be greater than zero, but can be set
/// to sub-second intervals.
///
///
/// and must be greater than zero, but can be less than one.
///
public bool UtcTimeIsValid(double lagTime, double leadTime)
{
return TimeIsValid(DateTime.UtcNow.Ticks, lagTime, leadTime);
}
///
/// Determines if time, represented by value in UTC time, is valid by comparing it to
/// the system clock.
///
/// The allowed lag time, in ticks, before assuming time is too old to be valid.
/// The allowed lead time, in ticks, before assuming time is too advanced to be valid.
/// True, if UTC time represented by value, is within the specified range.
///
/// Time, represented by value, is considered valid if it exists within the specified
/// and range of system clock time in UTC. Note
/// that and must be greater than zero.
///
///
/// and must be greater than zero.
///
public bool UtcTimeIsValid(Ticks lagTime, Ticks leadTime)
{
return TimeIsValid(DateTime.UtcNow.Ticks, lagTime, leadTime);
}
///
/// Determines if time, represented by value in local time, is valid by comparing it to
/// the system clock.
///
/// 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 local time represented by value, is within the specified range.
///
/// Time, represented by value, is considered valid if it exists within the specified
/// and range of local system clock time. Note
/// that and must be greater than zero, but can be set
/// to sub-second intervals.
///
///
/// and must be greater than zero, but can be less than one.
///
public bool LocalTimeIsValid(double lagTime, double leadTime)
{
return TimeIsValid(DateTime.Now.Ticks, lagTime, leadTime);
}
///
/// Determines if time, represented by value in local time, is valid by comparing it to
/// the system clock.
///
/// The allowed lag time, in ticks, before assuming time is too old to be valid.
/// The allowed lead time, in ticks, before assuming time is too advanced to be valid.
/// True, if local time represented by value, is within the specified range.
///
/// Time, represented by value, is considered valid if it exists within the specified
/// and range of local system clock time. Note
/// that and must be greater than zero.
///
///
/// and must be greater than zero.
///
public bool LocalTimeIsValid(Ticks lagTime, Ticks leadTime)
{
return TimeIsValid(DateTime.Now.Ticks, lagTime, leadTime);
}
///
/// Determines if time, represented by value, is valid by comparing it to the specified
/// current time.
///
/// Specified current time (e.g., could be DateTime.Now.Ticks).
/// 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 represented by value, is within the specified range.
///
/// Time, represented by value, is considered valid if it exists within the specified
/// and range of . Note
/// that and must be greater than zero, but can be set
/// to sub-second intervals.
///
///
/// and must be greater than zero, but can be less than one.
///
public bool TimeIsValid(Ticks currentTime, double lagTime, double leadTime)
{
if (lagTime <= 0)
throw new ArgumentOutOfRangeException("lagTime", "lagTime must be greater than zero, but it can be less than one");
if (leadTime <= 0)
throw new ArgumentOutOfRangeException("leadTime", "leadTime must be greater than zero, but it can be less than one");
double distance = (currentTime.m_value - m_value) / (double)Ticks.PerSecond;
return (distance >= -leadTime && distance <= lagTime);
}
///
/// Determines if time, represented by value, is valid by comparing it to the specified
/// current time.
///
/// Specified current time (e.g., could be DateTime.Now.Ticks).
/// The allowed lag time, in ticks, before assuming time is too old to be valid.
/// The allowed lead time, in ticks, before assuming time is too advanced to be valid.
/// True, if time represented by value, is within the specified range.
///
/// Time, represented by value, is considered valid if it exists within the specified
/// and range of . Note
/// that and must be greater than zero.
///
///
/// and must be greater than zero.
///
public bool TimeIsValid(Ticks currentTime, Ticks lagTime, Ticks leadTime)
{
if (lagTime.m_value <= 0)
throw new ArgumentOutOfRangeException("lagTime", "lagTime must be greater than zero");
if (leadTime.m_value <= 0)
throw new ArgumentOutOfRangeException("leadTime", "leadTime must be greater than zero");
long distance = (currentTime.m_value - m_value);
return (distance >= -leadTime.m_value && distance <= lagTime.m_value);
}
///
/// Gets the distance, in 100-nanoseconds intervals, beyond the top of the second in the timestamp
/// represented by the .
///
///
/// Number of 100-nanoseconds intervals value is from the top of the second.
///
public Ticks DistanceBeyondSecond()
{
return m_value - (m_value - m_value % Ticks.PerSecond);
}
///
/// Creates a new value that represents a baselined timestamp, in 100-nanoseconds
/// intervals, that begins at the beginning of the specified time interval.
///
///
/// to which timestamp should be baselined.
///
///
/// A new value that represents a baselined timestamp, in 100-nanoseconds intervals,
/// 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 Ticks BaselinedTimestamp(BaselineTimeInterval interval)
{
switch (interval)
{
case BaselineTimeInterval.Second:
return m_value - m_value % Ticks.PerSecond;
case BaselineTimeInterval.Minute:
return m_value - m_value % Ticks.PerMinute;
case BaselineTimeInterval.Hour:
return m_value - m_value % Ticks.PerHour;
case BaselineTimeInterval.Day:
return m_value - m_value % Ticks.PerDay;
case BaselineTimeInterval.Month:
DateTime toMonth = new DateTime(m_value);
return new DateTime(toMonth.Year, toMonth.Month, 1, 0, 0, 0, 0).Ticks;
case BaselineTimeInterval.Year:
return new DateTime((new DateTime(m_value)).Year, 1, 1, 0, 0, 0, 0).Ticks;
default:
return this;
}
}
///
/// Converts the value of the value to its equivalent string representation.
///
/// A string representation of the value.
public override string ToString()
{
return ((DateTime)this).ToString();
}
///
/// Converts the value to its equivalent string representation, using
/// the specified format.
///
/// A format string.
///
/// The string representation of the value of this instance as specified by format.
///
public string ToString(string format)
{
return ((DateTime)this).ToString(format);
}
///
/// Converts the value to its equivalent string representation, using
/// the specified culture-specific format information.
///
///
/// A that supplies culture-specific formatting information.
///
///
/// The string representation of the value of this instance as specified by provider.
///
public string ToString(IFormatProvider provider)
{
return ((DateTime)this).ToString(provider);
}
///
/// Converts the value to its equivalent string representation, using
/// specified format and culture-specific format information.
///
/// A format specification.
///
/// A that supplies culture-specific formatting information.
///
///
/// The string representation of the value of this instance as specified by format and provider.
///
public string ToString(string format, IFormatProvider provider)
{
return ((DateTime)this).ToString(format, provider);
}
///
/// Converts the value into a textual representation of years, days, hours,
/// minutes and seconds.
///
///
/// Note that this ToString overload will not display fractional seconds. To allow display of
/// fractional seconds, or completely remove second resolution from the textual representation,
/// use the overload instead.
///
///
/// The string representation of the value of this instance, consisting of the number of
/// years, days, hours, minutes and seconds represented by this value.
///
public string ToElapsedTimeString()
{
return ToElapsedTimeString(0, s_timeNames);
}
///
/// Converts the value into a textual representation of years, days, hours,
/// minutes and seconds with the specified number of fractional digits.
///
/// Number of fractional digits to display for seconds.
/// Set to -1 to suppress seconds display.
///
/// The string representation of the value of this instance, consisting of the number of
/// years, days, hours, minutes and seconds represented by this value.
///
public string ToElapsedTimeString(int secondPrecision)
{
if (secondPrecision < 0)
return ToElapsedTimeString(secondPrecision, s_timeNamesNoSeconds);
else
return ToElapsedTimeString(secondPrecision, s_timeNames);
}
///
/// Converts the value into a textual representation of years, days, hours,
/// minutes and seconds with the specified number of fractional digits given string array of
/// time names.
///
/// Number of fractional digits to display for seconds.
/// Time names array to use during textual conversion.
///
/// Set to -1 to suppress seconds display.
///
/// array needs one string entry for each of the following names:
/// "Year", "Years", "Day", "Days", "Hour", "Hours", "Minute", "Minutes", "Second", "Seconds",
/// "Less Than 60 Seconds", "0 Seconds".
///
///
///
/// The string representation of the value of this instance, consisting of the number of
/// years, days, hours, minutes and seconds represented by this value.
///
public string ToElapsedTimeString(int secondPrecision, string[] timeNames)
{
// One year of seconds estimated for display use as 365.2425 days, i.e., 31556952 seconds
const int SecondsPerYear = 31556952;
const int SecondsPerMinute = 60;
const int SecondsPerHour = 60 * SecondsPerMinute;
const int SecondsPerDay = 24 * SecondsPerHour;
StringBuilder timeImage = new StringBuilder();
int years, days, hours, minutes;
double seconds = ToSeconds();
// Checks if number of seconds ranges in years
years = (int)(seconds / SecondsPerYear);
if (years >= 1)
{
// Removes whole years from remaining seconds
seconds = seconds - years * SecondsPerYear;
// Appends textual representation of years
timeImage.Append(years);
if (years == 1)
timeImage.Append(timeNames[TimeName.Year]);
else
timeImage.Append(timeNames[TimeName.Years]);
}
// Checks if remaining number of seconds ranges in days
days = (int)(seconds / SecondsPerDay);
if (days >= 1)
{
// Removes whole days from remaining seconds
seconds = seconds - days * SecondsPerDay;
// Appends textual representation of days
timeImage.Append(' ');
timeImage.Append(days);
if (days == 1)
timeImage.Append(timeNames[TimeName.Day]);
else
timeImage.Append(timeNames[TimeName.Days]);
}
// Checks if remaining number of seconds ranges in hours
hours = (int)(seconds / SecondsPerHour);
if (hours >= 1)
{
// Removes whole hours from remaining seconds
seconds = seconds - hours * SecondsPerHour;
// Appends textual representation of hours
timeImage.Append(' ');
timeImage.Append(hours);
if (hours == 1)
timeImage.Append(timeNames[TimeName.Hour]);
else
timeImage.Append(timeNames[TimeName.Hours]);
}
// Checks if remaining number of seconds ranges in minutes
minutes = (int)(seconds / SecondsPerMinute);
if (minutes >= 1)
{
// Removes whole minutes from remaining seconds
seconds = seconds - minutes * SecondsPerMinute;
// If no fractional seconds were requested and remaining seconds are approximately 60,
// then we just add another minute
if (secondPrecision == 0 && (int)seconds == 60)
{
minutes++;
seconds = 0;
}
// Appends textual representation of minutes
timeImage.Append(' ');
timeImage.Append(minutes);
if (minutes == 1)
timeImage.Append(timeNames[TimeName.Minute]);
else
timeImage.Append(timeNames[TimeName.Minutes]);
}
// Handles remaining seconds
if (secondPrecision == 0)
{
// No fractional seconds requested. Rounds seconds to nearest integer
int wholeSeconds = (int)seconds;
if (wholeSeconds > 0)
{
// Appends textual representation of whole seconds
timeImage.Append(' ');
timeImage.Append(wholeSeconds);
if (wholeSeconds == 1)
timeImage.Append(timeNames[TimeName.Second]);
else
timeImage.Append(timeNames[TimeName.Seconds]);
}
}
else
{
// Handles fractional seconds request
if (seconds > 0)
{
if (secondPrecision < 0)
{
// If second display has been disabled and less than 60 seconds remain, we still need
// to show something
if (timeImage.Length == 0)
timeImage.Append(timeNames[TimeName.LessThan60Seconds]);
}
else
{
// Appends textual representation of fractional seconds
timeImage.Append(' ');
timeImage.Append(seconds.ToString("0." + (new string('0', secondPrecision))));
if (seconds == 1)
timeImage.Append(timeNames[TimeName.Second]);
else
timeImage.Append(timeNames[TimeName.Seconds]);
}
}
}
// Handles zero seconds display
if (timeImage.Length == 0)
timeImage.Append(timeNames[TimeName.NoSeconds]);
return timeImage.ToString().Trim();
}
#region [ Numeric Interface Implementations ]
///
/// Compares this instance to a specified object and returns an indication of their relative values.
///
/// An object to compare, or null.
///
/// A signed number indicating the relative values of this instance and value. Returns less than zero
/// if this instance is less than value, zero if this instance is equal to value, or greater than zero
/// if this instance is greater than value.
///
/// value is not an or .
public int CompareTo(object value)
{
if ((object)value == null)
return 1;
if (!(value is long) && !(value is Ticks) && !(value is DateTime) && !(value is TimeSpan))
throw new ArgumentException("Argument must be an Int64 or a Ticks");
long num = (Ticks)value;
return (m_value < num ? -1 : (m_value > num ? 1 : 0));
}
///
/// Compares this instance to a specified and returns an indication of their
/// relative values.
///
/// A to compare.
///
/// A signed number indicating the relative values of this instance and value. Returns less than zero
/// if this instance is less than value, zero if this instance is equal to value, or greater than zero
/// if this instance is greater than value.
///
public int CompareTo(Ticks value)
{
return CompareTo((long)value);
}
///
/// Compares this instance to a specified and returns an indication of their
/// relative values.
///
/// A to compare.
///
/// A signed number indicating the relative values of this instance and value. Returns less than zero
/// if this instance is less than value, zero if this instance is equal to value, or greater than zero
/// if this instance is greater than value.
///
public int CompareTo(DateTime value)
{
return CompareTo((Ticks)value);
}
///
/// Compares this instance to a specified and returns an indication of their
/// relative values.
///
/// A to compare.
///
/// A signed number indicating the relative values of this instance and value. Returns less than zero
/// if this instance is less than value, zero if this instance is equal to value, or greater than zero
/// if this instance is greater than value.
///
public int CompareTo(TimeSpan value)
{
return CompareTo((Ticks)value);
}
///
/// Compares this instance to a specified and returns an indication of their
/// relative values.
///
/// An to compare.
///
/// A signed number indicating the relative values of this instance and value. Returns less than zero
/// if this instance is less than value, zero if this instance is equal to value, or greater than zero
/// if this instance is greater than value.
///
public int CompareTo(long value)
{
return (m_value < value ? -1 : (m_value > value ? 1 : 0));
}
///
/// Returns a value indicating whether this instance is equal to a specified object.
///
/// An object to compare, or null.
///
/// True if obj is an instance of or and equals the value of this instance;
/// otherwise, False.
///
public override bool Equals(object obj)
{
if (obj is long || obj is Ticks || obj is DateTime || obj is TimeSpan)
return Equals((Ticks)obj);
return false;
}
///
/// Returns a value indicating whether this instance is equal to a specified value.
///
/// A value to compare to this instance.
///
/// True if obj has the same value as this instance; otherwise, False.
///
public bool Equals(Ticks obj)
{
return Equals((long)obj);
}
///
/// Returns a value indicating whether this instance is equal to a specified value.
///
/// A value to compare to this instance.
///
/// True if obj has the same value as this instance; otherwise, False.
///
public bool Equals(DateTime obj)
{
return Equals((Ticks)obj);
}
///
/// Returns a value indicating whether this instance is equal to a specified value.
///
/// A value to compare to this instance.
///
/// True if obj has the same value as this instance; otherwise, False.
///
public bool Equals(TimeSpan obj)
{
return Equals((Ticks)obj);
}
///
/// Returns a value indicating whether this instance is equal to a specified value.
///
/// An value to compare to this instance.
///
/// True if obj has the same value as this instance; otherwise, False.
///
public bool Equals(long obj)
{
return (m_value == obj);
}
///
/// Returns the hash code for this instance.
///
///
/// A 32-bit signed integer hash code.
///
public override int GetHashCode()
{
return m_value.GetHashCode();
}
///
/// Returns the for value type .
///
/// The enumerated constant, .
public TypeCode GetTypeCode()
{
return TypeCode.Int64;
}
#region [ Explicit IConvertible Implementation ]
// These are explicitly implemented on the native System.Int64 implementations, so we do the same...
bool IConvertible.ToBoolean(IFormatProvider provider)
{
return Convert.ToBoolean(m_value, provider);
}
char IConvertible.ToChar(IFormatProvider provider)
{
return Convert.ToChar(m_value, provider);
}
sbyte IConvertible.ToSByte(IFormatProvider provider)
{
return Convert.ToSByte(m_value, provider);
}
byte IConvertible.ToByte(IFormatProvider provider)
{
return Convert.ToByte(m_value, provider);
}
short IConvertible.ToInt16(IFormatProvider provider)
{
return Convert.ToInt16(m_value, provider);
}
ushort IConvertible.ToUInt16(IFormatProvider provider)
{
return Convert.ToUInt16(m_value, provider);
}
int IConvertible.ToInt32(IFormatProvider provider)
{
return Convert.ToInt32(m_value, provider);
}
uint IConvertible.ToUInt32(IFormatProvider provider)
{
return Convert.ToUInt32(m_value, provider);
}
long IConvertible.ToInt64(IFormatProvider provider)
{
return m_value;
}
ulong IConvertible.ToUInt64(IFormatProvider provider)
{
return Convert.ToUInt64(m_value, provider);
}
float IConvertible.ToSingle(IFormatProvider provider)
{
return Convert.ToSingle(m_value, provider);
}
double IConvertible.ToDouble(IFormatProvider provider)
{
return Convert.ToDouble(m_value, provider);
}
decimal IConvertible.ToDecimal(IFormatProvider provider)
{
return Convert.ToDecimal(m_value, provider);
}
DateTime IConvertible.ToDateTime(IFormatProvider provider)
{
return Convert.ToDateTime(m_value, provider);
}
object IConvertible.ToType(Type type, IFormatProvider provider)
{
return Convert.ChangeType(m_value, type, provider);
}
#endregion
#endregion
#endregion
#region [ Operators ]
#region [ Comparison Operators ]
///
/// Compares the two values for equality.
///
/// left hand operand.
/// right hand operand.
/// value representing the result.
public static bool operator ==(Ticks value1, Ticks value2)
{
return value1.Equals(value2);
}
///
/// Compares the two values for inequality.
///
/// left hand operand.
/// right hand operand.
/// value representing the result.
public static bool operator !=(Ticks value1, Ticks value2)
{
return !value1.Equals(value2);
}
///
/// Returns true if left value is less than right value.
///
/// left hand operand.
/// right hand operand.
/// value representing the result.
public static bool operator <(Ticks value1, Ticks value2)
{
return (value1.CompareTo(value2) < 0);
}
///
/// Returns true if left value is less or equal to than right value.
///
/// left hand operand.
/// right hand operand.
/// value representing the result.
public static bool operator <=(Ticks value1, Ticks value2)
{
return (value1.CompareTo(value2) <= 0);
}
///
/// Returns true if left value is greater than right value.
///
/// left hand operand.
/// right hand operand.
/// value representing the result.
public static bool operator >(Ticks value1, Ticks value2)
{
return (value1.CompareTo(value2) > 0);
}
///
/// Returns true if left value is greater than or equal to right value.
///
/// left hand operand.
/// right hand operand.
/// value representing the result.
public static bool operator >=(Ticks value1, Ticks value2)
{
return (value1.CompareTo(value2) >= 0);
}
#endregion
#region [ Type Conversion Operators ]
///
/// Implicitly converts value, represented in ticks, to a .
///
/// value to convert.
/// value representing the result.
public static implicit operator Ticks(Int64 value)
{
return new Ticks(value);
}
///
/// Implicitly converts value, represented as a , to a .
///
/// value to convert.
/// value representing the result.
public static implicit operator Ticks(DateTime value)
{
return new Ticks(value);
}
///
/// Implicitly converts value, represented as a , to a .
///
/// value to convert.
/// value representing the result.
public static implicit operator Ticks(TimeSpan value)
{
return new Ticks(value);
}
///
/// Implicitly converts value, represented as a , to a .
///
/// value to convert.
/// value representing the result.
public static implicit operator Ticks(TimeTagBase value)
{
return new Ticks(value.ToDateTime());
}
///
/// Implicitly converts , represented in ticks, to an .
///
/// value to convert.
/// value representing the result.
public static implicit operator Int64(Ticks value)
{
return value.m_value;
}
///
/// Implicitly converts , represented in ticks, to a .
///
/// value to convert.
/// value representing the result.
public static implicit operator DateTime(Ticks value)
{
return new DateTime(value.m_value);
}
///
/// Implicitly converts , represented in ticks, to a .
///
/// value to convert.
/// value representing the result.
public static implicit operator TimeSpan(Ticks value)
{
return new TimeSpan(value.m_value);
}
///
/// Implicitly converts , represented in ticks, to an .
///
/// value to convert.
/// value representing the result.
public static implicit operator NtpTimeTag(Ticks value)
{
return new NtpTimeTag(new DateTime(value.m_value));
}
///
/// Implicitly converts , represented in ticks, to a .
///
/// value to convert.
/// value representing the result.
public static implicit operator UnixTimeTag(Ticks value)
{
return new UnixTimeTag(new DateTime(value.m_value));
}
#endregion
#region [ Boolean and Bitwise Operators ]
///
/// Returns true if value is not zero.
///
/// value to evaluate.
/// value representing the result.
public static bool operator true(Ticks value)
{
return (value.m_value != 0);
}
///
/// Returns true if value is equal to zero.
///
/// value to evaluate.
/// value representing the result.
public static bool operator false(Ticks value)
{
return (value.m_value == 0);
}
///
/// Returns bitwise complement of value.
///
/// value to evaluate.
/// value representing the result.
public static Ticks operator ~(Ticks value)
{
return ~value.m_value;
}
///
/// Returns logical bitwise AND of values.
///
/// left hand operand.
/// right hand operand.
/// value representing the result.
public static Ticks operator &(Ticks value1, Ticks value2)
{
return value1.m_value & value2.m_value;
}
///
/// Returns logical bitwise OR of values.
///
/// left hand operand.
/// right hand operand.
/// value representing the result.
public static Ticks operator |(Ticks value1, Ticks value2)
{
return value1.m_value | value2.m_value;
}
///
/// Returns logical bitwise exclusive-OR of values.
///
/// left hand operand.
/// right hand operand.
/// value representing the result.
public static Ticks operator ^(Ticks value1, Ticks value2)
{
return value1.m_value ^ value2.m_value;
}
///
/// Returns value after right shifts of first value by the number of bits specified by second value.
///
/// value to shift.
/// number of bits to shift.
/// value representing the result.
public static Ticks operator >>(Ticks value, int shifts)
{
return (Ticks)(value.m_value >> shifts);
}
///
/// Returns value after left shifts of first value by the number of bits specified by second value.
///
/// value to shift.
/// number of bits to shift.
/// value representing the result.
public static Ticks operator <<(Ticks value, int shifts)
{
return (Ticks)(value.m_value << shifts);
}
#endregion
#region [ Arithmetic Operators ]
///
/// Returns computed remainder after dividing first value by the second.
///
/// Left hand operand.
/// Right hand operand.
/// value representing the result.
public static Ticks operator %(Ticks value1, Ticks value2)
{
return value1.m_value % value2.m_value;
}
///
/// Returns computed sum of values.
///
/// Left hand operand.
/// Right hand operand.
/// value representing the result.
public static Ticks operator +(Ticks value1, Ticks value2)
{
return value1.m_value + value2.m_value;
}
///
/// Returns computed difference of values.
///
/// Left hand operand.
/// Right hand operand.
/// value representing the result.
public static Ticks operator -(Ticks value1, Ticks value2)
{
return value1.m_value - value2.m_value;
}
///
/// Returns computed product of values.
///
/// Left hand operand.
/// Right hand operand.
/// value representing the result.
public static Ticks operator *(Ticks value1, Ticks value2)
{
return value1.m_value * value2.m_value;
}
// Integer division operators
///
/// Returns computed division of values.
///
/// Left hand operand.
/// Right hand operand.
/// value representing the result.
public static Ticks operator /(Ticks value1, Ticks value2)
{
return value1.m_value / value2.m_value;
}
// C# doesn't expose an exponent operator but some other .NET languages do,
// so we expose the operator via its native special IL function name
///
/// Returns result of first value raised to power of second value.
///
/// Left hand operand.
/// Right hand operand.
/// value representing the result.
[EditorBrowsable(EditorBrowsableState.Advanced), SpecialName()]
public static double op_Exponent(Ticks value1, Ticks value2)
{
return Math.Pow((double)value1.m_value, (double)value2.m_value);
}
#endregion
#endregion
#region [ Static ]
// Static Fields
/// Represents the largest possible value of a . This field is constant.
public static readonly Ticks MaxValue = (Ticks)long.MaxValue;
/// Represents the smallest possible value of a . This field is constant.
public static readonly Ticks MinValue = (Ticks)long.MinValue;
// Static Methods
///
/// Converts , in 100-nanosecond tick intervals, to seconds.
///
/// Number of ticks to convert to seconds.
/// Number seconds represented by specified in ticks.
///
/// If represents an instant in time, returned value will represent the number of seconds
/// that have elapsed since 12:00:00 midnight, January 1, 0001.
///
public static double ToSeconds(Ticks value)
{
return value / (double)Ticks.PerSecond;
}
///
/// Converts , in 100-nanosecond tick intervals, to milliseconds.
///
/// Number of ticks to convert to milliseconds.
/// Number milliseconds represented by specified in ticks.
///
/// If represents an instant in time, returned value will represent the number of milliseconds
/// that have elapsed since 12:00:00 midnight, January 1, 0001.
///
public static double ToMilliseconds(Ticks value)
{
return value / (double)Ticks.PerMillisecond;
}
///
/// Converts , in 100-nanosecond tick intervals, to microseconds.
///
/// Number of ticks to convert to microseconds.
/// Number microseconds represented by specified in ticks.
///
/// If represents an instant in time, returned value will represent the number of microseconds
/// that have elapsed since 12:00:00 midnight, January 1, 0001.
///
public static double ToMicroseconds(Ticks value)
{
return value / (double)Ticks.PerMicrosecond;
}
///
/// Creates a new from the specified in seconds.
///
/// New value in seconds.
/// New object from the specified in seconds.
public static Ticks FromSeconds(double value)
{
return new Ticks((long)(value * Ticks.PerSecond));
}
///
/// Creates a new from the specified in milliseconds.
///
/// New value in milliseconds.
/// New object from the specified in milliseconds.
public static Ticks FromMilliseconds(double value)
{
return new Ticks((long)(value * Ticks.PerMillisecond));
}
///
/// Creates a new from the specified in microseconds.
///
/// New value in microseconds.
/// New object from the specified in microseconds.
public static Ticks FromMicroseconds(double value)
{
return new Ticks((long)(value * Ticks.PerMicrosecond));
}
///
/// Converts the string representation of a number to its equivalent.
///
/// A string containing a number to convert.
///
/// A equivalent to the number contained in s.
///
/// s is null.
///
/// s represents a number less than or greater than .
///
/// s is not in the correct format.
public static Ticks Parse(string s)
{
return (Ticks)long.Parse(s);
}
///
/// Converts the string representation of a number in a specified style to its equivalent.
///
/// A string containing a number to convert.
///
/// A bitwise combination of System.Globalization.NumberStyles values that indicates the permitted format of s.
///
///
/// A equivalent to the number contained in s.
///
///
/// style is not a System.Globalization.NumberStyles value. -or- style is not a combination of
/// System.Globalization.NumberStyles.AllowHexSpecifier and System.Globalization.NumberStyles.HexNumber values.
///
/// s is null.
///
/// s represents a number less than or greater than .
///
/// s is not in a format compliant with style.
public static Ticks Parse(string s, NumberStyles style)
{
return (Ticks)long.Parse(s, style);
}
///
/// Converts the string representation of a number in a specified culture-specific format to its equivalent.
///
/// A string containing a number to convert.
///
/// A that supplies culture-specific formatting information about s.
///
///
/// A equivalent to the number contained in s.
///
/// s is null.
///
/// s represents a number less than or greater than .
///
/// s is not in the correct format.
public static Ticks Parse(string s, IFormatProvider provider)
{
return (Ticks)long.Parse(s, provider);
}
///
/// Converts the string representation of a number in a specified style and culture-specific format to its equivalent.
///
/// A string containing a number to convert.
///
/// A bitwise combination of System.Globalization.NumberStyles values that indicates the permitted format of s.
///
///
/// A that supplies culture-specific formatting information about s.
///
///
/// A equivalent to the number contained in s.
///
///
/// style is not a System.Globalization.NumberStyles value. -or- style is not a combination of
/// System.Globalization.NumberStyles.AllowHexSpecifier and System.Globalization.NumberStyles.HexNumber values.
///
/// s is null.
///
/// s represents a number less than or greater than .
///
/// s is not in a format compliant with style.
public static Ticks Parse(string s, NumberStyles style, IFormatProvider provider)
{
return (Ticks)long.Parse(s, style, provider);
}
///
/// Converts the string representation of a number to its equivalent. A return value
/// indicates whether the conversion succeeded or failed.
///
/// A string containing a number to convert.
///
/// When this method returns, contains the value equivalent to the number contained in s,
/// if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null,
/// is not of the correct format, or represents a number less than or greater than .
/// This parameter is passed uninitialized.
///
/// true if s was converted successfully; otherwise, false.
public static bool TryParse(string s, out Ticks result)
{
long parseResult;
bool parseResponse;
parseResponse = long.TryParse(s, out parseResult);
result = parseResult;
return parseResponse;
}
///
/// Converts the string representation of a number in a specified style and culture-specific format to its
/// equivalent. A return value indicates whether the conversion succeeded or failed.
///
/// A string containing a number to convert.
///
/// A bitwise combination of System.Globalization.NumberStyles values that indicates the permitted format of s.
///
///
/// When this method returns, contains the value equivalent to the number contained in s,
/// if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null,
/// is not in a format compliant with style, or represents a number less than or
/// greater than . This parameter is passed uninitialized.
///
///
/// A object that supplies culture-specific formatting information about s.
///
/// true if s was converted successfully; otherwise, false.
///
/// style is not a System.Globalization.NumberStyles value. -or- style is not a combination of
/// System.Globalization.NumberStyles.AllowHexSpecifier and System.Globalization.NumberStyles.HexNumber values.
///
public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out Ticks result)
{
long parseResult;
bool parseResponse;
parseResponse = long.TryParse(s, style, provider, out parseResult);
result = parseResult;
return parseResponse;
}
#endregion
}
}