/*=================================ParseTimeZone========================== **Action: Parse the timezone offset in the following format: ** "+8", "+08", "+0800", "+0800" ** This method is used by DateTime.Parse(). **Returns: The TimeZone offset. **Arguments: ** str the parsing string **Exceptions: ** FormatException if invalid timezone format is found. ============================================================================*/ private static bool ParseTimeZone(ref __DTString str, ref TimeSpan result) { // The hour/minute offset for timezone. int hourOffset = 0; int minuteOffset = 0; DTSubString sub; // Consume the +/- character that has already been read sub = str.GetSubString(); if (sub.length != 1) { return false; } char offsetChar = sub[0]; if (offsetChar != '+' && offsetChar != '-') { return false; } str.ConsumeSubString(sub); sub = str.GetSubString(); if (sub.type != DTSubStringType.Number) { return false; } int value = sub.value; int length = sub.length; if (length == 1 || length == 2) { // Parsing "+8" or "+08" hourOffset = value; str.ConsumeSubString(sub); // See if we have minutes sub = str.GetSubString(); if (sub.length == 1 && sub[0] == ':') { // Parsing "+8:00" or "+08:00" str.ConsumeSubString(sub); sub = str.GetSubString(); if (sub.type != DTSubStringType.Number || sub.length < 1 || sub.length > 2) { return false; } minuteOffset = sub.value; str.ConsumeSubString(sub); } } else if (length == 3 || length == 4) { // Parsing "+800" or "+0800" hourOffset = value / 100; minuteOffset = value % 100; str.ConsumeSubString(sub); } else { // Wrong number of digits return false; } Contract.Assert(hourOffset >= 0 && hourOffset <= 99, "hourOffset >= 0 && hourOffset <= 99"); Contract.Assert(minuteOffset >= 0 && minuteOffset <= 99, "minuteOffset >= 0 && minuteOffset <= 99"); if (minuteOffset < 0 || minuteOffset >= 60) { return false; } result = new TimeSpan(hourOffset, minuteOffset, 0); if (offsetChar == '-') { result = result.Negate(); } return true; }
private static bool ParseTimeZone(ref __DTString str, ref TimeSpan result) { int hours = 0; int num = 0; DTSubString subString = str.GetSubString(); if (subString.length != 1) { return false; } char c = subString[0]; if (c != '+' && c != '-') { return false; } str.ConsumeSubString(subString); subString = str.GetSubString(); if (subString.type != DTSubStringType.Number) { return false; } int value = subString.value; int length = subString.length; if (length == 1 || length == 2) { hours = value; str.ConsumeSubString(subString); subString = str.GetSubString(); if (subString.length == 1 && subString[0] == ':') { str.ConsumeSubString(subString); subString = str.GetSubString(); if (subString.type != DTSubStringType.Number || subString.length < 1 || subString.length > 2) { return false; } num = subString.value; str.ConsumeSubString(subString); } } else { if (length != 3 && length != 4) { return false; } hours = value / 100; num = value % 100; str.ConsumeSubString(subString); } if (num < 0 || num >= 60) { return false; } result = new TimeSpan(hours, num, 0); if (c == '-') { result = result.Negate(); } return true; }
private static bool ParseTimeZone(ref __DTString str, ref TimeSpan result) { int hours = 0; int minutes = 0; DTSubString subString = str.GetSubString(); if (subString.length != 1) { return false; } char ch = subString[0]; if ((ch != '+') && (ch != '-')) { return false; } str.ConsumeSubString(subString); subString = str.GetSubString(); if (subString.type != DTSubStringType.Number) { return false; } int num3 = subString.value; int length = subString.length; switch (length) { case 1: case 2: hours = num3; str.ConsumeSubString(subString); subString = str.GetSubString(); if ((subString.length == 1) && (subString[0] == ':')) { str.ConsumeSubString(subString); subString = str.GetSubString(); if (((subString.type != DTSubStringType.Number) || (subString.length < 1)) || (subString.length > 2)) { return false; } minutes = subString.value; str.ConsumeSubString(subString); } break; default: if ((length != 3) && (length != 4)) { return false; } hours = num3 / 100; minutes = num3 % 100; str.ConsumeSubString(subString); break; } if ((minutes < 0) || (minutes >= 60)) { return false; } result = new TimeSpan(hours, minutes, 0); if (ch == '-') { result = result.Negate(); } return true; }