ParseHttpDate(
String DateString,
out DateTime dtOut
) {
int index = 0;
int i = 0, iLastLettered = -1;
bool fIsANSIDateFormat = false;
int [] rgdwDateParseResults = new int[MAX_FIELD_DATE_ENTRIES];
bool fRet = true;
char [] lpInputBuffer = DateString.ToCharArray();
dtOut = new DateTime();
//
// Date Parsing v2 (1 more to go), and here is how it works...
// We take a date string and churn through it once, converting
// integers to integers, Month,Day, and GMT strings into integers,
// and all is then placed IN order in a temp array.
//
// At the completetion of the parse stage, we simple look at
// the data, and then map the results into the correct
// places in the SYSTIME structure. Simple, No allocations, and
// No dirting the data.
//
// The end of the function does something munging and pretting
// up of the results to handle the year 2000, and TZ offsets
// Note: do we need to fully handle TZs anymore?
//
while (index < DateString.Length && i < MAX_FIELD_DATE_ENTRIES) {
if (lpInputBuffer[index] >= '0' && lpInputBuffer[index] <= '9') {
//
// we have a numerical entry, scan through it and convent to DWORD
//
rgdwDateParseResults[i] = 0;
do {
rgdwDateParseResults[i] *= BASE_DEC;
rgdwDateParseResults[i] += (lpInputBuffer[index] - '0');
index++;
} while (index < DateString.Length &&
lpInputBuffer[index] >= '0' &&
lpInputBuffer[index] <= '9');
i++; // next token
}
else if ((lpInputBuffer[index] >= 'A' && lpInputBuffer[index] <= 'Z') ||
(lpInputBuffer[index] >= 'a' && lpInputBuffer[index] <= 'z')) {
//
// we have a string, should be a day, month, or GMT
// lets skim to the end of the string
//
rgdwDateParseResults[i] =
MapDayMonthToDword(lpInputBuffer, index);
iLastLettered = i;
// We want to ignore the possibility of a time zone such as PST or EST in a non-standard
// date format such as "Thu Dec 17 16:01:28 PST 1998" (Notice that the year is _after_ the time zone
if ((rgdwDateParseResults[i] == DATE_TOKEN_ERROR)
&&
!(fIsANSIDateFormat && (i==DATE_ANSI_INDEX_YEAR))) {
fRet = false;
goto quit;
}
//
// At this point if we have a vaild string
// at this index, we know for sure that we're
// looking at a ANSI type DATE format.
//
if (i == DATE_ANSI_INDEX_MONTH) {
fIsANSIDateFormat = true;
}
//
// Read past the end of the current set of alpha characters,
// as MapDayMonthToDword only peeks at a few characters
//
do {
index++;
} while (index < DateString.Length &&
( (lpInputBuffer[index] >= 'A' && lpInputBuffer[index] <= 'Z') ||
(lpInputBuffer[index] >= 'a' && lpInputBuffer[index] <= 'z') ));
i++; // next token
}
else {
//
// For the generic case its either a space, comma, semi-colon, etc.
// the point is we really don't care, nor do we need to waste time
// worring about it (the orginal code did). The point is we
// care about the actual date information, So we just advance to the
// next lexume.
//
index++;
}
}
//
// We're finished parsing the string, now take the parsed tokens
// and turn them to the actual structured information we care about.
// So we build lpSysTime from the Array, using a local if none is passed in.
//
int year;
int month;
int day;
int hour;
int minute;
int second;
int millisecond;
millisecond = 0;
if (fIsANSIDateFormat) {
day = rgdwDateParseResults[DATE_ANSI_INDEX_DAY];
month = rgdwDateParseResults[DATE_ANSI_INDEX_MONTH];
hour = rgdwDateParseResults[DATE_ANSI_INDEX_HRS];
minute = rgdwDateParseResults[DATE_ANSI_INDEX_MINS];
second = rgdwDateParseResults[DATE_ANSI_INDEX_SECS];
if (iLastLettered != DATE_ANSI_INDEX_YEAR) {
year = rgdwDateParseResults[DATE_ANSI_INDEX_YEAR];
}
else {
// This is a fix to get around toString/toGMTstring (where the timezone is
// appended at the end. (See above)
year = rgdwDateParseResults[DATE_INDEX_TZ];
}
}
else {
day = rgdwDateParseResults[DATE_1123_INDEX_DAY];
month = rgdwDateParseResults[DATE_1123_INDEX_MONTH];
year = rgdwDateParseResults[DATE_1123_INDEX_YEAR];
hour = rgdwDateParseResults[DATE_1123_INDEX_HRS];
minute = rgdwDateParseResults[DATE_1123_INDEX_MINS];
second = rgdwDateParseResults[DATE_1123_INDEX_SECS];
}
if (year < 100) {
year += ((year < 80) ? 2000 : 1900);
}
//
// if we got misformed time, then plug in the current time
// !lpszHrs || !lpszMins || !lpszSec
//
if ((i < 4)
|| (day > 31)
|| (hour > 23)
|| (minute > 59)
|| (second > 59)) {
fRet = false;
goto quit;
}
//
// Now do the DateTime conversion
//
dtOut = new DateTime (year, month, day, hour, minute, second, millisecond);
//
// we want the system time to be accurate. This is _suhlow_
// The time passed in is in the local time zone; we have to convert this into GMT.
//
if (iLastLettered==DATE_ANSI_INDEX_YEAR) {
// this should be an unusual case.
dtOut = dtOut.ToUniversalTime();
}
//
// If we have an Offset to another Time Zone
// then convert to appropriate GMT time
//
if ((i > DATE_INDEX_TZ &&
rgdwDateParseResults[DATE_INDEX_TZ] != DATE_TOKEN_GMT)) {
//
// if we received +/-nnnn as offset (hhmm), modify the output FILETIME
//
double offset;
offset = (double) rgdwDateParseResults[DATE_INDEX_TZ];
dtOut.AddHours(offset);
}
// In the end, we leave it all in LocalTime
dtOut = dtOut.ToLocalTime();
quit:
return fRet;
}