25 #include <sys/param.h>
29 #include <QtCore/QMutableStringListIterator>
30 #include <QtCore/QCharRef>
31 #include <QtCore/QByteArray>
33 static unsigned int ymdhms_to_seconds(
int year,
int mon,
int day,
int hour,
int minute,
int second)
35 if (
sizeof(time_t) == 4)
63 unsigned int ret = (day - 32075)
64 + 1461L * (year + 4800L + (mon - 14) / 12) / 4
65 + 367 * (mon - 2 - (mon - 14) / 12 * 12) / 12
66 - 3 * ((year + 4900L + (mon - 14) / 12) / 100) / 4
69 ret = 60*ret + minute;
70 ret = 60*ret + second;
75 static const char haystack[37]=
"janfebmaraprmayjunjulaugsepoctnovdec";
117 const char *dateString = dateArray.
data();
127 while(*dateString &&
isspace(*dateString))
131 while(*dateString && !
isdigit(*dateString) && !
isspace(*dateString))
135 while(*dateString &&
isspace(*dateString))
145 while(*dateString &&
isspace(*dateString))
148 for(
int i=0; i < 3;i++)
150 if (!*dateString || (*dateString ==
'-') ||
isspace(*dateString))
152 monthStr[i] = tolower(*dateString++);
156 newPosStr = (
char*)strstr(
haystack, monthStr);
163 if ((month < 0) || (month > 11))
166 while (*dateString &&
isalpha(*dateString))
172 day = strtol(dateString, &newPosStr, 10);
173 dateString = newPosStr;
175 if ((day < 1) || (day > 31))
181 while(*dateString && (
isspace(*dateString) || (*dateString ==
'-')))
186 for(
int i=0; i < 3;i++)
188 if (!*dateString || (*dateString ==
'-') ||
isspace(*dateString))
190 monthStr[i] = tolower(*dateString++);
194 newPosStr = (
char*)strstr(
haystack, monthStr);
201 if ((month < 0) || (month > 11))
204 while (*dateString &&
isalpha(*dateString))
210 while(*dateString && (
isspace(*dateString) || (*dateString ==
'-')))
213 if (!*dateString || !
isdigit(*dateString))
217 year = strtol(dateString, &newPosStr, 10);
218 dateString = newPosStr;
221 if ((year >= 0) && (year < 50))
224 if ((year >= 50) && (year < 100))
227 if ((year < 1900) || (year > 2500))
237 hour = strtol(dateString, &newPosStr, 10);
238 dateString = newPosStr;
240 if ((hour < 0) || (hour > 23))
247 if (*dateString++ !=
':')
250 minute = strtol(dateString, &newPosStr, 10);
251 dateString = newPosStr;
253 if ((minute < 0) || (minute > 59))
260 if (*dateString !=
':' && !
isspace(*dateString))
264 if (*dateString ==
':') {
267 second = strtol(dateString, &newPosStr, 10);
268 dateString = newPosStr;
270 if ((second < 0) || (second > 59))
276 while(*dateString &&
isspace(*dateString))
283 if ((strncasecmp(dateString,
"gmt", 3) == 0) ||
284 (strncasecmp(dateString,
"utc", 3) == 0))
287 while(*dateString &&
isspace(*dateString))
291 if ((*dateString ==
'+') || (*dateString ==
'-')) {
292 offset = strtol(dateString, &newPosStr, 10);
293 if (abs(offset) < 30)
295 dateString = newPosStr;
297 offset = offset * 100;
299 if (*dateString && *(dateString+1))
302 int minutes = strtol(dateString, &newPosStr, 10);
310 if ((offset < -9959) || (offset > 9959))
313 int sgn = (offset < 0)? -1:1;
314 offset = abs(offset);
315 offset = ((offset / 100)*60 + (offset % 100))*sgn;
329 if ((offset > 0) && (offset > result))
337 if (result < 1) result = 1;
354 unsigned int year = 0;
355 unsigned int month = 0;
356 unsigned int mday = 0;
357 unsigned int hour = 0;
358 unsigned int min = 0;
359 unsigned int sec = 0;
371 const int dashes = input.
count(
'-');
374 }
else if (1 == dashes) {
378 input +=
"T12:00:00";
391 year = l[0].toUInt();
392 month = l[1].toUInt();
393 mday = l[2].toUInt();
396 if (
'Z' == timeString.
at(timeString.
length() - 1)) {
405 QString offsetString = timeString.
mid(plusPos + 1);
409 timeString = timeString.
left(plusPos);
413 if (-1 != minusPos) {
414 QString offsetString = timeString.
mid(minusPos + 1);
418 timeString = timeString.
left(minusPos);
426 timeString = timeString.
left(dotPos);
431 l = timeString.
split(
':');
435 hour = l[0].toUInt();
442 if ((offset > 0) && (offset > result))
450 if (result < 1) result = 1;
458 time_t timeNow = time((time_t*) 0);
460 tm *tM = gmtime(&timeNow);
461 unsigned int timeUTC =
ymdhms_to_seconds(tM->tm_year+1900, tM->tm_mon+1, tM->tm_mday,
462 tM->tm_hour, tM->tm_min, tM->tm_sec);
464 tM = localtime(&timeNow);
465 unsigned int timeLocal =
ymdhms_to_seconds(tM->tm_year+1900, tM->tm_mon+1, tM->tm_mday,
466 tM->tm_hour, tM->tm_min, tM->tm_sec);
468 return ((
int)(timeLocal-timeUTC))/60;
473 "Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
477 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
478 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
484 utcTime += utcOffset * 60;
485 tm *tM = gmtime(&utcTime);
486 char sgn = (utcOffset < 0) ?
'-' :
'+';
487 int z = (utcOffset < 0) ? -utcOffset : utcOffset;
490 dateStr =
QString().
sprintf(
"%s, %02d %s %04d %02d:%02d:%02d %c%02d%02d",
493 tM->tm_hour, tM->tm_min, tM->tm_sec,
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
static time_t parseDate(const QString &date)
This function tries to parse a string containing a date/time in any of the formats specified by RFC82...
QString & remove(int position, int n)
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
static const char haystack[37]
static const char day_names[][4]
static const char month_names[][4]
QString right(int n) const
QByteArray toLatin1() const
QString mid(int position, int n) const
QString & sprintf(const char *cformat,...)
const QChar at(int position) const
static time_t parseDateISO8601(const QString &date)
This function tries to parse a string containing a date/time in any of the formats specified by http:...
QString left(int n) const
static const struct @1 known_zones[]
static int localUTCOffset()
Returns the local timezone offset to UTC in minutes.
static unsigned int ymdhms_to_seconds(int year, int mon, int day, int hour, int minute, int second)
static QByteArray rfc2822DateString(time_t utcTime, int utcOffset=localUTCOffset())
Returns a string representation of the given date and time formated in conformance to RFC2822...
uint toUInt(bool *ok, int base) const