//******************************************************************************************************// Time.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:// ----------------------------------------------------------------------------------------------------// 01/25/2008 - J. Ritchie Carroll// Initial version of source generated.// 09/11/2008 - J. Ritchie Carroll// Converted to C#.// 08/10/2009 - Josh L. Patterson// Edited Comments.// 09/14/2009 - Stephen C. Wills// Added new header and license agreement.// 12/14/2012 - Starlynn Danyelle Gilliam// Modified Header.// 10/03/2017 - J. Ritchie Carroll// Added units enumeration with associated Convert method.////****************************************************************************************************** #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 System;using System.ComponentModel;using System.Diagnostics.CodeAnalysis;using System.Globalization;using System.Runtime.CompilerServices;using System.Text; namespace GSF.Units{ #region [ Enumerations ] /// <summary> /// Represents the units available for a <see cref="Time"/> value. /// </summary> public enum TimeUnit { /// <summary> /// Second time units. /// </summary> Seconds, /// <summary> /// Minute time units. /// </summary> Minutes, /// <summary> /// Hour time units. /// </summary> Hours, /// <summary> /// Day time units. /// </summary> Days, /// <summary> /// Week time units. /// </summary> Weeks, /// <summary> /// Tick time units, 100-nanosecond intervals since 1/1/0001. /// </summary> Ticks, /// <summary> /// Atomic time units. /// </summary> AtomicUnitsOfTime, /// <summary> /// Planck time units. /// </summary> PlanckTime, /// <summary> /// Ke time units, traditional Chinese unit of decimal time. /// </summary> Ke } #endregion /// <summary> /// Represents a time measurement, in seconds, as a double-precision floating-point number. /// </summary> /// <remarks> /// This class behaves just like a <see cref="double"/> representing a time in seconds; it is implicitly /// castable to and from a <see cref="double"/> and therefore can be generally used "as" a double, but it /// has the advantage of handling conversions to and from other time representations, specifically /// minutes, hours, days, weeks, atomic units of time, Planck time and ke. Metric conversions are handled /// simply by applying the needed <see cref="SI"/> conversion factor, for example: /// <example> /// Convert time in nanoseconds to seconds: /// <code> /// public Time GetSeconds(double nanoseconds) /// { /// return nanoseconds * SI.Nano; /// } /// </code> /// Convert time in seconds to milliseconds: /// <code> /// public double GetMilliseconds(Time seconds) /// { /// return time / SI.Milli; /// } /// </code> /// This example converts minutes to hours: /// <code> /// public double GetHours(double minutes) /// { /// return Time.FromMinutes(minutes).ToHours(); /// } /// </code> /// </example> /// <para> /// Note that the <see cref="Time.ToString()"/> method will convert the <see cref="Time"/> value, in seconds, /// into a textual representation of years, days, hours, minutes and seconds using the static function /// <see cref="ToElapsedTimeString"/>. /// </para> /// </remarks> // ReSharper disable RedundantNameQualifier [Serializable] public struct Time : IComparable, IFormattable, IConvertible, IComparable<Time>, IComparable<TimeSpan>, IComparable<double>, IEquatable<Time>, IEquatable<TimeSpan>, IEquatable<double> { #region [ Members ] // Nested Types // Indices into TimeNames array 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 LessThan = 10; } // Constants private const double AtomicUnitsOfTimeFactor = 2.418884254e-17D; private const double PlanckTimeFactor = 1.351211818e-43D; private const double KeFactor = 864.0D; /// <summary> /// Fractional number of seconds in one tick. /// </summary> public const double SecondsPerTick = 1.0D / Ticks.PerSecond; /// <summary> /// Number of seconds in one minute. /// </summary> public const int SecondsPerMinute = 60; /// <summary> /// Number of seconds in one hour. /// </summary> public const int SecondsPerHour = 60 * SecondsPerMinute; /// <summary> /// Number of seconds in one day. /// </summary> public const int SecondsPerDay = 24 * SecondsPerHour; /// <summary> /// Number of seconds in one week. /// </summary> public const int SecondsPerWeek = 7 * SecondsPerDay; // Fields private readonly double m_value; // Time value stored in seconds #endregion #region [ Constructors ] /// <summary> /// Creates a new <see cref="Time"/>. /// </summary> /// <param name="value">New time value in seconds.</param> public Time(double value) { m_value = value; } /// <summary> /// Creates a new <see cref="Time"/>. /// </summary> /// <param name="value">New time value as a <see cref="TimeSpan"/>.</param> public Time(TimeSpan value) { m_value = value.TotalSeconds; } #endregion #region [ Methods ] /// <summary> /// Gets the <see cref="Time"/> value in atomic units of time. /// </summary> /// <returns>Value of <see cref="Time"/> in atomic units of time.</returns> public double ToAtomicUnitsOfTime() { return m_value / AtomicUnitsOfTimeFactor; } /// <summary> /// Gets the <see cref="Time"/> value in Planck time. /// </summary> /// <returns>Value of <see cref="Time"/> in Planck time.</returns> public double ToPlanckTime() { return m_value / PlanckTimeFactor; } /// <summary> /// Gets the <see cref="Time"/> value in ke, the traditional Chinese unit of decimal time. /// </summary> /// <returns>Value of <see cref="Time"/> in ke.</returns> public double ToKe() { return m_value / KeFactor; } /// <summary> /// Gets the <see cref="Time"/> value in minutes. /// </summary> /// <returns>Value of <see cref="Time"/> in minutes.</returns> public double ToMinutes() { return m_value / SecondsPerMinute; } /// <summary> /// Gets the <see cref="Time"/> value in hours. /// </summary> /// <returns>Value of <see cref="Time"/> in hours.</returns> public double ToHours() { return m_value / SecondsPerHour; } /// <summary> /// Gets the <see cref="Time"/> value in days. /// </summary> /// <returns>Value of <see cref="Time"/> in days.</returns> public double ToDays() { return m_value / SecondsPerDay; } /// <summary> /// Gets the <see cref="Time"/> value in weeks. /// </summary> /// <returns>Value of <see cref="Time"/> in weeks.</returns> public double ToWeeks() { return m_value / SecondsPerWeek; } /// <summary> /// Converts the <see cref="Time"/> value, in seconds, to 100-nanosecond tick intervals. /// </summary> /// <returns>A <see cref="Ticks"/> object.</returns> public Ticks ToTicks() { return (Ticks)(m_value * Ticks.PerSecond); } /// <summary> /// Converts the <see cref="Time"/> to the specified <paramref name="targetUnit"/>. /// </summary> /// <param name="targetUnit">Target units.</param> /// <returns><see cref="Time"/> converted to <paramref name="targetUnit"/>.</returns> /// <remarks> /// When converting to <see cref="TimeUnit.Ticks"/>, precision loss will /// occur for values that are outside the range of -2^52 and 2^52. /// </remarks> public double ConvertTo(TimeUnit targetUnit) { switch (targetUnit) { case TimeUnit.Seconds: return m_value; case TimeUnit.Minutes: return ToMinutes(); case TimeUnit.Hours: return ToHours(); case TimeUnit.Days: return ToDays(); case TimeUnit.Weeks: return ToWeeks(); case TimeUnit.Ticks: return ToTicks(); case TimeUnit.AtomicUnitsOfTime: return ToAtomicUnitsOfTime(); case TimeUnit.PlanckTime: return ToPlanckTime(); case TimeUnit.Ke: return ToKe(); default: throw new ArgumentOutOfRangeException(nameof(targetUnit), targetUnit, null); } } /// <summary> /// Converts the <see cref="Time"/> value into a textual representation of years, days, hours, /// minutes and seconds. /// </summary> /// <remarks> /// 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 <see cref="ToString(int, double)"/> overload instead. /// </remarks> /// <returns> /// The string representation of the value of this instance, consisting of the number of /// years, days, hours, minutes and seconds represented by this value. /// </returns> public override string ToString() { return ToElapsedTimeString(m_value, 0); } /// <summary> /// Converts the <see cref="Time"/> value into a textual representation of years, days, hours, /// minutes and seconds with the specified number of fractional digits. /// </summary> /// <param name="secondPrecision">Number of fractional digits to display for seconds.</param> /// <param name="minimumSubSecondResolution"> /// Minimum sub-second resolution to display. Defaults to <see cref="SI.Milli"/>. /// </param> /// <remarks>Set second precision to -1 to suppress seconds display.</remarks> /// <returns> /// The string representation of the value of this instance, consisting of the number of /// years, days, hours, minutes and seconds represented by this value. /// </returns> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="minimumSubSecondResolution"/> is not less than or equal to <see cref="SI.Milli"/> or /// <paramref name="minimumSubSecondResolution"/> is not defined in <see cref="SI.Factors"/> array. /// </exception> public string ToString(int secondPrecision, double minimumSubSecondResolution = SI.Milli) { return ToElapsedTimeString(m_value, secondPrecision, null, minimumSubSecondResolution); } /// <summary> /// Converts the <see cref="Time"/> 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. /// </summary> /// <param name="secondPrecision">Number of fractional digits to display for seconds.</param> /// <param name="timeNames">Time names array to use during textual conversion.</param> /// <param name="minimumSubSecondResolution"> /// Minimum sub-second resolution to display. Defaults to <see cref="SI.Milli"/>. /// </param> /// <remarks> /// <para>Set second precision to -1 to suppress seconds display.</para> /// <para> /// <paramref name="timeNames"/> array needs one string entry for each of the following names:<br/> /// " year", " years", " day", " days", " hour", " hours", " minute", " minutes", " second", " seconds", "less than ". /// </para> /// </remarks> /// <returns> /// The string representation of the value of this instance, consisting of the number of /// years, days, hours, minutes and seconds represented by this value. /// </returns> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="minimumSubSecondResolution"/> is not less than or equal to <see cref="SI.Milli"/> or /// <paramref name="minimumSubSecondResolution"/> is not defined in <see cref="SI.Factors"/> array. /// </exception> public string ToString(int secondPrecision, string[] timeNames, double minimumSubSecondResolution = SI.Milli) { return ToElapsedTimeString(m_value, secondPrecision, timeNames, minimumSubSecondResolution); } #region [ Numeric Interface Implementations ] /// <summary> /// Compares this instance to a specified object and returns an indication of their relative values. /// </summary> /// <param name="value">An object to compare, or null.</param> /// <returns> /// 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. /// </returns> /// <exception cref="ArgumentException">value is not a <see cref="Double"/> or <see cref="Time"/>.</exception> public int CompareTo(object value) { if (value == null) return 1; if (!(value is double) && !(value is Time) && !(value is DateTime) && !(value is TimeSpan)) throw new ArgumentException("Argument must be a Double or a Time"); double num = (Time)value; return m_value < num ? -1 : (m_value > num ? 1 : 0); } /// <summary> /// Compares this instance to a specified <see cref="Time"/> and returns an indication of their /// relative values. /// </summary> /// <param name="value">A <see cref="Time"/> to compare.</param> /// <returns> /// 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. /// </returns> public int CompareTo(Time value) { return CompareTo((double)value); } /// <summary> /// Compares this instance to a specified <see cref="TimeSpan"/> and returns an indication of their /// relative values. /// </summary> /// <param name="value">A <see cref="TimeSpan"/> to compare.</param> /// <returns> /// 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. /// </returns> public int CompareTo(TimeSpan value) { return CompareTo(value.TotalSeconds); } /// <summary> /// Compares this instance to a specified <see cref="Double"/> and returns an indication of their /// relative values. /// </summary> /// <param name="value">An <see cref="Double"/> to compare.</param> /// <returns> /// 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. /// </returns> public int CompareTo(double value) { return m_value < value ? -1 : (m_value > value ? 1 : 0); } /// <summary> /// Returns a value indicating whether this instance is equal to a specified object. /// </summary> /// <param name="obj">An object to compare, or null.</param> /// <returns>Lines 1–500 of 1443. Load more
//****************************************************************************************************** // Time.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: // ---------------------------------------------------------------------------------------------------- // 01/25/2008 - J. Ritchie Carroll // Initial version of source generated. // 09/11/2008 - J. Ritchie Carroll // Converted to C#. // 08/10/2009 - Josh L. Patterson // Edited Comments. // 09/14/2009 - Stephen C. Wills // Added new header and license agreement. // 12/14/2012 - Starlynn Danyelle Gilliam // Modified Header. // 10/03/2017 - J. Ritchie Carroll // Added units enumeration with associated Convert method. // //****************************************************************************************************** #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 System; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Runtime.CompilerServices; using System.Text; namespace GSF.Units { #region [ Enumerations ] /// <summary> /// Represents the units available for a <see cref="Time"/> value. /// </summary> public enum TimeUnit { /// <summary> /// Second time units. /// </summary> Seconds, /// <summary> /// Minute time units. /// </summary> Minutes, /// <summary> /// Hour time units. /// </summary> Hours, /// <summary> /// Day time units. /// </summary> Days, /// <summary> /// Week time units. /// </summary> Weeks, /// <summary> /// Tick time units, 100-nanosecond intervals since 1/1/0001. /// </summary> Ticks, /// <summary> /// Atomic time units. /// </summary> AtomicUnitsOfTime, /// <summary> /// Planck time units. /// </summary> PlanckTime, /// <summary> /// Ke time units, traditional Chinese unit of decimal time. /// </summary> Ke } #endregion /// <summary> /// Represents a time measurement, in seconds, as a double-precision floating-point number. /// </summary> /// <remarks> /// This class behaves just like a <see cref="double"/> representing a time in seconds; it is implicitly /// castable to and from a <see cref="double"/> and therefore can be generally used "as" a double, but it /// has the advantage of handling conversions to and from other time representations, specifically /// minutes, hours, days, weeks, atomic units of time, Planck time and ke. Metric conversions are handled /// simply by applying the needed <see cref="SI"/> conversion factor, for example: /// <example> /// Convert time in nanoseconds to seconds: /// <code> /// public Time GetSeconds(double nanoseconds) /// { /// return nanoseconds * SI.Nano; /// } /// </code> /// Convert time in seconds to milliseconds: /// <code> /// public double GetMilliseconds(Time seconds) /// { /// return time / SI.Milli; /// } /// </code> /// This example converts minutes to hours: /// <code> /// public double GetHours(double minutes) /// { /// return Time.FromMinutes(minutes).ToHours(); /// } /// </code> /// </example> /// <para> /// Note that the <see cref="Time.ToString()"/> method will convert the <see cref="Time"/> value, in seconds, /// into a textual representation of years, days, hours, minutes and seconds using the static function /// <see cref="ToElapsedTimeString"/>. /// </para> /// </remarks> // ReSharper disable RedundantNameQualifier [Serializable] public struct Time : IComparable, IFormattable, IConvertible, IComparable<Time>, IComparable<TimeSpan>, IComparable<double>, IEquatable<Time>, IEquatable<TimeSpan>, IEquatable<double> { #region [ Members ] // Nested Types // Indices into TimeNames array 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 LessThan = 10; } // Constants private const double AtomicUnitsOfTimeFactor = 2.418884254e-17D; private const double PlanckTimeFactor = 1.351211818e-43D; private const double KeFactor = 864.0D; /// <summary> /// Fractional number of seconds in one tick. /// </summary> public const double SecondsPerTick = 1.0D / Ticks.PerSecond; /// <summary> /// Number of seconds in one minute. /// </summary> public const int SecondsPerMinute = 60; /// <summary> /// Number of seconds in one hour. /// </summary> public const int SecondsPerHour = 60 * SecondsPerMinute; /// <summary> /// Number of seconds in one day. /// </summary> public const int SecondsPerDay = 24 * SecondsPerHour; /// <summary> /// Number of seconds in one week. /// </summary> public const int SecondsPerWeek = 7 * SecondsPerDay; // Fields private readonly double m_value; // Time value stored in seconds #endregion #region [ Constructors ] /// <summary> /// Creates a new <see cref="Time"/>. /// </summary> /// <param name="value">New time value in seconds.</param> public Time(double value) { m_value = value; } /// <summary> /// Creates a new <see cref="Time"/>. /// </summary> /// <param name="value">New time value as a <see cref="TimeSpan"/>.</param> public Time(TimeSpan value) { m_value = value.TotalSeconds; } #endregion #region [ Methods ] /// <summary> /// Gets the <see cref="Time"/> value in atomic units of time. /// </summary> /// <returns>Value of <see cref="Time"/> in atomic units of time.</returns> public double ToAtomicUnitsOfTime() { return m_value / AtomicUnitsOfTimeFactor; } /// <summary> /// Gets the <see cref="Time"/> value in Planck time. /// </summary> /// <returns>Value of <see cref="Time"/> in Planck time.</returns> public double ToPlanckTime() { return m_value / PlanckTimeFactor; } /// <summary> /// Gets the <see cref="Time"/> value in ke, the traditional Chinese unit of decimal time. /// </summary> /// <returns>Value of <see cref="Time"/> in ke.</returns> public double ToKe() { return m_value / KeFactor; } /// <summary> /// Gets the <see cref="Time"/> value in minutes. /// </summary> /// <returns>Value of <see cref="Time"/> in minutes.</returns> public double ToMinutes() { return m_value / SecondsPerMinute; } /// <summary> /// Gets the <see cref="Time"/> value in hours. /// </summary> /// <returns>Value of <see cref="Time"/> in hours.</returns> public double ToHours() { return m_value / SecondsPerHour; } /// <summary> /// Gets the <see cref="Time"/> value in days. /// </summary> /// <returns>Value of <see cref="Time"/> in days.</returns> public double ToDays() { return m_value / SecondsPerDay; } /// <summary> /// Gets the <see cref="Time"/> value in weeks. /// </summary> /// <returns>Value of <see cref="Time"/> in weeks.</returns> public double ToWeeks() { return m_value / SecondsPerWeek; } /// <summary> /// Converts the <see cref="Time"/> value, in seconds, to 100-nanosecond tick intervals. /// </summary> /// <returns>A <see cref="Ticks"/> object.</returns> public Ticks ToTicks() { return (Ticks)(m_value * Ticks.PerSecond); } /// <summary> /// Converts the <see cref="Time"/> to the specified <paramref name="targetUnit"/>. /// </summary> /// <param name="targetUnit">Target units.</param> /// <returns><see cref="Time"/> converted to <paramref name="targetUnit"/>.</returns> /// <remarks> /// When converting to <see cref="TimeUnit.Ticks"/>, precision loss will /// occur for values that are outside the range of -2^52 and 2^52. /// </remarks> public double ConvertTo(TimeUnit targetUnit) { switch (targetUnit) { case TimeUnit.Seconds: return m_value; case TimeUnit.Minutes: return ToMinutes(); case TimeUnit.Hours: return ToHours(); case TimeUnit.Days: return ToDays(); case TimeUnit.Weeks: return ToWeeks(); case TimeUnit.Ticks: return ToTicks(); case TimeUnit.AtomicUnitsOfTime: return ToAtomicUnitsOfTime(); case TimeUnit.PlanckTime: return ToPlanckTime(); case TimeUnit.Ke: return ToKe(); default: throw new ArgumentOutOfRangeException(nameof(targetUnit), targetUnit, null); } } /// <summary> /// Converts the <see cref="Time"/> value into a textual representation of years, days, hours, /// minutes and seconds. /// </summary> /// <remarks> /// 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 <see cref="ToString(int, double)"/> overload instead. /// </remarks> /// <returns> /// The string representation of the value of this instance, consisting of the number of /// years, days, hours, minutes and seconds represented by this value. /// </returns> public override string ToString() { return ToElapsedTimeString(m_value, 0); } /// <summary> /// Converts the <see cref="Time"/> value into a textual representation of years, days, hours, /// minutes and seconds with the specified number of fractional digits. /// </summary> /// <param name="secondPrecision">Number of fractional digits to display for seconds.</param> /// <param name="minimumSubSecondResolution"> /// Minimum sub-second resolution to display. Defaults to <see cref="SI.Milli"/>. /// </param> /// <remarks>Set second precision to -1 to suppress seconds display.</remarks> /// <returns> /// The string representation of the value of this instance, consisting of the number of /// years, days, hours, minutes and seconds represented by this value. /// </returns> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="minimumSubSecondResolution"/> is not less than or equal to <see cref="SI.Milli"/> or /// <paramref name="minimumSubSecondResolution"/> is not defined in <see cref="SI.Factors"/> array. /// </exception> public string ToString(int secondPrecision, double minimumSubSecondResolution = SI.Milli) { return ToElapsedTimeString(m_value, secondPrecision, null, minimumSubSecondResolution); } /// <summary> /// Converts the <see cref="Time"/> 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. /// </summary> /// <param name="secondPrecision">Number of fractional digits to display for seconds.</param> /// <param name="timeNames">Time names array to use during textual conversion.</param> /// <param name="minimumSubSecondResolution"> /// Minimum sub-second resolution to display. Defaults to <see cref="SI.Milli"/>. /// </param> /// <remarks> /// <para>Set second precision to -1 to suppress seconds display.</para> /// <para> /// <paramref name="timeNames"/> array needs one string entry for each of the following names:<br/> /// " year", " years", " day", " days", " hour", " hours", " minute", " minutes", " second", " seconds", "less than ". /// </para> /// </remarks> /// <returns> /// The string representation of the value of this instance, consisting of the number of /// years, days, hours, minutes and seconds represented by this value. /// </returns> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="minimumSubSecondResolution"/> is not less than or equal to <see cref="SI.Milli"/> or /// <paramref name="minimumSubSecondResolution"/> is not defined in <see cref="SI.Factors"/> array. /// </exception> public string ToString(int secondPrecision, string[] timeNames, double minimumSubSecondResolution = SI.Milli) { return ToElapsedTimeString(m_value, secondPrecision, timeNames, minimumSubSecondResolution); } #region [ Numeric Interface Implementations ] /// <summary> /// Compares this instance to a specified object and returns an indication of their relative values. /// </summary> /// <param name="value">An object to compare, or null.</param> /// <returns> /// 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. /// </returns> /// <exception cref="ArgumentException">value is not a <see cref="Double"/> or <see cref="Time"/>.</exception> public int CompareTo(object value) { if (value == null) return 1; if (!(value is double) && !(value is Time) && !(value is DateTime) && !(value is TimeSpan)) throw new ArgumentException("Argument must be a Double or a Time"); double num = (Time)value; return m_value < num ? -1 : (m_value > num ? 1 : 0); } /// <summary> /// Compares this instance to a specified <see cref="Time"/> and returns an indication of their /// relative values. /// </summary> /// <param name="value">A <see cref="Time"/> to compare.</param> /// <returns> /// 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. /// </returns> public int CompareTo(Time value) { return CompareTo((double)value); } /// <summary> /// Compares this instance to a specified <see cref="TimeSpan"/> and returns an indication of their /// relative values. /// </summary> /// <param name="value">A <see cref="TimeSpan"/> to compare.</param> /// <returns> /// 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. /// </returns> public int CompareTo(TimeSpan value) { return CompareTo(value.TotalSeconds); } /// <summary> /// Compares this instance to a specified <see cref="Double"/> and returns an indication of their /// relative values. /// </summary> /// <param name="value">An <see cref="Double"/> to compare.</param> /// <returns> /// 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. /// </returns> public int CompareTo(double value) { return m_value < value ? -1 : (m_value > value ? 1 : 0); } /// <summary> /// Returns a value indicating whether this instance is equal to a specified object. /// </summary> /// <param name="obj">An object to compare, or null.</param> /// <returns>