date_object.cpp

来自「konqueror3 embedded版本, KDE环境下的当家浏览器的嵌入式版」· C++ 代码 · 共 1,198 行 · 第 1/3 页

CPP
1,198 行
字号
        (void)localtime(&zero);#  if defined(__BORLANDC__) || defined(__CYGWIN__)        utcOffset = - _timezone;#  else        utcOffset = - timezone;#  endif        t->tm_isdst = 0;#endif    } else {        utcOffset = 0;        t->tm_isdst = -1;    }    double yearOffset = 0.0;    if (t->tm_year < (1970 - 1900) || t->tm_year > (2038 - 1900)) {      // we'll fool mktime() into believing that this year is within      // its normal, portable range (1970-2038) by setting tm_year to      // 2000 or 2001 and adding the difference in milliseconds later.      // choice between offset will depend on whether the year is a      // leap year or not.      int y = t->tm_year + 1900;      int baseYear = daysInYear(y) == 365 ? 2001 : 2000;      const double baseTime = timeFromYear(baseYear);      yearOffset = timeFromYear(y) - baseTime;      t->tm_year = baseYear - 1900;    }    // Determine if we passed over a DST change boundary    if (!utc) {      time_t tval = mktime(t) + utcOffset + int((ms + yearOffset)/1000);      struct tm t3;      localtime_r(&tval, &t3);      t->tm_isdst = t3.tm_isdst;    }    return (mktime(t) + utcOffset) * 1000.0 + ms + yearOffset;}// returns 0-11 (Jan-Dec); -1 on failurestatic int findMonth(const char *monthStr){  assert(monthStr);  static const char haystack[37] = "janfebmaraprmayjunjulaugsepoctnovdec";  char needle[4];  for (int i = 0; i < 3; ++i) {    if (!*monthStr)      return -1;    needle[i] = tolower(*monthStr++);  }  needle[3] = '\0';  const char *str = strstr(haystack, needle);  if (str) {    int position = str - haystack;    if (position % 3 == 0) {      return position / 3;    }  }  return -1;}double KJS::KRFCDate_parseDate(const UString &_date){     // This parse a date in the form:     //     Wednesday, 09-Nov-99 23:12:40 GMT     // or     //     Sat, 01-Jan-2000 08:00:00 GMT     // or     //     Sat, 01 Jan 2000 08:00:00 GMT     // or     //     01 Jan 99 22:00 +0100    (exceptions in rfc822/rfc2822)     // ### non RFC formats, added for Javascript:     //     [Wednesday] January 09 1999 23:12:40 GMT     //     [Wednesday] January 09 23:12:40 GMT 1999     //     // We ignore the weekday     //     double result = -1;     int offset = 0;     bool have_tz = false;     char *newPosStr;     const char *dateString = _date.ascii();     int day = 0;     int month = -1; // not set yet     int year = 0;     int hour = 0;     int minute = 0;     int second = 0;     bool have_time = false;     // Skip leading space     while(*dateString && isspace(*dateString))     	dateString++;     const char *wordStart = dateString;     // Check contents of first words if not number     while(*dateString && !isdigit(*dateString))     {        if ( isspace(*dateString) && dateString - wordStart >= 3 )        {          month = findMonth(wordStart);          while(*dateString && isspace(*dateString))             dateString++;          wordStart = dateString;        }        else           dateString++;     }     // missing delimiter between month and day (like "January29")?     if (month == -1 && dateString && wordStart != dateString) {       month = findMonth(wordStart);       // TODO: emit warning about dubious format found     }     while(*dateString && isspace(*dateString))     	dateString++;     if (!*dateString)     	return invalidDate;     // ' 09-Nov-99 23:12:40 GMT'     errno = 0;     day = strtol(dateString, &newPosStr, 10);     if (errno)       return invalidDate;     dateString = newPosStr;     if (!*dateString)     	return invalidDate;     if (day < 1)       return invalidDate;     if (day > 31) {       // ### where is the boundary and what happens below?       if (*dateString == '/' && day >= 1000) {         // looks like a YYYY/MM/DD date         if (!*++dateString)           return invalidDate;         year = day;         month = strtol(dateString, &newPosStr, 10) - 1;         if (errno)           return invalidDate;         dateString = newPosStr;         if (*dateString++ != '/' || !*dateString)           return invalidDate;         day = strtol(dateString, &newPosStr, 10);         if (errno)           return invalidDate;         dateString = newPosStr;       } else {         return invalidDate;       }     } else if (*dateString == '/' && day <= 12 && month == -1)     {     	dateString++;        // This looks like a MM/DD/YYYY date, not an RFC date.....        month = day - 1; // 0-based        day = strtol(dateString, &newPosStr, 10);        if (errno)          return invalidDate;        dateString = newPosStr;        if (*dateString == '/')          dateString++;        if (!*dateString)          return invalidDate;        //printf("month=%d day=%d dateString=%s\n", month, day, dateString);     }     else     {       if (*dateString == '-')         dateString++;       while(*dateString && isspace(*dateString))         dateString++;       if (*dateString == ',')         dateString++;       if ( month == -1 ) // not found yet       {         month = findMonth(dateString);         if (month == -1)           return invalidDate;         while(*dateString && (*dateString != '-') && !isspace(*dateString))           dateString++;         if (!*dateString)           return invalidDate;         // '-99 23:12:40 GMT'         if ((*dateString != '-') && (*dateString != '/') && !isspace(*dateString))           return invalidDate;         dateString++;       }       if ((month < 0) || (month > 11))         return invalidDate;     }     // '99 23:12:40 GMT'     if (year <= 0 && *dateString) {       year = strtol(dateString, &newPosStr, 10);       if (errno)         return invalidDate;     }     // Don't fail if the time is missing.     if (*newPosStr)     {        // ' 23:12:40 GMT'        if (!isspace(*newPosStr)) {           if ( *newPosStr == ':' ) // Ah, so there was no year, but the number was the hour               year = -1;           else               return invalidDate;        } else // in the normal case (we parsed the year), advance to the next number            dateString = ++newPosStr;        hour = strtol(dateString, &newPosStr, 10);        // Do not check for errno here since we want to continue        // even if errno was set becasue we are still looking        // for the timezone!        // read a number? if not this might be a timezone name        if (newPosStr != dateString) {          have_time = true;          dateString = newPosStr;          if ((hour < 0) || (hour > 23))            return invalidDate;          if (!*dateString)            return invalidDate;          // ':12:40 GMT'          if (*dateString++ != ':')            return invalidDate;          minute = strtol(dateString, &newPosStr, 10);          if (errno)            return invalidDate;          dateString = newPosStr;          if ((minute < 0) || (minute > 59))            return invalidDate;          // ':40 GMT'          if (*dateString && *dateString != ':' && !isspace(*dateString))            return invalidDate;          // seconds are optional in rfc822 + rfc2822          if (*dateString ==':') {            dateString++;            second = strtol(dateString, &newPosStr, 10);            if (errno)              return invalidDate;            dateString = newPosStr;            if ((second < 0) || (second > 59))              return invalidDate;          }          while(*dateString && isspace(*dateString))            dateString++;	  if (strncasecmp(dateString, "AM", 2) == 0) {	    if (hour > 12)	      return invalidDate;	    if (hour == 12)	      hour = 0;	    dateString += 2;	    while (isspace(*dateString))	      dateString++;	  } else if (strncasecmp(dateString, "PM", 2) == 0) {	    if (hour > 12)	      return invalidDate;	    if (hour != 12)	      hour += 12;	    dateString += 2;	    while (isspace(*dateString))	      dateString++;	  }        }     } else {       dateString = newPosStr;     }     // don't fail if the time zone is missing, some     // broken mail-/news-clients omit the time zone     if (*dateString) {       if (strncasecmp(dateString, "GMT", 3) == 0 ||	   strncasecmp(dateString, "UTC", 3) == 0)       {         dateString += 3;         have_tz = true;       }       while (*dateString && isspace(*dateString))         ++dateString;       if (strncasecmp(dateString, "GMT", 3) == 0) {         dateString += 3;       }       if ((*dateString == '+') || (*dateString == '-')) {         offset = strtol(dateString, &newPosStr, 10);         if (errno)           return invalidDate;         dateString = newPosStr;         if ((offset < -9959) || (offset > 9959))            return invalidDate;         int sgn = (offset < 0)? -1:1;         offset = abs(offset);         if ( *dateString == ':' ) { // GMT+05:00           int offset2 = strtol(dateString, &newPosStr, 10);           if (errno)             return invalidDate;           dateString = newPosStr;           offset = (offset*60 + offset2)*sgn;         }         else           offset = ((offset / 100)*60 + (offset % 100))*sgn;         have_tz = true;       } else {         for (int i=0; i < int(sizeof(known_zones)/sizeof(KnownZone)); i++) {           if (0 == strncasecmp(dateString, known_zones[i].tzName, strlen(known_zones[i].tzName))) {             offset = known_zones[i].tzOffset;             dateString += strlen(known_zones[i].tzName);             have_tz = true;             break;           }         }       }     }     while(*dateString && isspace(*dateString))        dateString++;     if ( *dateString && year == -1 ) {       year = strtol(dateString, &newPosStr, 10);       if (errno)         return invalidDate;       dateString = newPosStr;     }     while (isspace(*dateString))       dateString++;#if 0     // Trailing garbage     if (*dateString != '\0')       return invalidDate;#endif     // Y2K: Solve 2 digit years     if ((year >= 0) && (year < 50))         year += 2000;     if ((year >= 50) && (year < 100))         year += 1900;  // Y2K     if (!have_tz) {       // fall back to midnight, local timezone       struct tm t;       memset(&t, 0, sizeof(tm));       t.tm_mday = day;       t.tm_mon = month;       t.tm_year = year - 1900;       t.tm_isdst = -1;       if (have_time) {         t.tm_sec = second;         t.tm_min = minute;         t.tm_hour = hour;       }       // better not use mktime() as it can't handle the full year range       return makeTime(&t, 0, false) / 1000.0;     }     result = ymdhms_to_seconds(year, month+1, day, hour, minute, second) - offset*60;     return result;}double KJS::timeClip(double t){  if (isInf(t))    return NaN;  double at = fabs(t);  if (at > 8.64E15)    return NaN;  return floor(at) * (t != at ? -1 : 1);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?