// (C) 2004 Andreas Nahr
// Copyright (C) 2004 Novell (http://www.novell.com)
//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
using System.Text;
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 ();
private string _src;
private int _cur = 0;
private int _length;
+ private bool formatError;
public Parser (string src)
{
}
}
- private void ThrowFormatException ()
- {
- throw new FormatException (Locale.GetText ("Invalid format for TimeSpan.Parse."));
- }
-
// All "Parse" functions throw a FormatException on syntax error.
// Their return value is semantic value of the item parsed.
}
if (count == 0)
- ThrowFormatException ();
+ formatError = true;
return res;
}
if (_src[_cur] == ':')
_cur++;
else
- ThrowFormatException ();
+ formatError = true;
}
}
}
if (!digitseen)
- ThrowFormatException ();
+ formatError = true;
return res;
}
int seconds;
long ticks;
+ // documented as...
// Parse [ws][-][dd.]hh:mm:ss[.ff][ws]
+ // ... but not entirely true as an lonely
+ // integer will be parsed as a number of days
ParseWhiteSpace ();
sign = ParseSign ();
days = ParseInt (false);
ParseWhiteSpace ();
if (!AtEnd)
- ThrowFormatException ();
+ formatError = true;
+ // Overflow has presceance over FormatException
if (hours > 23 || minutes > 59 || seconds > 59) {
- throw new OverflowException (Locale.GetText (
- "Invalid time data."));
+ throw new OverflowException (
+ Locale.GetText ("Invalid time data."));
+ }
+ else if (formatError) {
+ throw new FormatException (
+ Locale.GetText ("Invalid format for TimeSpan.Parse."));
}
long t = TimeSpan.CalculateTicks (days, hours, minutes, seconds, 0);
- t += ticks;
- if (sign)
- t = -t;
+ t = checked ((sign) ? (-t - ticks) : (t + ticks));
return new TimeSpan (t);
}
}