From 81f916f750685fcfb2c0b1a91bdfa83b8fb4d28d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Fri, 15 Jun 2018 15:13:10 +0100 Subject: [PATCH] Work on TimeSpan (#14) --- source/System/DateTime.cs | 6 +- source/System/TimeSpan.cs | 258 +++++++++++++++++--------------------- 2 files changed, 121 insertions(+), 143 deletions(-) diff --git a/source/System/DateTime.cs b/source/System/DateTime.cs index c97640ee..3c17d279 100644 --- a/source/System/DateTime.cs +++ b/source/System/DateTime.cs @@ -513,7 +513,7 @@ public TimeSpan Subtract(DateTime val) /// An object that is equal to the date and time represented by this instance minus the time interval represented by val. public DateTime Subtract(TimeSpan val) { - return new DateTime((long)(_ticks - (ulong)val._numberOfTicks)); + return new DateTime((long)(_ticks - (ulong)val._ticks)); } /// @@ -545,7 +545,7 @@ public String ToString(String format) /// public static DateTime operator +(DateTime d, TimeSpan t) { - return new DateTime((long)(d._ticks + (ulong)t._numberOfTicks)); + return new DateTime((long)(d._ticks + (ulong)t._ticks)); } @@ -559,7 +559,7 @@ public String ToString(String format) /// public static DateTime operator -(DateTime d, TimeSpan t) { - return new DateTime((long)(d._ticks - (ulong)t._numberOfTicks)); + return new DateTime((long)(d._ticks - (ulong)t._ticks)); } /// diff --git a/source/System/TimeSpan.cs b/source/System/TimeSpan.cs index 31df6ef5..270b121e 100644 --- a/source/System/TimeSpan.cs +++ b/source/System/TimeSpan.cs @@ -10,57 +10,73 @@ namespace System /// /// Represents a time interval. /// + /// + /// A object represents a time interval (duration of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. The structure can also be used to represent the time of day, but only if the time is unrelated to a particular date. Otherwise, the structure should be used instead. + /// The value of a object is the number of ticks that equal the represented time interval. A tick is equal to 100 nanoseconds, or one ten-millionth of a second. The value of a object can range from to . + /// [Serializable] public struct TimeSpan { - internal long _numberOfTicks; + internal long _ticks; + + internal const long MaxMilliSeconds = Int64.MaxValue / TicksPerMillisecond; + internal const long MinMilliSeconds = Int64.MinValue / TicksPerMillisecond; /// /// Represents the number of ticks in 1 millisecond. This field is constant. /// public const long TicksPerMillisecond = 10000; + private const double MillisecondsPerTick = 1.0 / TicksPerMillisecond; + /// /// Represents the number of ticks in 1 second. /// - public const long TicksPerSecond = TicksPerMillisecond * 1000; + public const long TicksPerSecond = TicksPerMillisecond * 1000; // 10.000.000 + private const double SecondsPerTick = 1.0 / TicksPerSecond; // 0.0001 + /// /// Represents the number of ticks in 1 minute. This field is constant. /// - public const long TicksPerMinute = TicksPerSecond * 60; + public const long TicksPerMinute = TicksPerSecond * 60; // 600.000.000 + private const double MinutesPerTick = 1.0 / TicksPerMinute; // 1.6666666666667e-9 + /// /// Represents the number of ticks in 1 hour. This field is constant. /// - public const long TicksPerHour = TicksPerMinute * 60; + public const long TicksPerHour = TicksPerMinute * 60; // 36.000.000.000 + private const double HoursPerTick = 1.0 / TicksPerHour; // 2.77777777777777778e-11 + /// /// Represents the number of ticks in 1 day. This field is constant. /// - public const long TicksPerDay = TicksPerHour * 24; + public const long TicksPerDay = TicksPerHour * 24; // 864.000.000.000 + private const double DaysPerTick = 1.0 / TicksPerDay; // 1.1574074074074074074e-12 /// - /// Represents the zero TimeSpan value. This field is read-only. + /// Represents the zero value. This field is read-only. /// public static readonly TimeSpan Zero = new TimeSpan(0); /// - /// Represents the maximum TimeSpan value. This field is read-only. + /// Represents the maximum value. This field is read-only. /// public static readonly TimeSpan MaxValue = new TimeSpan(Int64.MaxValue); /// - /// Represents the minimum TimeSpan value. This field is read-only. + /// Represents the minimum value. This field is read-only. /// public static readonly TimeSpan MinValue = new TimeSpan(Int64.MinValue); /// - /// Initializes a new instance of the TimeSpan structure to the specified number of ticks. + /// Initializes a new instance of the structure to the specified number of ticks. /// /// A time period expressed in 100-nanosecond units. public TimeSpan(long ticks) { - _numberOfTicks = ticks; + _ticks = ticks; } /// - /// Initializes a new instance of the TimeSpan structure to a specified number of hours, minutes, and seconds. + /// Initializes a new instance of the structure to a specified number of hours, minutes, and seconds. /// /// Number of hours. /// Number of minutes. @@ -69,7 +85,7 @@ public TimeSpan(long ticks) public extern TimeSpan(int hours, int minutes, int seconds); /// - /// Initializes a new instance of the TimeSpan structure to a specified number of days, hours, minutes, and seconds. + /// Initializes a new instance of the structure to a specified number of days, hours, minutes, and seconds. /// /// Number of days. /// Number of hours. @@ -79,7 +95,7 @@ public TimeSpan(long ticks) public extern TimeSpan(int days, int hours, int minutes, int seconds); /// - /// Initializes a new instance of the TimeSpan structure to a specified number of days, hours, minutes, seconds and milliseconds. + /// Initializes a new instance of the structure to a specified number of days, hours, minutes, seconds and milliseconds. /// /// Number of days. /// Number of hours. @@ -90,89 +106,93 @@ public TimeSpan(long ticks) public extern TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds); /// - /// Gets the number of ticks that represent the value of the current TimeSpan structure. + /// Gets the number of ticks that represent the value of the current structure. /// /// The number of ticks contained in this instance. - public long Ticks - { - get - { - return _numberOfTicks; - } - } + public long Ticks => _ticks; /// - /// Gets the days component of the time interval represented by the current TimeSpan structure. + /// Gets the days component of the time interval represented by the current structure. /// /// The day component of this instance. The return value can be positive or negative. - public int Days - { - get - { - return (int)(_numberOfTicks / TicksPerDay); - } - } + public int Days => (int)(_ticks / TicksPerDay); /// - /// Gets the hours component of the time interval represented by the current TimeSpan structure. + /// Gets the hours component of the time interval represented by the current structure. /// - /// The hour component of this instance. The return value can be positive or negative. - public int Hours - { - get - { - return (int)((_numberOfTicks / TicksPerHour) % 24); - } - } + /// The hour component of this instance. The return value ranges from -23 through 23. + public int Hours => (int)((_ticks / TicksPerHour) % 24); /// - /// Gets the milliseconds component of the time interval represented by the current TimeSpan structure. + /// Gets the milliseconds component of the time interval represented by the current structure. /// - /// The millisecond component of this instance. The return value can be positive or negative. - public int Milliseconds - { - get - { - return (int)((_numberOfTicks / TicksPerMillisecond) % 1000); - } - } + /// The millisecond component of this instance. The return value ranges from -999 through 999. + public int Milliseconds => (int)((_ticks / TicksPerMillisecond) % 1000); /// - /// Gets the minutes component of the time interval represented by the current TimeSpan structure. + /// Gets the minutes component of the time interval represented by the current structure. /// - /// The minute component of this instance. The return value can be positive or negative. - public int Minutes - { - get - { - return (int)((_numberOfTicks / TicksPerMinute) % 60); - } - } + /// The minute component of this instance. The return value ranges from -59 through 59. + public int Minutes => (int)((_ticks / TicksPerMinute) % 60); + + /// + /// Gets the seconds component of the time interval represented by the current structure. + /// + /// The second component of this instance. The return value ranges from -59 through 59. + public int Seconds => (int)((_ticks / TicksPerSecond) % 60); + + /// + /// Gets the value of the current structure expressed in whole and fractional days. + /// + /// The total number of days represented by this instance. + public double TotalDays => (_ticks) * DaysPerTick; + + /// + /// Gets the value of the current structure expressed in whole and fractional hours. + /// + /// The total number of hours represented by this instance. + public double TotalHours => _ticks * HoursPerTick; /// - /// Gets the seconds component of the time interval represented by the current TimeSpan structure. + /// Gets the value of the current structure expressed in whole and fractional milliseconds. /// - /// The second component of this instance. The return value can be positive or negative. - public int Seconds + /// The total number of milliseconds represented by this instance. + public double TotalMilliseconds { get { - return (int)((_numberOfTicks / TicksPerSecond) % 60); + double temp = _ticks * MillisecondsPerTick; + if (temp > MaxMilliSeconds) + return MaxMilliSeconds; + + if (temp < MinMilliSeconds) + return MinMilliSeconds; + + return temp; } } /// - /// Returns a new TimeSpan object whose value is the sum of the specified TimeSpan object and this instance. + /// Gets the value of the current structure expressed in whole and fractional minutes. + /// + /// The total number of minutes represented by this instance. + public double TotalMinutes => _ticks * MinutesPerTick; + + /// + /// Gets the value of the current structure expressed in whole and fractional seconds. + /// + /// The total number of seconds represented by this instance. + public double TotalSeconds => _ticks * SecondsPerTick; + + /// + /// Returns a new object whose value is the sum of the specified object and this instance. /// /// The time interval to add. /// A new object that represents the value of this instance plus the value of ts. - public TimeSpan Add(TimeSpan ts) - { - return new TimeSpan(_numberOfTicks + ts._numberOfTicks); - } + public TimeSpan Add(TimeSpan ts) => new TimeSpan(_ticks + ts._ticks); /// - /// Compares two TimeSpan values and returns an integer that indicates whether the first value is shorter than, equal to, or longer than the second value. + /// Compares two values and returns an integer that indicates whether the first value is shorter than, equal to, or longer than the second value. /// /// The first time interval to compare. /// The second time interval to compare. @@ -197,24 +217,21 @@ public TimeSpan Add(TimeSpan ts) public extern int CompareTo(Object value); /// - /// Returns a new TimeSpan object whose value is the absolute value of the current TimeSpan object. + /// Returns a new object whose value is the absolute value of the current object. /// - /// A new object whose value is the absolute value of the current TimeSpan object. - public TimeSpan Duration() - { - return new TimeSpan(_numberOfTicks >= 0 ? _numberOfTicks : -_numberOfTicks); - } + /// A new object whose value is the absolute value of the current object. + public TimeSpan Duration() => new TimeSpan(_ticks >= 0 ? _ticks : -_ticks); /// /// Returns a value indicating whether this instance is equal to a specified object. /// /// An object to compare with this instance. - /// true if value is a TimeSpan object that represents the same time interval as the current TimeSpan structure; otherwise, false. + /// true if value is a object that represents the same time interval as the current structure; otherwise, false. [MethodImpl(MethodImplOptions.InternalCall)] public override extern bool Equals(Object value); /// - /// Returns a value that indicates whether two specified instances of TimeSpan are equal. + /// Returns a value that indicates whether two specified instances of are equal. /// /// The first time interval to compare. /// The second time interval to compare. @@ -223,149 +240,110 @@ public TimeSpan Duration() public static extern bool Equals(TimeSpan t1, TimeSpan t2); /// - /// Returns a new TimeSpan object whose value is the negated value of this instance. + /// Returns a new object whose value is the negated value of this instance. /// /// A new object with the same numeric value as this instance, but with the opposite sign. - public TimeSpan Negate() - { - return new TimeSpan(-_numberOfTicks); - } + public TimeSpan Negate() => new TimeSpan(-_ticks); /// - /// Returns a new TimeSpan object whose value is the difference between the specified TimeSpan object and this instance. + /// Returns a new object whose value is the difference between the specified object and this instance. /// /// The time interval to be subtracted. /// A new time interval whose value is the result of the value of this instance minus the value of ts. - public TimeSpan Subtract(TimeSpan ts) - { - return new TimeSpan(_numberOfTicks - ts._numberOfTicks); - } + public TimeSpan Subtract(TimeSpan ts) => new TimeSpan(_ticks - ts._ticks); /// - /// Returns a TimeSpan that represents a specified time, where the specification is in units of ticks. + /// Returns a that represents a specified time, where the specification is in units of ticks. /// /// A number of ticks that represent a time. /// An object that represents value. - public static TimeSpan FromTicks(long value) - { - return new TimeSpan(value); - } + public static TimeSpan FromTicks(long value) => new TimeSpan(value); /// - /// Converts the value of the current TimeSpan object to its equivalent string representation. + /// Converts the value of the current object to its equivalent string representation. /// - /// The string representation of the current TimeSpan value. + /// The string representation of the current value. /// The returned string is formatted with the "c" format specifier and has the following format: [-][d.]hh:mm:ss[.fffffff] [MethodImpl(MethodImplOptions.InternalCall)] public override extern String ToString(); /// - /// Returns a TimeSpan whose value is the negated value of the specified instance. + /// Returns a whose value is the negated value of the specified instance. /// /// The time interval to be negated. /// An object that has the same numeric value as this instance, but the opposite sign. - public static TimeSpan operator -(TimeSpan t) - { - return new TimeSpan(-t._numberOfTicks); - } + public static TimeSpan operator -(TimeSpan t) => new TimeSpan(-t._ticks); /// - /// Subtracts a specified TimeSpan from another specified TimeSpan. + /// Subtracts a specified from another specified TimeSpan. /// /// The minuend. /// The subtrahend. /// An object whose value is the result of the value of t1 minus the value of t2. - public static TimeSpan operator -(TimeSpan t1, TimeSpan t2) - { - return new TimeSpan(t1._numberOfTicks - t2._numberOfTicks); - } + public static TimeSpan operator -(TimeSpan t1, TimeSpan t2) => new TimeSpan(t1._ticks - t2._ticks); /// /// Returns the specified instance of TimeSpan. /// /// The time interval to return. /// The time interval specified by t. - public static TimeSpan operator +(TimeSpan t) - { - return t; - } + public static TimeSpan operator +(TimeSpan t) => t; /// - /// Adds two specified TimeSpan instances. + /// Adds two specified instances. /// /// The first time interval to add. /// The second time interval to add. /// An object whose value is the sum of the values of t1 and t2. - public static TimeSpan operator +(TimeSpan t1, TimeSpan t2) - { - return new TimeSpan(t1._numberOfTicks + t2._numberOfTicks); - } + public static TimeSpan operator +(TimeSpan t1, TimeSpan t2) => new TimeSpan(t1._ticks + t2._ticks); /// - /// Indicates whether two TimeSpan instances are equal + /// Indicates whether two instances are equal /// /// The first time interval to compare. /// The second time interval to compare. /// true if the values of t1 and t2 are equal; otherwise, false. - public static bool operator ==(TimeSpan t1, TimeSpan t2) - { - return t1._numberOfTicks == t2._numberOfTicks; - } + public static bool operator ==(TimeSpan t1, TimeSpan t2) => t1._ticks == t2._ticks; /// - /// Indicates whether two TimeSpan instances are not equal. + /// Indicates whether two instances are not equal. /// /// The first time interval to compare. /// The second time interval to compare. /// true if the values of t1 and t2 are not equal; otherwise, false. - public static bool operator !=(TimeSpan t1, TimeSpan t2) - { - return t1._numberOfTicks != t2._numberOfTicks; - } + public static bool operator !=(TimeSpan t1, TimeSpan t2) => t1._ticks != t2._ticks; /// - /// Indicates whether a specified TimeSpan is less than another specified TimeSpan. + /// Indicates whether a specified is less than another specified TimeSpan. /// /// The first time interval to compare. /// The second time interval to compare. /// true if the value of t1 is less than the value of t2; otherwise, false. - public static bool operator <(TimeSpan t1, TimeSpan t2) - { - return t1._numberOfTicks < t2._numberOfTicks; - } + public static bool operator <(TimeSpan t1, TimeSpan t2) => t1._ticks < t2._ticks; /// - /// Indicates whether a specified TimeSpan is less than or equal to another specified TimeSpan. + /// Indicates whether a specified is less than or equal to another specified TimeSpan. /// /// The first time interval to compare. /// The second time interval to compare. /// true if the value of t1 is less than or equal to the value of t2; otherwise, false. - public static bool operator <=(TimeSpan t1, TimeSpan t2) - { - return t1._numberOfTicks <= t2._numberOfTicks; - } + public static bool operator <=(TimeSpan t1, TimeSpan t2) => t1._ticks <= t2._ticks; /// - /// Indicates whether a specified TimeSpan is greater than another specified TimeSpan. + /// Indicates whether a specified is greater than another specified TimeSpan. /// /// The first time interval to compare. /// The second time interval to compare. /// true if the value of t1 is greater than the value of t2; otherwise, false. - public static bool operator >(TimeSpan t1, TimeSpan t2) - { - return t1._numberOfTicks > t2._numberOfTicks; - } + public static bool operator >(TimeSpan t1, TimeSpan t2) => t1._ticks > t2._ticks; /// - /// Indicates whether a specified TimeSpan is greater than or equal to another specified TimeSpan. + /// Indicates whether a specified is greater than or equal to another specified TimeSpan. /// /// The first time interval to compare. /// The second time interval to compare. /// true if the value of t1 is greater than or equal to the value of t2; otherwise, false. - public static bool operator >=(TimeSpan t1, TimeSpan t2) - { - return t1._numberOfTicks >= t2._numberOfTicks; - } + public static bool operator >=(TimeSpan t1, TimeSpan t2) => t1._ticks >= t2._ticks; } }