예제 #1
0
 public TimeSpan Add(TimeSpan ts)
 {
     try
     {
         return(new TimeSpan(Int64.AddOvf(_ticks, ts.Ticks)));
     }
     catch (OverflowException)
     {
         throw new OverflowException(Locale.GetText("Resulting timespan is too big."));
     }
 }
예제 #2
0
            public TimeSpan Execute()
            {
                bool sign;
                int  days;
                int  hours = 0;
                int  minutes;
                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);
                if (ParseOptDot())
                {
                    hours = ParseInt(true);
                }
                else if (!AtEnd)
                {
                    hours = days;
                    days  = 0;
                }
                ParseOptColon();
                minutes = ParseInt(true);
                ParseOptColon();
                seconds = ParseInt(true);
                if (ParseOptDot())
                {
                    ticks = ParseTicks();
                }
                else
                {
                    ticks = 0;
                }
                ParseWhiteSpace();

                if (!AtEnd)
                {
                    formatError = true;
                }

                // Overflow has presceance over FormatException
                if (hours > 23 || minutes > 59 || seconds > 59)
                {
                    throw new OverflowException(
                              Locale.GetText("Invalid time data."));
                }
                else if (formatError)
                {
                    throw new FormatException(
                              Locale.GetText("Invalid format for TimeSpan.Parse."));
                }

                try
                {
                    long t = CalculateTicks(days, hours, minutes, seconds, 0);
                    return(new TimeSpan(sign ? Int64.SubOvf(-t, ticks) : Int64.AddOvf(t, ticks)));
                }
                catch (ArgumentOutOfRangeException)
                {
                    throw new OverflowException(
                              Locale.GetText("Invalid time data."));
                }
            }