namespace System
{
[Serializable]
+#if NET_2_0
+ [System.Runtime.InteropServices.ComVisible (true)]
+#endif
public struct TimeSpan : IComparable
+#if NET_2_0
+ , IComparable<TimeSpan>, IEquatable <TimeSpan>
+#endif
{
public static readonly TimeSpan MaxValue = new TimeSpan (long.MaxValue);
public static readonly TimeSpan MinValue = new TimeSpan (long.MinValue);
internal static long CalculateTicks (int days, int hours, int minutes, int seconds, int milliseconds)
{
- bool overflow = false;
-
- // this part cannot overflow Int64
- long t = checked (TimeSpan.TicksPerHour * hours +
- TimeSpan.TicksPerMinute * minutes +
- TimeSpan.TicksPerSecond * seconds +
- TimeSpan.TicksPerMillisecond * milliseconds);
+ // there's no overflow checks for hours, minutes, ...
+ // so big hours/minutes values can overflow at some point and change expected values
+ int hrssec = (hours * 3600); // break point at (Int32.MaxValue - 596523)
+ int minsec = (minutes * 60);
+ long t = ((long)(hrssec + minsec + seconds) * 1000L + (long)milliseconds);
+ t *= 10000;
+ bool overflow = false;
// days is problematic because it can overflow but that overflow can be
// "legal" (i.e. temporary) (e.g. if other parameters are negative) or
// illegal (e.g. sign change).
return Compare (this, (TimeSpan) value);
}
+#if NET_2_0
+ public int CompareTo (TimeSpan value)
+ {
+ return Compare (this, value);
+ }
+
+ public bool Equals (TimeSpan value)
+ {
+ return value._ticks == _ticks;
+ }
+#endif
+
public TimeSpan Duration ()
{
try {
return p.Execute ();
}
+#if NET_2_0
+ public static bool TryParse (string s, out TimeSpan result)
+ {
+ try {
+ result = Parse (s);
+ return true;
+ } catch {
+ result = TimeSpan.Zero;
+ return false;
+ }
+ }
+#endif
+
public TimeSpan Subtract (TimeSpan ts)
{
try {
sb.Append ('.');
}
- sb.Append (IntegerFormatter.FormatDecimal (Math.Abs (Hours), 2, 4));
+ System.Globalization.NumberFormatInfo nfi = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
+ sb.Append (NumberFormatter.FormatDecimal (new NumberFormatter.NumberStore ((int)Math.Abs (Hours)), 2, nfi));
sb.Append (':');
- sb.Append (IntegerFormatter.FormatDecimal (Math.Abs (Minutes), 2, 4));
+ sb.Append (NumberFormatter.FormatDecimal (new NumberFormatter.NumberStore ((int)Math.Abs (Minutes)), 2, nfi));
sb.Append (':');
- sb.Append (IntegerFormatter.FormatDecimal (Math.Abs (Seconds), 2, 4));
+ sb.Append (NumberFormatter.FormatDecimal (new NumberFormatter.NumberStore ((int)Math.Abs (Seconds)), 2, nfi));
int fractional = (int) Math.Abs (_ticks % TicksPerSecond);
if (fractional != 0) {
sb.Append ('.');
- sb.Append (IntegerFormatter.FormatDecimal (Math.Abs (fractional), 7, 4));
+ sb.Append (NumberFormatter.FormatDecimal (new NumberFormatter.NumberStore (fractional), 7, nfi));
}
return sb.ToString ();