26 #include <sys/param.h>
30 #include <tqstringlist.h>
34 static unsigned int ymdhms_to_seconds(
int year,
int mon,
int day,
int hour,
int minute,
int second)
36 if (
sizeof(time_t) == 4)
64 unsigned int ret = (day - 32075)
65 + 1461L * (year + 4800L + (mon - 14) / 12) / 4
66 + 367 * (mon - 2 - (mon - 14) / 12 * 12) / 12
67 - 3 * ((year + 4900L + (mon - 14) / 12) / 100) / 4
70 ret = 60*ret + minute;
71 ret = 60*ret + second;
76 static const char haystack[37]=
"janfebmaraprmayjunjulaugsepoctnovdec";
117 const char *dateString = _date.latin1();
127 while(*dateString && isspace(*dateString))
131 while(*dateString && !isdigit(*dateString) && !isspace(*dateString))
135 while(*dateString && isspace(*dateString))
141 if (isalpha(*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);
161 month = (newPosStr-haystack)/3;
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);
199 month = (newPosStr-haystack)/3;
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))
234 if (!isspace(*dateString++))
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;
317 for (
int i=0; known_zones[i].tzName != 0; i++) {
318 if (0 == strncasecmp(dateString, known_zones[i].tzName, strlen(known_zones[i].tzName))) {
319 offset = known_zones[i].tzOffset;
326 result = ymdhms_to_seconds(year, month+1, day, hour, minute, second);
329 if ((offset > 0) && (offset > result))
337 if (result < 1) result = 1;
345 if (input_.isEmpty())
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;
363 TQString input = input_;
366 int tPos = input.find(
'T');
371 const int dashes = input.contains(
'-');
374 }
else if (1 == dashes) {
377 tPos = input.length();
378 input +=
"T12:00:00";
383 TQString dateString = input.left(tPos).stripWhiteSpace();
385 TQString timeString = input.mid(tPos + 1).stripWhiteSpace();
387 TQStringList l = TQStringList::split(
'-', dateString);
392 year = l[0].toUInt();
393 month = l[1].toUInt();
394 mday = l[2].toUInt();
397 if ((TQChar)
'Z' == timeString.at(timeString.length() - 1)) {
398 timeString.remove(timeString.length() - 1, 1);
403 int plusPos = timeString.findRev(
'+');
406 TQString offsetString = timeString.mid(plusPos + 1);
408 offset = offsetString.left(2).toUInt() * 60 + offsetString.right(2).toUInt();
410 timeString = timeString.left(plusPos);
412 int minusPos = timeString.findRev(
'-');
414 if (-1 != minusPos) {
415 TQString offsetString = timeString.mid(minusPos + 1);
417 offset = - int(offsetString.left(2).toUInt() * 60 + offsetString.right(2).toUInt());
419 timeString = timeString.left(minusPos);
424 int dotPos = timeString.findRev(
'.');
427 timeString = timeString.left(dotPos);
432 l = TQStringList::split(
':', timeString);
437 hour = l[0].toUInt();
441 time_t result = ymdhms_to_seconds(year, month, mday, hour, min, sec);
444 if ((offset > 0) && (offset > result))
452 if (result < 1) result = 1;
460 time_t timeNow = time((time_t*) 0);
462 tm *tM = gmtime(&timeNow);
463 unsigned int timeUTC = ymdhms_to_seconds(tM->tm_year+1900, tM->tm_mon+1, tM->tm_mday,
464 tM->tm_hour, tM->tm_min, tM->tm_sec);
466 tM = localtime(&timeNow);
467 unsigned int timeLocal = ymdhms_to_seconds(tM->tm_year+1900, tM->tm_mon+1, tM->tm_mday,
468 tM->tm_hour, tM->tm_min, tM->tm_sec);
470 return ((
int)(timeLocal-timeUTC))/60;
474 static const char *
const day_names[] = {
475 "Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
478 static const char *
const month_names[] = {
479 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
480 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
486 utcTime += utcOffset * 60;
487 tm *tM = gmtime(&utcTime);
488 char sgn = (utcOffset < 0) ?
'-' :
'+';
489 int z = (utcOffset < 0) ? -utcOffset : utcOffset;
492 dateStr.sprintf(
"%s, %02d %s %04d %02d:%02d:%02d %c%02d%02d",
493 day_names[tM->tm_wday], tM->tm_mday,
494 month_names[tM->tm_mon], tM->tm_year+1900,
495 tM->tm_hour, tM->tm_min, tM->tm_sec,
static TQCString rfc2822DateString(time_t utcTime, int utcOffset)
Returns a string representation of the given date and time formated in conformance to RFC2822.
static int localUTCOffset()
Returns the local timezone offset to UTC in minutes.
static time_t parseDateISO8601(const TQString &date)
This function tries to parse a string containing a date/time in any of the formats specified by http:...
static time_t parseDate(const TQString &date)
This function tries to parse a string containing a date/time in any of the formats specified by RFC82...