tools/qdatetime.cpp

Switch to Source codePreprocessed file
LineSource CodeCoverage
1 -
2 -
3 -
4 -
5 -
6 -
7 -
8enum { -
9 SECS_PER_DAY = 86400, -
10 MSECS_PER_DAY = 86400000, -
11 SECS_PER_HOUR = 3600, -
12 MSECS_PER_HOUR = 3600000, -
13 SECS_PER_MIN = 60, -
14 MSECS_PER_MIN = 60000, -
15 JULIAN_DAY_FOR_EPOCH = 2440588 -
16}; -
17 -
18static inline QDate fixedDate(int y, int m, int d) -
19{ -
20 QDate result(y, m, 1); -
21 result.setDate(y, m, qMin(d, result.daysInMonth())); -
22 return result;
-
23} -
24 -
25static inline qint64 floordiv(qint64 a, qint64 b) -
26{ -
27 return (a - (a < 0 ? b-1 : 0)) / b;
-
28} -
29 -
30static inline qint64 floordiv(qint64 a, int b) -
31{ -
32 return (a - (a < 0 ? b-1 : 0)) / b;
-
33} -
34 -
35static inline int floordiv(int a, int b) -
36{ -
37 return (a - (a < 0 ? b-1 : 0)) / b;
-
38} -
39 -
40static inline qint64 julianDayFromDate(int year, int month, int day) -
41{ -
42 -
43 if (year < 0)
-
44 ++year;
-
45 -
46 -
47 -
48 -
49 -
50 -
51 int a = floordiv(14 - month, 12); -
52 qint64 y = (qint64)year + 4800 - a; -
53 int m = month + 12 * a - 3; -
54 return day + floordiv(153 * m + 2, 5) + 365 * y + floordiv(y, 4) - floordiv(y, 100) + floordiv(y, 400) - 32045;
-
55} -
56 -
57static void getDateFromJulianDay(qint64 julianDay, int *yearp, int *monthp, int *dayp) -
58{ -
59 -
60 -
61 -
62 -
63 -
64 qint64 a = julianDay + 32044; -
65 qint64 b = floordiv(4 * a + 3, 146097); -
66 int c = a - floordiv(146097 * b, 4); -
67 -
68 int d = floordiv(4 * c + 3, 1461); -
69 int e = c - floordiv(1461 * d, 4); -
70 int m = floordiv(5 * e + 2, 153); -
71 -
72 int day = e - floordiv(153 * m + 2, 5) + 1; -
73 int month = m + 3 - 12 * floordiv(m, 10); -
74 int year = 100 * b + d - 4800 + floordiv(m, 10); -
75 -
76 -
77 if (year <= 0)
-
78 --year ;
-
79 -
80 if (yearp)
-
81 *yearp = year;
-
82 if (monthp)
-
83 *monthp = month;
-
84 if (dayp)
-
85 *dayp = day;
-
86}
-
87 -
88 -
89static const char monthDays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; -
90 -
91 -
92static const char * const qt_shortMonthNames[] = { -
93 "Jan", "Feb", "Mar", "Apr", "May", "Jun", -
94 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; -
95 -
96 -
97static QString fmtDateTime(const QString& f, const QTime* dt = 0, const QDate* dd = 0); -
98QDate::QDate(int y, int m, int d) -
99{ -
100 setDate(y, m, d); -
101}
-
102int QDate::year() const -
103{ -
104 if (isNull())
-
105 return 0;
-
106 -
107 int y; -
108 getDateFromJulianDay(jd, &y, 0, 0); -
109 return y;
-
110} -
111int QDate::month() const -
112{ -
113 if (isNull())
-
114 return 0;
-
115 -
116 int m; -
117 getDateFromJulianDay(jd, 0, &m, 0); -
118 return m;
-
119} -
120int QDate::day() const -
121{ -
122 if (isNull())
-
123 return 0;
-
124 -
125 int d; -
126 getDateFromJulianDay(jd, 0, 0, &d); -
127 return d;
-
128} -
129int QDate::dayOfWeek() const -
130{ -
131 if (isNull())
-
132 return 0;
-
133 -
134 if (jd >= 0)
-
135 return (jd % 7) + 1;
-
136 else -
137 return ((jd + 1) % 7) + 7;
-
138} -
139int QDate::dayOfYear() const -
140{ -
141 if (isNull())
-
142 return 0;
-
143 -
144 return jd - julianDayFromDate(year(), 1, 1) + 1;
-
145} -
146int QDate::daysInMonth() const -
147{ -
148 if (isNull())
-
149 return 0;
-
150 -
151 int y, m; -
152 getDateFromJulianDay(jd, &y, &m, 0); -
153 if (m == 2 && isLeapYear(y))
-
154 return 29;
-
155 else -
156 return monthDays[m];
-
157} -
158int QDate::daysInYear() const -
159{ -
160 if (isNull())
-
161 return 0;
-
162 -
163 int y; -
164 getDateFromJulianDay(jd, &y, 0, 0); -
165 return isLeapYear(y) ? 366 : 365;
-
166} -
167int QDate::weekNumber(int *yearNumber) const -
168{ -
169 if (!isValid())
-
170 return 0;
-
171 -
172 int year = QDate::year(); -
173 int yday = dayOfYear() - 1; -
174 int wday = dayOfWeek(); -
175 if (wday == 7)
-
176 wday = 0;
-
177 int w; -
178 -
179 for (;;) { -
180 int len; -
181 int bot; -
182 int top; -
183 -
184 len = isLeapYear(year) ? 366 : 365;
-
185 -
186 -
187 -
188 -
189 bot = ((yday + 11 - wday) % 7) - 3; -
190 -
191 -
192 -
193 -
194 top = bot - (len % 7); -
195 if (top < -3)
-
196 top += 7;
-
197 top += len; -
198 if (yday >= top) {
-
199 ++year; -
200 w = 1; -
201 break;
-
202 } -
203 if (yday >= bot) {
-
204 w = 1 + ((yday - bot) / 7); -
205 break;
-
206 } -
207 --year; -
208 yday += isLeapYear(year) ? 366 : 365;
-
209 }
-
210 if (yearNumber != 0)
-
211 *yearNumber = year;
-
212 return w;
-
213} -
214QString QDate::shortMonthName(int month, QDate::MonthNameType type) -
215{ -
216 if (month < 1 || month > 12)
-
217 return QString();
-
218 -
219 switch (type) { -
220 case QDate::DateFormat: -
221 return QLocale::system().monthName(month, QLocale::ShortFormat);
-
222 case QDate::StandaloneFormat: -
223 return QLocale::system().standaloneMonthName(month, QLocale::ShortFormat);
-
224 default: -
225 break;
-
226 } -
227 return QString();
-
228} -
229QString QDate::longMonthName(int month, MonthNameType type) -
230{ -
231 if (month < 1 || month > 12)
-
232 return QString();
-
233 -
234 switch (type) { -
235 case QDate::DateFormat: -
236 return QLocale::system().monthName(month, QLocale::LongFormat);
-
237 case QDate::StandaloneFormat: -
238 return QLocale::system().standaloneMonthName(month, QLocale::LongFormat);
-
239 default: -
240 break;
-
241 } -
242 return QString();
-
243} -
244QString QDate::shortDayName(int weekday, MonthNameType type) -
245{ -
246 if (weekday < 1 || weekday > 7)
-
247 return QString();
-
248 -
249 switch (type) { -
250 case QDate::DateFormat: -
251 return QLocale::system().dayName(weekday, QLocale::ShortFormat);
-
252 case QDate::StandaloneFormat: -
253 return QLocale::system().standaloneDayName(weekday, QLocale::ShortFormat);
-
254 default: -
255 break;
-
256 } -
257 return QString();
-
258} -
259QString QDate::longDayName(int weekday, MonthNameType type) -
260{ -
261 if (weekday < 1 || weekday > 7)
-
262 return QString();
-
263 -
264 switch (type) { -
265 case QDate::DateFormat: -
266 return QLocale::system().dayName(weekday, QLocale::LongFormat);
-
267 case QDate::StandaloneFormat: -
268 return QLocale::system().standaloneDayName(weekday, QLocale::LongFormat);
-
269 default: -
270 break;
-
271 } -
272 return QLocale::system().dayName(weekday, QLocale::LongFormat);
-
273} -
274QString QDate::toString(Qt::DateFormat f) const -
275{ -
276 if (!isValid())
-
277 return QString();
-
278 int y, m, d; -
279 getDateFromJulianDay(jd, &y, &m, &d); -
280 switch (f) { -
281 case Qt::SystemLocaleDate: -
282 case Qt::SystemLocaleShortDate: -
283 case Qt::SystemLocaleLongDate: -
284 return QLocale::system().toString(*this, f == Qt::SystemLocaleLongDate ? QLocale::LongFormat -
285 : QLocale::ShortFormat);
-
286 case Qt::LocaleDate: -
287 case Qt::DefaultLocaleShortDate: -
288 case Qt::DefaultLocaleLongDate: -
289 return QLocale().toString(*this, f == Qt::DefaultLocaleLongDate ? QLocale::LongFormat -
290 : QLocale::ShortFormat);
-
291 default: -
292 -
293 case Qt::TextDate: -
294 { -
295 return QString::fromLatin1("%0 %1 %2 %3") -
296 .arg(shortDayName(dayOfWeek())) -
297 .arg(shortMonthName(m)) -
298 .arg(d) -
299 .arg(y);
-
300 } -
301 -
302 case Qt::ISODate: -
303 { -
304 if (year() < 0 || year() > 9999)
-
305 return QString();
-
306 QString year(QString::number(y).rightJustified(4, QLatin1Char('0'))); -
307 QString month(QString::number(m).rightJustified(2, QLatin1Char('0'))); -
308 QString day(QString::number(d).rightJustified(2, QLatin1Char('0'))); -
309 return year + QLatin1Char('-') + month + QLatin1Char('-') + day;
-
310 } -
311 } -
312}
-
313QString QDate::toString(const QString& format) const -
314{ -
315 if (year() > 9999)
-
316 return QString();
-
317 return fmtDateTime(format, 0, this);
-
318} -
319bool QDate::setDate(int year, int month, int day) -
320{ -
321 if (isValid(year, month, day))
-
322 jd = julianDayFromDate(year, month, day);
-
323 else -
324 jd = nullJd();
-
325 -
326 return isValid();
-
327} -
328void QDate::getDate(int *year, int *month, int *day) -
329{ -
330 if (isValid()) {
-
331 getDateFromJulianDay(jd, year, month, day); -
332 } else {
-
333 if (year)
-
334 *year = 0;
-
335 if (month)
-
336 *month = 0;
-
337 if (day)
-
338 *day = 0;
-
339 }
-
340} -
341QDate QDate::addDays(qint64 ndays) const -
342{ -
343 if (isNull())
-
344 return QDate();
-
345 -
346 -
347 -
348 return fromJulianDay(jd + ndays);
-
349} -
350QDate QDate::addMonths(int nmonths) const -
351{ -
352 if (!isValid())
-
353 return QDate();
-
354 if (!nmonths)
-
355 return *this;
-
356 -
357 int old_y, y, m, d; -
358 getDateFromJulianDay(jd, &y, &m, &d); -
359 old_y = y; -
360 -
361 bool increasing = nmonths > 0; -
362 -
363 while (nmonths != 0) {
-
364 if (nmonths < 0 && nmonths + 12 <= 0) {
-
365 y--; -
366 nmonths+=12; -
367 } else if (nmonths < 0) {
-
368 m+= nmonths; -
369 nmonths = 0; -
370 if (m <= 0) {
-
371 --y; -
372 m += 12; -
373 }
-
374 } else if (nmonths - 12 >= 0) {
-
375 y++; -
376 nmonths -= 12; -
377 } else if (m == 12) {
-
378 y++; -
379 m = 0; -
380 } else {
-
381 m += nmonths; -
382 nmonths = 0; -
383 if (m > 12) {
-
384 ++y; -
385 m -= 12; -
386 }
-
387 }
-
388 } -
389 -
390 -
391 if ((old_y > 0 && y <= 0) ||
-
392 (old_y < 0 && y >= 0))
-
393 -
394 y += increasing ? +1 : -1;
-
395 -
396 return fixedDate(y, m, d);
-
397} -
398QDate QDate::addYears(int nyears) const -
399{ -
400 if (!isValid())
-
401 return QDate();
-
402 -
403 int y, m, d; -
404 getDateFromJulianDay(jd, &y, &m, &d); -
405 -
406 int old_y = y; -
407 y += nyears; -
408 -
409 -
410 if ((old_y > 0 && y <= 0) ||
-
411 (old_y < 0 && y >= 0))
-
412 -
413 y += nyears > 0 ? +1 : -1;
-
414 -
415 return fixedDate(y, m, d);
-
416} -
417qint64 QDate::daysTo(const QDate &d) const -
418{ -
419 if (isNull() || d.isNull())
-
420 return 0;
-
421 -
422 -
423 return d.jd - jd;
-
424} -
425QDate QDate::fromString(const QString& s, Qt::DateFormat f) -
426{ -
427 if (s.isEmpty())
-
428 return QDate();
-
429 -
430 switch (f) { -
431 case Qt::ISODate: -
432 { -
433 int year(s.mid(0, 4).toInt()); -
434 int month(s.mid(5, 2).toInt()); -
435 int day(s.mid(8, 2).toInt()); -
436 if (year && month && day)
-
437 return QDate(year, month, day);
-
438 } -
439 break;
-
440 case Qt::SystemLocaleDate: -
441 case Qt::SystemLocaleShortDate: -
442 case Qt::SystemLocaleLongDate: -
443 return fromString(s, QLocale::system().dateFormat(f == Qt::SystemLocaleLongDate ? QLocale::LongFormat -
444 : QLocale::ShortFormat));
-
445 case Qt::LocaleDate: -
446 case Qt::DefaultLocaleShortDate: -
447 case Qt::DefaultLocaleLongDate: -
448 return fromString(s, QLocale().dateFormat(f == Qt::DefaultLocaleLongDate ? QLocale::LongFormat -
449 : QLocale::ShortFormat));
-
450 default: -
451 -
452 case Qt::TextDate: { -
453 QStringList parts = s.split(QLatin1Char(' '), QString::SkipEmptyParts); -
454 -
455 if (parts.count() != 4) {
-
456 return QDate();
-
457 } -
458 -
459 QString monthName = parts.at(1); -
460 int month = -1; -
461 -
462 for (int i = 0; i < 12; ++i) {
-
463 if (monthName == QLatin1String(qt_shortMonthNames[i])) {
-
464 month = i + 1; -
465 break;
-
466 } -
467 }
-
468 -
469 if (month == -1) {
-
470 for (int i = 1; i <= 12; ++i) {
-
471 if (monthName == QDate::shortMonthName(i)) {
-
472 month = i; -
473 break;
-
474 } -
475 }
-
476 if (month == -1) {
-
477 -
478 return QDate();
-
479 } -
480 }
-
481 -
482 bool ok; -
483 int day = parts.at(2).toInt(&ok); -
484 if (!ok) {
-
485 return QDate();
-
486 } -
487 -
488 int year = parts.at(3).toInt(&ok); -
489 if (!ok) {
-
490 return QDate();
-
491 } -
492 -
493 return QDate(year, month, day);
-
494 } -
495 -
496 -
497 -
498 } -
499 return QDate();
-
500} -
501QDate QDate::fromString(const QString &string, const QString &format) -
502{ -
503 QDate date; -
504 -
505 QDateTimeParser dt(QVariant::Date, QDateTimeParser::FromString); -
506 if (dt.parseFormat(format))
-
507 dt.fromString(string, &date, 0);
-
508 -
509 -
510 -
511 -
512 return date;
-
513} -
514bool QDate::isValid(int year, int month, int day) -
515{ -
516 -
517 if (year == 0)
-
518 return false;
-
519 -
520 return (day > 0 && month > 0 && month <= 12) && -
521 (day <= monthDays[month] || (day == 29 && month == 2 && isLeapYear(year)));
-
522} -
523bool QDate::isLeapYear(int y) -
524{ -
525 -
526 if ( y < 1)
-
527 ++y;
-
528 -
529 return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
-
530} -
531QTime::QTime(int h, int m, int s, int ms) -
532{ -
533 setHMS(h, m, s, ms); -
534}
-
535bool QTime::isValid() const -
536{ -
537 return mds > NullTime && mds < MSECS_PER_DAY;
-
538} -
539int QTime::hour() const -
540{ -
541 if (!isValid())
-
542 return -1;
-
543 -
544 return ds() / MSECS_PER_HOUR;
-
545} -
546int QTime::minute() const -
547{ -
548 if (!isValid())
-
549 return -1;
-
550 -
551 return (ds() % MSECS_PER_HOUR) / MSECS_PER_MIN;
-
552} -
553int QTime::second() const -
554{ -
555 if (!isValid())
-
556 return -1;
-
557 -
558 return (ds() / 1000)%SECS_PER_MIN;
-
559} -
560int QTime::msec() const -
561{ -
562 if (!isValid())
-
563 return -1;
-
564 -
565 return ds() % 1000;
-
566} -
567QString QTime::toString(Qt::DateFormat format) const -
568{ -
569 if (!isValid())
-
570 return QString();
-
571 -
572 switch (format) { -
573 case Qt::SystemLocaleDate: -
574 case Qt::SystemLocaleShortDate: -
575 case Qt::SystemLocaleLongDate: -
576 return QLocale::system().toString(*this, format == Qt::SystemLocaleLongDate ? QLocale::LongFormat -
577 : QLocale::ShortFormat);
-
578 case Qt::LocaleDate: -
579 case Qt::DefaultLocaleShortDate: -
580 case Qt::DefaultLocaleLongDate: -
581 return QLocale().toString(*this, format == Qt::DefaultLocaleLongDate ? QLocale::LongFormat -
582 : QLocale::ShortFormat);
-
583 -
584 default: -
585 case Qt::ISODate: -
586 case Qt::TextDate: -
587 return QString::fromLatin1("%1:%2:%3") -
588 .arg(hour(), 2, 10, QLatin1Char('0')) -
589 .arg(minute(), 2, 10, QLatin1Char('0')) -
590 .arg(second(), 2, 10, QLatin1Char('0'));
-
591 } -
592}
-
593QString QTime::toString(const QString& format) const -
594{ -
595 return fmtDateTime(format, this, 0);
-
596} -
597bool QTime::setHMS(int h, int m, int s, int ms) -
598{ -
599 -
600 -
601 -
602 if (!isValid(h,m,s,ms)) {
-
603 mds = NullTime; -
604 return false;
-
605 } -
606 mds = (h*SECS_PER_HOUR + m*SECS_PER_MIN + s)*1000 + ms; -
607 return true;
-
608} -
609QTime QTime::addSecs(int s) const -
610{ -
611 return addMSecs(s * 1000);
-
612} -
613int QTime::secsTo(const QTime &t) const -
614{ -
615 if (!isValid() || !t.isValid())
-
616 return 0;
-
617 -
618 -
619 int ourSeconds = ds() / 1000; -
620 int theirSeconds = t.ds() / 1000; -
621 return theirSeconds - ourSeconds;
-
622} -
623QTime QTime::addMSecs(int ms) const -
624{ -
625 QTime t; -
626 if (isValid()) {
-
627 if (ms < 0) {
-
628 -
629 int negdays = (MSECS_PER_DAY - ms) / MSECS_PER_DAY; -
630 t.mds = (ds() + ms + negdays * MSECS_PER_DAY) % MSECS_PER_DAY; -
631 } else {
-
632 t.mds = (ds() + ms) % MSECS_PER_DAY; -
633 }
-
634 } -
635 -
636 -
637 -
638 -
639 return t;
-
640} -
641int QTime::msecsTo(const QTime &t) const -
642{ -
643 if (!isValid() || !t.isValid())
-
644 return 0;
-
645 -
646 -
647 -
648 -
649 -
650 -
651 return t.ds() - ds();
-
652} -
653namespace { -
654inline bool isMidnight(int hour, int minute, int second, int msec) -
655{ -
656 return hour == 24 && minute == 0 && second == 0 && msec == 0;
-
657} -
658 -
659QTime fromStringImpl(const QString &s, Qt::DateFormat f, bool &isMidnight24) -
660{ -
661 if (s.isEmpty()) {
-
662 -
663 return QTime();
-
664 } -
665 -
666 switch (f) { -
667 case Qt::SystemLocaleDate: -
668 case Qt::SystemLocaleShortDate: -
669 case Qt::SystemLocaleLongDate: -
670 { -
671 QLocale::FormatType formatType(Qt::SystemLocaleLongDate ? QLocale::LongFormat : QLocale::ShortFormat); -
672 return QTime::fromString(s, QLocale::system().timeFormat(formatType));
-
673 } -
674 case Qt::LocaleDate: -
675 case Qt::DefaultLocaleShortDate: -
676 case Qt::DefaultLocaleLongDate: -
677 { -
678 QLocale::FormatType formatType(f == Qt::DefaultLocaleLongDate ? QLocale::LongFormat : QLocale::ShortFormat); -
679 return QTime::fromString(s, QLocale().timeFormat(formatType));
-
680 } -
681 case Qt::TextDate: -
682 case Qt::ISODate: -
683 { -
684 bool ok = true; -
685 const int hour(s.mid(0, 2).toInt(&ok)); -
686 if (!ok)
-
687 return QTime();
-
688 const int minute(s.mid(3, 2).toInt(&ok)); -
689 if (!ok)
-
690 return QTime();
-
691 if (f == Qt::ISODate) {
-
692 if (s.size() == 5) {
-
693 -
694 return QTime(hour, minute, 0, 0);
-
695 } else if ((s.size() > 6) && (s[5] == QLatin1Char(',') || s[5] == QLatin1Char('.'))) {
-
696 const QString minuteFractionStr(QLatin1String("0.") + s.mid(6, 5)); -
697 const float minuteFraction = minuteFractionStr.toFloat(&ok); -
698 if (!ok)
-
699 return QTime();
-
700 const float secondWithMs = minuteFraction * 60; -
701 const float second = std::floor(secondWithMs); -
702 const float millisecond = 1000 * (secondWithMs - second); -
703 const int millisecondRounded = qMin(qRound(millisecond), 999); -
704 -
705 if (isMidnight(hour, minute, second, millisecondRounded)) {
-
706 isMidnight24 = true; -
707 return QTime(0, 0, 0, 0);
-
708 } -
709 -
710 return QTime(hour, minute, second, millisecondRounded);
-
711 } -
712 } -
713 -
714 const int second(s.mid(6, 2).toInt(&ok)); -
715 if (!ok)
-
716 return QTime();
-
717 const QString msec_s(QLatin1String("0.") + s.mid(9, 4)); -
718 const double msec(msec_s.toDouble(&ok)); -
719 if (!ok)
-
720 return QTime(hour, minute, second, 0);
-
721 -
722 if (f == Qt::ISODate) {
-
723 if (isMidnight(hour, minute, second, msec)) {
-
724 isMidnight24 = true; -
725 return QTime(0, 0, 0, 0);
-
726 } -
727 }
-
728 return QTime(hour, minute, second, qMin(qRound(msec * 1000.0), 999));
-
729 } -
730 } -
731 do { qt_noop(); qt_noop(); } while (0);
-
732 return QTime();
-
733} -
734} -
735QTime QTime::fromString(const QString& s, Qt::DateFormat f) -
736{ -
737 bool unused; -
738 return fromStringImpl(s, f, unused);
-
739} -
740QTime QTime::fromString(const QString &string, const QString &format) -
741{ -
742 QTime time; -
743 -
744 QDateTimeParser dt(QVariant::Time, QDateTimeParser::FromString); -
745 if (dt.parseFormat(format))
-
746 dt.fromString(string, 0, &time);
-
747 -
748 -
749 -
750 -
751 return time;
-
752} -
753bool QTime::isValid(int h, int m, int s, int ms) -
754{ -
755 return (uint)h < 24 && (uint)m < 60 && (uint)s < 60 && (uint)ms < 1000;
-
756} -
757void QTime::start() -
758{ -
759 *this = currentTime(); -
760}
-
761int QTime::restart() -
762{ -
763 QTime t = currentTime(); -
764 int n = msecsTo(t); -
765 if (n < 0)
-
766 n += 86400*1000;
-
767 *this = t; -
768 return n;
-
769} -
770int QTime::elapsed() const -
771{ -
772 int n = msecsTo(currentTime()); -
773 if (n < 0)
-
774 n += 86400 * 1000;
-
775 return n;
-
776} -
777QDateTime::QDateTime() -
778 : d(new QDateTimePrivate) -
779{ -
780}
-
781 -
782 -
783 -
784 -
785 -
786 -
787 -
788QDateTime::QDateTime(const QDate &date) -
789 : d(new QDateTimePrivate) -
790{ -
791 d->date = date; -
792 d->time = QTime(0, 0, 0); -
793}
-
794QDateTime::QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec) -
795 : d(new QDateTimePrivate) -
796{ -
797 d->date = date; -
798 d->time = date.isValid() && !time.isValid() ? QTime(0, 0, 0) : time;
-
799 d->spec = (spec == Qt::UTC) ? QDateTimePrivate::UTC : QDateTimePrivate::LocalUnknown;
-
800}
-
801 -
802 -
803 -
804 -
805 -
806QDateTime::QDateTime(const QDateTime &other) -
807 : d(other.d) -
808{ -
809}
-
810 -
811 -
812 -
813 -
814QDateTime::~QDateTime() -
815{ -
816} -
817 -
818 -
819 -
820 -
821 -
822 -
823QDateTime &QDateTime::operator=(const QDateTime &other) -
824{ -
825 d = other.d; -
826 return *this;
-
827} -
828bool QDateTime::isNull() const -
829{ -
830 return d->date.isNull() && d->time.isNull();
-
831} -
832bool QDateTime::isValid() const -
833{ -
834 return d->date.isValid() && d->time.isValid();
-
835} -
836 -
837 -
838 -
839 -
840 -
841 -
842 -
843QDate QDateTime::date() const -
844{ -
845 return d->date;
-
846} -
847 -
848 -
849 -
850 -
851 -
852 -
853 -
854QTime QDateTime::time() const -
855{ -
856 return d->time;
-
857} -
858 -
859 -
860 -
861 -
862 -
863 -
864 -
865Qt::TimeSpec QDateTime::timeSpec() const -
866{ -
867 switch(d->spec) -
868 { -
869 case QDateTimePrivate::UTC: -
870 return Qt::UTC;
-
871 case QDateTimePrivate::OffsetFromUTC: -
872 return Qt::OffsetFromUTC;
-
873 default: -
874 return Qt::LocalTime;
-
875 } -
876}
-
877void QDateTime::setDate(const QDate &date) -
878{ -
879 detach(); -
880 d->date = date; -
881 if (d->spec == QDateTimePrivate::LocalStandard
-
882 || d->spec == QDateTimePrivate::LocalDST)
-
883 d->spec = QDateTimePrivate::LocalUnknown;
-
884 if (date.isValid() && !d->time.isValid())
-
885 d->time = QTime(0, 0, 0);
-
886}
-
887 -
888 -
889 -
890 -
891 -
892 -
893 -
894void QDateTime::setTime(const QTime &time) -
895{ -
896 detach(); -
897 if (d->spec == QDateTimePrivate::LocalStandard
-
898 || d->spec == QDateTimePrivate::LocalDST)
-
899 d->spec = QDateTimePrivate::LocalUnknown;
-
900 d->time = time; -
901}
-
902void QDateTime::setTimeSpec(Qt::TimeSpec spec) -
903{ -
904 detach(); -
905 -
906 switch(spec) -
907 { -
908 case Qt::UTC: -
909 d->spec = QDateTimePrivate::UTC; -
910 break;
-
911 case Qt::OffsetFromUTC: -
912 d->spec = QDateTimePrivate::OffsetFromUTC; -
913 break;
-
914 default: -
915 d->spec = QDateTimePrivate::LocalUnknown; -
916 break;
-
917 } -
918}
-
919 -
920qint64 toMSecsSinceEpoch_helper(qint64 jd, int msecs) -
921{ -
922 qint64 days = jd - JULIAN_DAY_FOR_EPOCH; -
923 qint64 retval = (days * MSECS_PER_DAY) + msecs; -
924 return retval;
-
925} -
926qint64 QDateTime::toMSecsSinceEpoch() const -
927{ -
928 QDate utcDate; -
929 QTime utcTime; -
930 d->getUTC(utcDate, utcTime); -
931 -
932 return toMSecsSinceEpoch_helper(utcDate.toJulianDay(), QTime(0, 0, 0).msecsTo(utcTime));
-
933} -
934uint QDateTime::toTime_t() const -
935{ -
936 qint64 retval = toMSecsSinceEpoch() / 1000; -
937 if (quint64(retval) >= static_cast<unsigned long long>(0xFFFFFFFFULL))
-
938 return uint(-1);
-
939 return uint(retval);
-
940} -
941void QDateTime::setMSecsSinceEpoch(qint64 msecs) -
942{ -
943 detach(); -
944 -
945 QDateTimePrivate::Spec oldSpec = d->spec; -
946 -
947 qint64 ddays = msecs / MSECS_PER_DAY; -
948 msecs %= MSECS_PER_DAY; -
949 if (msecs < 0) {
-
950 -
951 --ddays; -
952 msecs += MSECS_PER_DAY; -
953 }
-
954 -
955 d->date = QDate(1970, 1, 1).addDays(ddays); -
956 d->time = QTime(0, 0, 0).addMSecs(msecs); -
957 d->spec = QDateTimePrivate::UTC; -
958 -
959 if (oldSpec != QDateTimePrivate::UTC)
-
960 d->spec = d->getLocal(d->date, d->time);
-
961}
-
962void QDateTime::setTime_t(uint secsSince1Jan1970UTC) -
963{ -
964 detach(); -
965 -
966 QDateTimePrivate::Spec oldSpec = d->spec; -
967 -
968 d->date = QDate(1970, 1, 1).addDays(secsSince1Jan1970UTC / SECS_PER_DAY); -
969 d->time = QTime(0, 0, 0).addSecs(secsSince1Jan1970UTC % SECS_PER_DAY); -
970 d->spec = QDateTimePrivate::UTC; -
971 -
972 if (oldSpec != QDateTimePrivate::UTC)
-
973 d->spec = d->getLocal(d->date, d->time);
-
974}
-
975QString QDateTime::toString(Qt::DateFormat f) const -
976{ -
977 QString buf; -
978 if (!isValid())
-
979 return buf;
-
980 -
981 if (f == Qt::ISODate) {
-
982 buf = d->date.toString(Qt::ISODate); -
983 if (buf.isEmpty())
-
984 return QString();
-
985 buf += QLatin1Char('T'); -
986 buf += d->time.toString(Qt::ISODate); -
987 switch (d->spec) { -
988 case QDateTimePrivate::UTC: -
989 buf += QLatin1Char('Z'); -
990 break;
-
991 case QDateTimePrivate::OffsetFromUTC: { -
992 int sign = d->utcOffset >= 0 ? 1: -1;
-
993 buf += QString::fromLatin1("%1%2:%3"). -
994 arg(sign == 1 ? QLatin1Char('+') : QLatin1Char('-')). -
995 arg(d->utcOffset * sign / SECS_PER_HOUR, 2, 10, QLatin1Char('0')). -
996 arg((d->utcOffset / 60) % 60, 2, 10, QLatin1Char('0')); -
997 break;
-
998 } -
999 default: -
1000 break;
-
1001 } -
1002 }
-
1003 -
1004 else if (f == Qt::TextDate) {
-
1005 -
1006 buf = d->date.shortDayName(d->date.dayOfWeek()); -
1007 buf += QLatin1Char(' '); -
1008 buf += d->date.shortMonthName(d->date.month()); -
1009 buf += QLatin1Char(' '); -
1010 buf += QString::number(d->date.day()); -
1011 buf += QLatin1Char(' '); -
1012 buf += d->time.toString(); -
1013 buf += QLatin1Char(' '); -
1014 buf += QString::number(d->date.year()); -
1015 }
-
1016 -
1017 else { -
1018 buf = d->date.toString(f); -
1019 if (buf.isEmpty())
-
1020 return QString();
-
1021 buf += QLatin1Char(' '); -
1022 buf += d->time.toString(f); -
1023 }
-
1024 -
1025 return buf;
-
1026} -
1027QString QDateTime::toString(const QString& format) const -
1028{ -
1029 return fmtDateTime(format, &d->time, &d->date);
-
1030} -
1031QDateTime QDateTime::addDays(qint64 ndays) const -
1032{ -
1033 return QDateTime(d->date.addDays(ndays), d->time, timeSpec());
-
1034} -
1035QDateTime QDateTime::addMonths(int nmonths) const -
1036{ -
1037 return QDateTime(d->date.addMonths(nmonths), d->time, timeSpec());
-
1038} -
1039QDateTime QDateTime::addYears(int nyears) const -
1040{ -
1041 return QDateTime(d->date.addYears(nyears), d->time, timeSpec());
-
1042} -
1043 -
1044QDateTime QDateTimePrivate::addMSecs(const QDateTime &dt, qint64 msecs) -
1045{ -
1046 if (!dt.isValid())
-
1047 return QDateTime();
-
1048 -
1049 QDate utcDate; -
1050 QTime utcTime; -
1051 dt.d->getUTC(utcDate, utcTime); -
1052 -
1053 addMSecs(utcDate, utcTime, msecs); -
1054 -
1055 return QDateTime(utcDate, utcTime, Qt::UTC).toTimeSpec(dt.timeSpec());
-
1056} -
1057void QDateTimePrivate::addMSecs(QDate &utcDate, QTime &utcTime, qint64 msecs) -
1058{ -
1059 qint64 dd = utcDate.toJulianDay(); -
1060 int tt = QTime(0, 0, 0).msecsTo(utcTime); -
1061 int sign = 1; -
1062 if (msecs < 0) {
-
1063 msecs = -msecs; -
1064 sign = -1; -
1065 }
-
1066 if (msecs >= int(MSECS_PER_DAY)) {
-
1067 dd += sign * (msecs / MSECS_PER_DAY); -
1068 msecs %= MSECS_PER_DAY; -
1069 }
-
1070 -
1071 tt += sign * msecs; -
1072 if (tt < 0) {
-
1073 tt = MSECS_PER_DAY - tt - 1; -
1074 dd -= tt / MSECS_PER_DAY; -
1075 tt = tt % MSECS_PER_DAY; -
1076 tt = MSECS_PER_DAY - tt - 1; -
1077 } else if (tt >= int(MSECS_PER_DAY)) {
-
1078 dd += tt / MSECS_PER_DAY; -
1079 tt = tt % MSECS_PER_DAY; -
1080 }
-
1081 -
1082 utcDate = QDate::fromJulianDay(dd); -
1083 utcTime = QTime(0, 0, 0).addMSecs(tt); -
1084}
-
1085QDateTime QDateTime::addSecs(qint64 s) const -
1086{ -
1087 return d->addMSecs(*this, s * 1000);
-
1088} -
1089QDateTime QDateTime::addMSecs(qint64 msecs) const -
1090{ -
1091 return d->addMSecs(*this, msecs);
-
1092} -
1093qint64 QDateTime::daysTo(const QDateTime &other) const -
1094{ -
1095 return d->date.daysTo(other.d->date);
-
1096} -
1097qint64 QDateTime::secsTo(const QDateTime &other) const -
1098{ -
1099 if (!isValid() || !other.isValid())
-
1100 return 0;
-
1101 -
1102 QDate date1, date2; -
1103 QTime time1, time2; -
1104 -
1105 d->getUTC(date1, time1); -
1106 other.d->getUTC(date2, time2); -
1107 -
1108 return (date1.daysTo(date2) * SECS_PER_DAY) + time1.secsTo(time2);
-
1109} -
1110qint64 QDateTime::msecsTo(const QDateTime &other) const -
1111{ -
1112 if (!isValid() || !other.isValid())
-
1113 return 0;
-
1114 -
1115 QDate selfDate; -
1116 QDate otherDate; -
1117 QTime selfTime; -
1118 QTime otherTime; -
1119 -
1120 d->getUTC(selfDate, selfTime); -
1121 other.d->getUTC(otherDate, otherTime); -
1122 -
1123 return (static_cast<qint64>(selfDate.daysTo(otherDate)) * static_cast<qint64>(MSECS_PER_DAY)) -
1124 + static_cast<qint64>(selfTime.msecsTo(otherTime));
-
1125} -
1126QDateTime QDateTime::toTimeSpec(Qt::TimeSpec spec) const -
1127{ -
1128 if ((d->spec == QDateTimePrivate::UTC) == (spec == Qt::UTC))
-
1129 return *this;
-
1130 -
1131 QDateTime ret; -
1132 if (spec == Qt::UTC) {
-
1133 d->getUTC(ret.d->date, ret.d->time); -
1134 ret.d->spec = QDateTimePrivate::UTC; -
1135 } else {
-
1136 ret.d->spec = d->getLocal(ret.d->date, ret.d->time); -
1137 }
-
1138 return ret;
-
1139} -
1140bool QDateTime::operator==(const QDateTime &other) const -
1141{ -
1142 if (d->spec == other.d->spec && d->utcOffset == other.d->utcOffset)
-
1143 return d->time == other.d->time && d->date == other.d->date;
-
1144 else { -
1145 QDate date1, date2; -
1146 QTime time1, time2; -
1147 -
1148 d->getUTC(date1, time1); -
1149 other.d->getUTC(date2, time2); -
1150 return time1 == time2 && date1 == date2;
-
1151 } -
1152} -
1153bool QDateTime::operator<(const QDateTime &other) const -
1154{ -
1155 if (d->spec == other.d->spec && d->spec != QDateTimePrivate::OffsetFromUTC) {
-
1156 if (d->date != other.d->date)
-
1157 return d->date < other.d->date;
-
1158 return d->time < other.d->time;
-
1159 } else { -
1160 QDate date1, date2; -
1161 QTime time1, time2; -
1162 d->getUTC(date1, time1); -
1163 other.d->getUTC(date2, time2); -
1164 if (date1 != date2)
-
1165 return date1 < date2;
-
1166 return time1 < time2;
-
1167 } -
1168} -
1169static inline uint msecsFromDecomposed(int hour, int minute, int sec, int msec = 0) -
1170{ -
1171 return MSECS_PER_HOUR * hour + MSECS_PER_MIN * minute + 1000 * sec + msec;
-
1172} -
1173QDate QDate::currentDate() -
1174{ -
1175 QDate d; -
1176 -
1177 time_t ltime; -
1178 time(&ltime); -
1179 struct tm *t = 0; -
1180 -
1181 -
1182 -
1183 tzset(); -
1184 struct tm res; -
1185 t = localtime_r(&ltime, &res); -
1186 -
1187 -
1188 -
1189 -
1190 d.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday); -
1191 return d;
-
1192} -
1193 -
1194QTime QTime::currentTime() -
1195{ -
1196 QTime ct; -
1197 -
1198 struct timeval tv; -
1199 gettimeofday(&tv, 0); -
1200 time_t ltime = tv.tv_sec; -
1201 struct tm *t = 0; -
1202 -
1203 -
1204 -
1205 tzset(); -
1206 struct tm res; -
1207 t = localtime_r(&ltime, &res); -
1208 -
1209 -
1210 -
1211 do { if (!(t)) qBadAlloc(); } while (0);
-
1212 -
1213 ct.mds = msecsFromDecomposed(t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000); -
1214 return ct;
-
1215} -
1216 -
1217QDateTime QDateTime::currentDateTime() -
1218{ -
1219 -
1220 -
1221 struct timeval tv; -
1222 gettimeofday(&tv, 0); -
1223 time_t ltime = tv.tv_sec; -
1224 struct tm *t = 0; -
1225 -
1226 -
1227 -
1228 tzset(); -
1229 struct tm res; -
1230 t = localtime_r(&ltime, &res); -
1231 -
1232 -
1233 -
1234 -
1235 QDateTime dt; -
1236 dt.d->time.mds = msecsFromDecomposed(t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000); -
1237 -
1238 dt.d->date.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday); -
1239 dt.d->spec = t->tm_isdst > 0 ? QDateTimePrivate::LocalDST :
-
1240 t->tm_isdst == 0 ? QDateTimePrivate::LocalStandard : -
1241 QDateTimePrivate::LocalUnknown; -
1242 return dt;
-
1243} -
1244 -
1245QDateTime QDateTime::currentDateTimeUtc() -
1246{ -
1247 -
1248 -
1249 struct timeval tv; -
1250 gettimeofday(&tv, 0); -
1251 time_t ltime = tv.tv_sec; -
1252 struct tm *t = 0; -
1253 -
1254 -
1255 -
1256 struct tm res; -
1257 t = gmtime_r(&ltime, &res); -
1258 -
1259 -
1260 -
1261 -
1262 QDateTime dt; -
1263 dt.d->time.mds = msecsFromDecomposed(t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000); -
1264 -
1265 dt.d->date.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday); -
1266 dt.d->spec = QDateTimePrivate::UTC; -
1267 return dt;
-
1268} -
1269 -
1270qint64 QDateTime::currentMSecsSinceEpoch() -
1271{ -
1272 -
1273 -
1274 struct timeval tv; -
1275 gettimeofday(&tv, 0); -
1276 return qint64(tv.tv_sec) * static_cast<long long>(1000LL) + tv.tv_usec / 1000;
-
1277} -
1278QDateTime QDateTime::fromTime_t(uint seconds) -
1279{ -
1280 QDateTime d; -
1281 d.setTime_t(seconds); -
1282 return d;
-
1283} -
1284QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs) -
1285{ -
1286 QDateTime d; -
1287 d.setMSecsSinceEpoch(msecs); -
1288 return d;
-
1289} -
1290void QDateTime::setUtcOffset(int seconds) -
1291{ -
1292 detach(); -
1293 -
1294 -
1295 -
1296 -
1297 if(seconds == 0)
-
1298 d->spec = QDateTimePrivate::UTC;
-
1299 else -
1300 d->spec = QDateTimePrivate::OffsetFromUTC;
-
1301 -
1302 -
1303 d->utcOffset = seconds; -
1304}
-
1305int QDateTime::utcOffset() const -
1306{ -
1307 if(isValid() && d->spec == QDateTimePrivate::OffsetFromUTC)
-
1308 return d->utcOffset;
-
1309 else -
1310 return 0;
-
1311} -
1312 -
1313 -
1314 -
1315static int fromShortMonthName(const QString &monthName) -
1316{ -
1317 -
1318 for (int i = 0; i < 12; ++i) {
-
1319 if (monthName == QLatin1String(qt_shortMonthNames[i]))
-
1320 return i + 1;
-
1321 }
-
1322 -
1323 for (int i = 1; i <= 12; ++i) {
-
1324 if (monthName == QDate::shortMonthName(i))
-
1325 return i;
-
1326 }
-
1327 return -1;
-
1328} -
1329QDateTime QDateTime::fromString(const QString& s, Qt::DateFormat f) -
1330{ -
1331 if (s.isEmpty()) {
-
1332 return QDateTime();
-
1333 } -
1334 -
1335 switch (f) { -
1336 case Qt::ISODate: { -
1337 QString tmp = s; -
1338 Qt::TimeSpec ts = Qt::LocalTime; -
1339 QDate date = QDate::fromString(tmp.left(10), Qt::ISODate); -
1340 if (tmp.size() == 10)
-
1341 return QDateTime(date);
-
1342 -
1343 tmp = tmp.mid(11); -
1344 -
1345 -
1346 if (tmp.endsWith(QLatin1Char('Z'))) {
-
1347 ts = Qt::UTC; -
1348 tmp.chop(1); -
1349 }
-
1350 -
1351 -
1352 QRegExp rx(QLatin1String("[+-]")); -
1353 if (tmp.contains(rx)) {
-
1354 int idx = tmp.indexOf(rx); -
1355 QString tmp2 = tmp.mid(idx); -
1356 tmp = tmp.left(idx); -
1357 bool ok = true; -
1358 int ntzhour = 1; -
1359 int ntzminute = 3; -
1360 if ( tmp2.indexOf(QLatin1Char(':')) == 3 )
-
1361 ntzminute = 4;
-
1362 const int tzhour(tmp2.mid(ntzhour, 2).toInt(&ok)); -
1363 const int tzminute(tmp2.mid(ntzminute, 2).toInt(&ok)); -
1364 QTime tzt(tzhour, tzminute); -
1365 int utcOffset = (tzt.hour() * 60 + tzt.minute()) * 60; -
1366 if ( utcOffset != 0 ) {
-
1367 ts = Qt::OffsetFromUTC; -
1368 QDateTime dt(date, QTime::fromString(tmp, Qt::ISODate), ts); -
1369 dt.setUtcOffset( utcOffset * (tmp2.startsWith(QLatin1Char('-')) ? -1 : 1) ); -
1370 return dt;
-
1371 } -
1372 }
-
1373 -
1374 bool isMidnight24 = false; -
1375 -
1376 QTime time(fromStringImpl(tmp, Qt::ISODate, isMidnight24)); -
1377 if (isMidnight24) {
-
1378 -
1379 date = date.addDays(1); -
1380 }
-
1381 -
1382 return QDateTime(date, time, ts);
-
1383 } -
1384 case Qt::SystemLocaleDate: -
1385 case Qt::SystemLocaleShortDate: -
1386 case Qt::SystemLocaleLongDate: -
1387 return fromString(s, QLocale::system().dateTimeFormat(f == Qt::SystemLocaleLongDate ? QLocale::LongFormat -
1388 : QLocale::ShortFormat));
-
1389 case Qt::LocaleDate: -
1390 case Qt::DefaultLocaleShortDate: -
1391 case Qt::DefaultLocaleLongDate: -
1392 return fromString(s, QLocale().dateTimeFormat(f == Qt::DefaultLocaleLongDate ? QLocale::LongFormat -
1393 : QLocale::ShortFormat));
-
1394 -
1395 case Qt::TextDate: { -
1396 QStringList parts = s.split(QLatin1Char(' '), QString::SkipEmptyParts); -
1397 -
1398 if ((parts.count() < 5) || (parts.count() > 6)) {
-
1399 return QDateTime();
-
1400 } -
1401 -
1402 -
1403 int month = -1, day = -1; -
1404 bool ok; -
1405 -
1406 month = fromShortMonthName(parts.at(1)); -
1407 if (month != -1) {
-
1408 day = parts.at(2).toInt(&ok); -
1409 if (!ok)
-
1410 day = -1;
-
1411 }
-
1412 -
1413 if (month == -1 || day == -1) {
-
1414 -
1415 month = fromShortMonthName(parts.at(2)); -
1416 if (month != -1) {
-
1417 QString dayStr = parts.at(1); -
1418 if (dayStr.endsWith(QLatin1Char('.'))) {
-
1419 dayStr.chop(1); -
1420 day = dayStr.toInt(&ok); -
1421 if (!ok)
-
1422 day = -1;
-
1423 } else {
-
1424 day = -1; -
1425 }
-
1426 } -
1427 }
-
1428 -
1429 if (month == -1 || day == -1) {
-
1430 -
1431 return QDateTime();
-
1432 } -
1433 -
1434 int year; -
1435 QStringList timeParts = parts.at(3).split(QLatin1Char(':')); -
1436 if ((timeParts.count() == 3) || (timeParts.count() == 2)) {
-
1437 -
1438 year = parts.at(4).toInt(&ok); -
1439 if (!ok)
-
1440 return QDateTime();
-
1441 } else {
-
1442 timeParts = parts.at(4).split(QLatin1Char(':')); -
1443 if ((timeParts.count() != 3) && (timeParts.count() != 2))
-
1444 return QDateTime();
-
1445 year = parts.at(3).toInt(&ok); -
1446 if (!ok)
-
1447 return QDateTime();
-
1448 }
-
1449 -
1450 int hour = timeParts.at(0).toInt(&ok); -
1451 if (!ok) {
-
1452 return QDateTime();
-
1453 } -
1454 -
1455 int minute = timeParts.at(1).toInt(&ok); -
1456 if (!ok) {
-
1457 return QDateTime();
-
1458 } -
1459 -
1460 int second = (timeParts.count() > 2) ? timeParts.at(2).toInt(&ok) : 0;
-
1461 if (!ok) {
-
1462 return QDateTime();
-
1463 } -
1464 -
1465 QDate date(year, month, day); -
1466 QTime time(hour, minute, second); -
1467 -
1468 if (parts.count() == 5)
-
1469 return QDateTime(date, time, Qt::LocalTime);
-
1470 -
1471 QString tz = parts.at(5); -
1472 if (!tz.startsWith(QLatin1String("GMT"), Qt::CaseInsensitive))
-
1473 return QDateTime();
-
1474 QDateTime dt(date, time, Qt::UTC); -
1475 if (tz.length() > 3) {
-
1476 int tzoffset = 0; -
1477 QChar sign = tz.at(3); -
1478 if ((sign != QLatin1Char('+'))
-
1479 && (sign != QLatin1Char('-'))) {
-
1480 return QDateTime();
-
1481 } -
1482 int tzhour = tz.mid(4, 2).toInt(&ok); -
1483 if (!ok)
-
1484 return QDateTime();
-
1485 int tzminute = tz.mid(6).toInt(&ok); -
1486 if (!ok)
-
1487 return QDateTime();
-
1488 tzoffset = (tzhour*60 + tzminute) * 60; -
1489 if (sign == QLatin1Char('-'))
-
1490 tzoffset = -tzoffset;
-
1491 dt.setUtcOffset(tzoffset); -
1492 }
-
1493 return dt.toLocalTime();
-
1494 } -
1495 -
1496 } -
1497 -
1498 return QDateTime();
-
1499} -
1500QDateTime QDateTime::fromString(const QString &string, const QString &format) -
1501{ -
1502 -
1503 QTime time; -
1504 QDate date; -
1505 -
1506 QDateTimeParser dt(QVariant::DateTime, QDateTimeParser::FromString); -
1507 if (dt.parseFormat(format) && dt.fromString(string, &date, &time))
-
1508 return QDateTime(date, time);
-
1509 -
1510 -
1511 -
1512 -
1513 return QDateTime(QDate(), QTime(-1, -1, -1));
-
1514} -
1515void QDateTime::detach() -
1516{ -
1517 d.detach(); -
1518}
-
1519QDataStream &operator<<(QDataStream &out, const QDate &date) -
1520{ -
1521 if (out.version() < QDataStream::Qt_5_0)
-
1522 return out << quint32(date.jd);
-
1523 else -
1524 return out << qint64(date.jd);
-
1525} -
1526QDataStream &operator>>(QDataStream &in, QDate &date) -
1527{ -
1528 if (in.version() < QDataStream::Qt_5_0) {
-
1529 quint32 jd; -
1530 in >> jd; -
1531 -
1532 date.jd = (jd != 0 ? jd : QDate::nullJd());
-
1533 } else {
-
1534 qint64 jd; -
1535 in >> jd; -
1536 date.jd = jd; -
1537 }
-
1538 -
1539 return in;
-
1540} -
1541QDataStream &operator<<(QDataStream &out, const QTime &time) -
1542{ -
1543 return out << quint32(time.mds);
-
1544} -
1545QDataStream &operator>>(QDataStream &in, QTime &time) -
1546{ -
1547 quint32 ds; -
1548 in >> ds; -
1549 time.mds = int(ds); -
1550 return in;
-
1551} -
1552QDataStream &operator<<(QDataStream &out, const QDateTime &dateTime) -
1553{ -
1554 if (out.version() >= 13) {
-
1555 if (dateTime.isValid()) {
-
1556 QDateTime asUTC = dateTime.toUTC(); -
1557 out << asUTC.d->date << asUTC.d->time; -
1558 } else {
-
1559 out << dateTime.d->date << dateTime.d->time; -
1560 }
-
1561 out << (qint8)dateTime.timeSpec(); -
1562 } else {
-
1563 out << dateTime.d->date << dateTime.d->time; -
1564 if (out.version() >= 7)
-
1565 out << (qint8)dateTime.d->spec;
-
1566 }
-
1567 return out;
-
1568} -
1569QDataStream &operator>>(QDataStream &in, QDateTime &dateTime) -
1570{ -
1571 dateTime.detach(); -
1572 -
1573 in >> dateTime.d->date >> dateTime.d->time; -
1574 -
1575 if (in.version() >= 13) {
-
1576 qint8 ts = 0; -
1577 in >> ts; -
1578 if (dateTime.isValid()) {
-
1579 -
1580 dateTime.d->spec = QDateTimePrivate::UTC; -
1581 dateTime = dateTime.toTimeSpec(static_cast<Qt::TimeSpec>(ts)); -
1582 }
-
1583 } else {
-
1584 qint8 ts = (qint8)QDateTimePrivate::LocalUnknown; -
1585 if (in.version() >= 7)
-
1586 in >> ts;
-
1587 dateTime.d->spec = (QDateTimePrivate::Spec)ts; -
1588 }
-
1589 return in;
-
1590} -
1591 -
1592 -
1593 -
1594 -
1595 -
1596static bool hasUnquotedAP(const QString &f) -
1597{ -
1598 const QLatin1Char quote('\''); -
1599 bool inquote = false; -
1600 const int max = f.size(); -
1601 for (int i=0; i<max; ++i) {
evaluated: i<max
TRUEFALSE
yes
Evaluation Count:322540
yes
Evaluation Count:27883
27883-322540
1602 if (f.at(i) == quote) {
evaluated: f.at(i) == quote
TRUEFALSE
yes
Evaluation Count:24
yes
Evaluation Count:322516
24-322516
1603 inquote = !inquote; -
1604 } else if (!inquote && f.at(i).toUpper() == QLatin1Char('A')8-322493
&& i + 1 < max && f.at(i + 1).toUpper() == QLatin1Char('P')) {
evaluated: !inquote
TRUEFALSE
yes
Evaluation Count:322493
yes
Evaluation Count:23
evaluated: f.at(i).toUpper() == QLatin1Char('A')
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:322485
executed: }
Execution Count:24
1605 return true;
executed: return true;
Execution Count:8
8
1606 } -
1607 } -
1608 return false;
executed: return false;
Execution Count:27883
27883
1609} -
1610 -
1611 -
1612 -
1613 -
1614 -
1615 -
1616 -
1617static QString getFmtString(const QString& f, const QTime* dt = 0, const QDate* dd = 0, bool am_pm = false) -
1618{ -
1619 if (f.isEmpty())
-
1620 return QString();
-
1621 -
1622 QString buf = f; -
1623 int removed = 0; -
1624 -
1625 if (dt) {
-
1626 if (f.startsWith(QLatin1String("hh")) || f.startsWith(QLatin1String("HH"))) {
-
1627 const bool hour12 = f.at(0) == QLatin1Char('h') && am_pm;
-
1628 if (hour12 && dt->hour() > 12)
-
1629 buf = QString::number(dt->hour() - 12).rightJustified(2, QLatin1Char('0'), true);
-
1630 else if (hour12 && dt->hour() == 0)
-
1631 buf = QLatin1String("12");
-
1632 else -
1633 buf = QString::number(dt->hour()).rightJustified(2, QLatin1Char('0'), true);
-
1634 removed = 2; -
1635 } else if (f.at(0) == QLatin1Char('h') || f.at(0) == QLatin1Char('H')) {
-
1636 const bool hour12 = f.at(0) == QLatin1Char('h') && am_pm;
-
1637 if (hour12 && dt->hour() > 12)
-
1638 buf = QString::number(dt->hour() - 12);
-
1639 else if (hour12 && dt->hour() == 0)
-
1640 buf = QLatin1String("12");
-
1641 else -
1642 buf = QString::number(dt->hour());
-
1643 removed = 1; -
1644 } else if (f.startsWith(QLatin1String("mm"))) {
-
1645 buf = QString::number(dt->minute()).rightJustified(2, QLatin1Char('0'), true); -
1646 removed = 2; -
1647 } else if (f.at(0) == (QLatin1Char('m'))) {
-
1648 buf = QString::number(dt->minute()); -
1649 removed = 1; -
1650 } else if (f.startsWith(QLatin1String("ss"))) {
-
1651 buf = QString::number(dt->second()).rightJustified(2, QLatin1Char('0'), true); -
1652 removed = 2; -
1653 } else if (f.at(0) == QLatin1Char('s')) {
-
1654 buf = QString::number(dt->second()); -
1655 } else if (f.startsWith(QLatin1String("zzz"))) {
-
1656 buf = QString::number(dt->msec()).rightJustified(3, QLatin1Char('0'), true); -
1657 removed = 3; -
1658 } else if (f.at(0) == QLatin1Char('z')) {
-
1659 buf = QString::number(dt->msec()); -
1660 removed = 1; -
1661 } else if (f.at(0).toUpper() == QLatin1Char('A')) {
-
1662 const bool upper = f.at(0) == QLatin1Char('A'); -
1663 buf = dt->hour() < 12 ? QLatin1String("am") : QLatin1String("pm");
-
1664 if (upper)
-
1665 buf = buf.toUpper();
-
1666 if (f.size() > 1 && f.at(1).toUpper() == QLatin1Char('P') &&
-
1667 f.at(0).isUpper() == f.at(1).isUpper()) {
-
1668 removed = 2; -
1669 } else {
-
1670 removed = 1; -
1671 }
-
1672 } -
1673 } -
1674 -
1675 if (dd) {
-
1676 if (f.startsWith(QLatin1String("dddd"))) {
-
1677 buf = dd->longDayName(dd->dayOfWeek()); -
1678 removed = 4; -
1679 } else if (f.startsWith(QLatin1String("ddd"))) {
-
1680 buf = dd->shortDayName(dd->dayOfWeek()); -
1681 removed = 3; -
1682 } else if (f.startsWith(QLatin1String("dd"))) {
-
1683 buf = QString::number(dd->day()).rightJustified(2, QLatin1Char('0'), true); -
1684 removed = 2; -
1685 } else if (f.at(0) == QLatin1Char('d')) {
-
1686 buf = QString::number(dd->day()); -
1687 removed = 1; -
1688 } else if (f.startsWith(QLatin1String("MMMM"))) {
-
1689 buf = dd->longMonthName(dd->month()); -
1690 removed = 4; -
1691 } else if (f.startsWith(QLatin1String("MMM"))) {
-
1692 buf = dd->shortMonthName(dd->month()); -
1693 removed = 3; -
1694 } else if (f.startsWith(QLatin1String("MM"))) {
-
1695 buf = QString::number(dd->month()).rightJustified(2, QLatin1Char('0'), true); -
1696 removed = 2; -
1697 } else if (f.at(0) == QLatin1Char('M')) {
-
1698 buf = QString::number(dd->month()); -
1699 removed = 1; -
1700 } else if (f.startsWith(QLatin1String("yyyy"))) {
-
1701 const int year = dd->year(); -
1702 buf = QString::number(qAbs(year)).rightJustified(4, QLatin1Char('0')); -
1703 if(year > 0)
-
1704 removed = 4;
-
1705 else -
1706 { -
1707 buf.prepend(QLatin1Char('-')); -
1708 removed = 5; -
1709 }
-
1710 -
1711 } else if (f.startsWith(QLatin1String("yy"))) {
-
1712 buf = QString::number(dd->year()).right(2).rightJustified(2, QLatin1Char('0')); -
1713 removed = 2; -
1714 }
-
1715 } -
1716 if (removed == 0 || removed >= f.size()) {
-
1717 return buf;
-
1718 } -
1719 -
1720 return buf + getFmtString(f.mid(removed), dt, dd, am_pm);
-
1721} -
1722 -
1723 -
1724static QString fmtDateTime(const QString& f, const QTime* dt, const QDate* dd) -
1725{ -
1726 QString buf; -
1727 -
1728 if (f.isEmpty())
-
1729 return buf;
-
1730 if (dt && !dt->isValid())
-
1731 return buf;
-
1732 if (dd && !dd->isValid())
-
1733 return buf;
-
1734 -
1735 const bool ap = hasUnquotedAP(f); -
1736 -
1737 QString frm; -
1738 uint status = '0'; -
1739 -
1740 for (int i = 0, n = f.length(); i < n; ++i) {
-
1741 const QChar c = f.at(i); -
1742 const uint cc = c.unicode(); -
1743 if (cc == '\'') {
-
1744 if (status == cc) {
-
1745 if (i > 0 && f.at(i - 1).unicode() == cc)
-
1746 buf += c;
-
1747 status = '0'; -
1748 } else {
-
1749 if (!frm.isEmpty()) {
-
1750 buf += getFmtString(frm, dt, dd, ap); -
1751 frm.clear(); -
1752 }
-
1753 status = cc; -
1754 }
-
1755 } else if (status == '\'') {
-
1756 buf += c; -
1757 } else if (c == status) {
-
1758 if (ap && (cc == 'P' || cc == 'p'))
-
1759 status = '0';
-
1760 frm += c; -
1761 } else {
-
1762 buf += getFmtString(frm, dt, dd, ap); -
1763 frm.clear(); -
1764 if (cc == 'h' || cc == 'm' || cc == 'H' || cc == 's' || cc == 'z') {
-
1765 status = cc; -
1766 frm += c; -
1767 } else if (cc == 'd' || cc == 'M' || cc == 'y') {
-
1768 status = cc; -
1769 frm += c; -
1770 } else if (ap && cc == 'A') {
-
1771 status = 'P'; -
1772 frm += c; -
1773 } else if (ap && cc == 'a') {
-
1774 status = 'p'; -
1775 frm += c; -
1776 } else {
-
1777 buf += c; -
1778 status = '0'; -
1779 }
-
1780 } -
1781 } -
1782 -
1783 buf += getFmtString(frm, dt, dd, ap); -
1784 -
1785 return buf;
-
1786} -
1787 -
1788 -
1789 -
1790 -
1791 -
1792static const int LowerYear = 1970; -
1793 -
1794static const int UpperYear = 2037; -
1795 -
1796static QDate adjustDate(QDate date) -
1797{ -
1798 QDate lowerLimit(LowerYear, 1, 2); -
1799 QDate upperLimit(UpperYear, 12, 30); -
1800 -
1801 if (date > lowerLimit && date < upperLimit)
-
1802 return date;
-
1803 -
1804 int month = date.month(); -
1805 int day = date.day(); -
1806 -
1807 -
1808 if (month == 2 && day == 29)
-
1809 --day;
-
1810 -
1811 if (date < lowerLimit)
-
1812 date.setDate(LowerYear, month, day);
-
1813 else -
1814 date.setDate(UpperYear, month, day);
-
1815 -
1816 return date;
-
1817} -
1818 -
1819static QDateTimePrivate::Spec utcToLocal(QDate &date, QTime &time) -
1820{ -
1821 QDate fakeDate = adjustDate(date); -
1822 -
1823 -
1824 time_t secsSince1Jan1970UTC = toMSecsSinceEpoch_helper(fakeDate.toJulianDay(), QTime(0, 0, 0).msecsTo(time)) / 1000; -
1825 tm *brokenDown = 0; -
1826 tzset(); -
1827 tm res; -
1828 brokenDown = localtime_r(&secsSince1Jan1970UTC, &res); -
1829 -
1830 -
1831 -
1832 -
1833 -
1834 -
1835 -
1836 if (!brokenDown) {
-
1837 date = QDate(1970, 1, 1); -
1838 time = QTime(); -
1839 return QDateTimePrivate::LocalUnknown;
-
1840 } else { -
1841 int deltaDays = fakeDate.daysTo(date); -
1842 date = QDate(brokenDown->tm_year + 1900, brokenDown->tm_mon + 1, brokenDown->tm_mday); -
1843 time = QTime(brokenDown->tm_hour, brokenDown->tm_min, brokenDown->tm_sec, time.msec()); -
1844 date = date.addDays(deltaDays); -
1845 if (brokenDown->tm_isdst > 0)
-
1846 return QDateTimePrivate::LocalDST;
-
1847 else if (brokenDown->tm_isdst < 0)
-
1848 return QDateTimePrivate::LocalUnknown;
-
1849 else -
1850 return QDateTimePrivate::LocalStandard;
-
1851 } -
1852} -
1853 -
1854static void localToUtc(QDate &date, QTime &time, int isdst) -
1855{ -
1856 if (!date.isValid())
-
1857 return;
-
1858 -
1859 QDate fakeDate = adjustDate(date); -
1860 -
1861 tm localTM; -
1862 localTM.tm_sec = time.second(); -
1863 localTM.tm_min = time.minute(); -
1864 localTM.tm_hour = time.hour(); -
1865 localTM.tm_mday = fakeDate.day(); -
1866 localTM.tm_mon = fakeDate.month() - 1; -
1867 localTM.tm_year = fakeDate.year() - 1900; -
1868 localTM.tm_isdst = (int)isdst; -
1869 -
1870 -
1871 -
1872 -
1873 -
1874 -
1875 time_t secsSince1Jan1970UTC = mktime(&localTM); -
1876 -
1877 tm *brokenDown = 0; -
1878 tm res; -
1879 brokenDown = gmtime_r(&secsSince1Jan1970UTC, &res); -
1880 -
1881 -
1882 -
1883 -
1884 -
1885 -
1886 -
1887 if (!brokenDown) {
-
1888 date = QDate(1970, 1, 1); -
1889 time = QTime(); -
1890 } else {
-
1891 int deltaDays = fakeDate.daysTo(date); -
1892 date = QDate(brokenDown->tm_year + 1900, brokenDown->tm_mon + 1, brokenDown->tm_mday); -
1893 time = QTime(brokenDown->tm_hour, brokenDown->tm_min, brokenDown->tm_sec, time.msec()); -
1894 date = date.addDays(deltaDays); -
1895 }
-
1896} -
1897 -
1898QDateTimePrivate::Spec QDateTimePrivate::getLocal(QDate &outDate, QTime &outTime) const -
1899{ -
1900 outDate = date; -
1901 outTime = time; -
1902 if (spec == QDateTimePrivate::UTC)
-
1903 return utcToLocal(outDate, outTime);
-
1904 return spec;
-
1905} -
1906 -
1907void QDateTimePrivate::getUTC(QDate &outDate, QTime &outTime) const -
1908{ -
1909 outDate = date; -
1910 outTime = time; -
1911 const bool isOffset = spec == QDateTimePrivate::OffsetFromUTC; -
1912 -
1913 if (spec != QDateTimePrivate::UTC && !isOffset)
-
1914 localToUtc(outDate, outTime, (int)spec);
-
1915 -
1916 if (isOffset)
-
1917 addMSecs(outDate, outTime, -(qint64(utcOffset) * 1000));
-
1918}
-
1919 -
1920 -
1921QDebug operator<<(QDebug dbg, const QDate &date) -
1922{ -
1923 dbg.nospace() << "QDate(" << date.toString() << ')'; -
1924 return dbg.space();
-
1925} -
1926 -
1927QDebug operator<<(QDebug dbg, const QTime &time) -
1928{ -
1929 dbg.nospace() << "QTime(" << time.toString() << ')'; -
1930 return dbg.space();
-
1931} -
1932 -
1933QDebug operator<<(QDebug dbg, const QDateTime &date) -
1934{ -
1935 dbg.nospace() << "QDateTime(" << date.toString() << ')'; -
1936 return dbg.space();
-
1937} -
1938uint qHash(const QDateTime &key, uint seed) -
1939{ -
1940 -
1941 -
1942 -
1943 -
1944 return qHash(key.toMSecsSinceEpoch(), seed);
-
1945} -
1946 -
1947 -
1948 -
1949 -
1950 -
1951 -
1952 -
1953uint qHash(const QDate &key, uint seed) -
1954{ -
1955 return qHash(key.toJulianDay(), seed);
-
1956} -
1957 -
1958 -
1959 -
1960 -
1961 -
1962 -
1963 -
1964uint qHash(const QTime &key, uint seed) -
1965{ -
1966 return qHash(QTime(0, 0, 0, 0).msecsTo(key), seed);
-
1967} -
1968int QDateTimeParser::getDigit(const QDateTime &t, int index) const -
1969{ -
1970 if (index < 0 || index >= sectionNodes.size()) {
-
1971 -
1972 QMessageLogger("tools/qdatetime.cpp", 41874182, __PRETTY_FUNCTION__).warning("QDateTimeParser::getDigit() Internal error (%s %d)", -
1973 QString(t.toString()).toLocal8Bit().constData(), index); -
1974 -
1975 -
1976 -
1977 return -1;
-
1978 } -
1979 const SectionNode &node = sectionNodes.at(index); -
1980 switch (node.type) { -
1981 case Hour24Section: case Hour12Section: return t.time().hour();
-
1982 case MinuteSection: return t.time().minute();
-
1983 case SecondSection: return t.time().second();
-
1984 case MSecSection: return t.time().msec();
-
1985 case YearSection2Digits: -
1986 case YearSection: return t.date().year();
-
1987 case MonthSection: return t.date().month();
-
1988 case DaySection: return t.date().day();
-
1989 case DayOfWeekSectionShort: -
1990 case DayOfWeekSectionLong: return t.date().day();
-
1991 case AmPmSection: return t.time().hour() > 11 ? 1 : 0;
-
1992 -
1993 default: break;
-
1994 } -
1995 -
1996 -
1997 QMessageLogger("tools/qdatetime.cpp", 42124207, __PRETTY_FUNCTION__).warning("QDateTimeParser::getDigit() Internal error 2 (%s %d)", -
1998 QString(t.toString()).toLocal8Bit().constData(), index); -
1999 -
2000 -
2001 -
2002 return -1;
-
2003} -
2004bool QDateTimeParser::setDigit(QDateTime &v, int index, int newVal) const -
2005{ -
2006 if (index < 0 || index >= sectionNodes.size()) {
-
2007 -
2008 QMessageLogger("tools/qdatetime.cpp", 42364231, __PRETTY_FUNCTION__).warning("QDateTimeParser::setDigit() Internal error (%s %d %d)", -
2009 QString(v.toString()).toLocal8Bit().constData(), index, newVal); -
2010 -
2011 -
2012 -
2013 return false;
-
2014 } -
2015 const SectionNode &node = sectionNodes.at(index); -
2016 -
2017 int year, month, day, hour, minute, second, msec; -
2018 year = v.date().year(); -
2019 month = v.date().month(); -
2020 day = v.date().day(); -
2021 hour = v.time().hour(); -
2022 minute = v.time().minute(); -
2023 second = v.time().second(); -
2024 msec = v.time().msec(); -
2025 -
2026 switch (node.type) { -
2027 case Hour24Section: case Hour12Section: hour = newVal; break;
-
2028 case MinuteSection: minute = newVal; break;
-
2029 case SecondSection: second = newVal; break;
-
2030 case MSecSection: msec = newVal; break;
-
2031 case YearSection2Digits: -
2032 case YearSection: year = newVal; break;
-
2033 case MonthSection: month = newVal; break;
-
2034 case DaySection: -
2035 case DayOfWeekSectionShort: -
2036 case DayOfWeekSectionLong: -
2037 if (newVal > 31) {
-
2038 -
2039 -
2040 -
2041 return false;
-
2042 } -
2043 day = newVal; -
2044 break;
-
2045 case AmPmSection: hour = (newVal == 0 ? hour % 12 : (hour % 12) + 12); break;
-
2046 default: -
2047 QMessageLogger("tools/qdatetime.cpp", 42754270, __PRETTY_FUNCTION__).warning("QDateTimeParser::setDigit() Internal error (%s)", -
2048 QString(sectionName(node.type)).toLocal8Bit().constData()); -
2049 break;
-
2050 } -
2051 -
2052 if (!(node.type & (DaySection|DayOfWeekSectionShort|DayOfWeekSectionLong))) {
-
2053 if (day < cachedDay)
-
2054 day = cachedDay;
-
2055 const int max = QDate(year, month, 1).daysInMonth(); -
2056 if (day > max) {
-
2057 day = max; -
2058 }
-
2059 }
-
2060 if (QDate::isValid(year, month, day) && QTime::isValid(hour, minute, second, msec)) {
-
2061 v = QDateTime(QDate(year, month, day), QTime(hour, minute, second, msec), spec); -
2062 return true;
-
2063 } -
2064 return false;
-
2065} -
2066int QDateTimeParser::absoluteMax(int s, const QDateTime &cur) const -
2067{ -
2068 const SectionNode &sn = sectionNode(s); -
2069 switch (sn.type) { -
2070 case Hour24Section: -
2071 case Hour12Section: return 23;
-
2072 -
2073 -
2074 case MinuteSection: -
2075 case SecondSection: return 59;
-
2076 case MSecSection: return 999;
-
2077 case YearSection2Digits: -
2078 case YearSection: return 9999;
-
2079 -
2080 -
2081 -
2082 case MonthSection: return 12;
-
2083 case DaySection: -
2084 case DayOfWeekSectionShort: -
2085 case DayOfWeekSectionLong: return cur.isValid() ? cur.date().daysInMonth() : 31;
-
2086 case AmPmSection: return 1;
-
2087 default: break;
-
2088 } -
2089 QMessageLogger("tools/qdatetime.cpp", 43264321, __PRETTY_FUNCTION__).warning("QDateTimeParser::absoluteMax() Internal error (%s)", -
2090 QString(sectionName(sn.type)).toLocal8Bit().constData()); -
2091 return -1;
-
2092} -
2093 -
2094 -
2095 -
2096 -
2097 -
2098 -
2099 -
2100int QDateTimeParser::absoluteMin(int s) const -
2101{ -
2102 const SectionNode &sn = sectionNode(s); -
2103 switch (sn.type) { -
2104 case Hour24Section: -
2105 case Hour12Section: -
2106 case MinuteSection: -
2107 case SecondSection: -
2108 case MSecSection: -
2109 case YearSection2Digits: -
2110 case YearSection: return 0;
-
2111 case MonthSection: -
2112 case DaySection: -
2113 case DayOfWeekSectionShort: -
2114 case DayOfWeekSectionLong: return 1;
-
2115 case AmPmSection: return 0;
-
2116 default: break;
-
2117 } -
2118 QMessageLogger("tools/qdatetime.cpp", 43554350, __PRETTY_FUNCTION__).warning("QDateTimeParser::absoluteMin() Internal error (%s, %0x)", -
2119 QString(sectionName(sn.type)).toLocal8Bit().constData(), sn.type); -
2120 return -1;
-
2121} -
2122 -
2123 -
2124 -
2125 -
2126 -
2127 -
2128 -
2129const QDateTimeParser::SectionNode &QDateTimeParser::sectionNode(int sectionIndex) const -
2130{ -
2131 if (sectionIndex < 0) {
-
2132 switch (sectionIndex) { -
2133 case FirstSectionIndex: -
2134 return first;
-
2135 case LastSectionIndex: -
2136 return last;
-
2137 case NoSectionIndex: -
2138 return none;
-
2139 } -
2140 } else if (sectionIndex < sectionNodes.size()) {
-
2141 return sectionNodes.at(sectionIndex);
-
2142 } -
2143 -
2144 QMessageLogger("tools/qdatetime.cpp", 43814376, __PRETTY_FUNCTION__).warning("QDateTimeParser::sectionNode() Internal error (%d)", -
2145 sectionIndex); -
2146 return none;
-
2147} -
2148 -
2149QDateTimeParser::Section QDateTimeParser::sectionType(int sectionIndex) const -
2150{ -
2151 return sectionNode(sectionIndex).type;
-
2152} -
2153int QDateTimeParser::sectionPos(int sectionIndex) const -
2154{ -
2155 return sectionPos(sectionNode(sectionIndex));
-
2156} -
2157 -
2158int QDateTimeParser::sectionPos(const SectionNode &sn) const -
2159{ -
2160 switch (sn.type) { -
2161 case FirstSection: return 0;
-
2162 case LastSection: return displayText().size() - 1;
-
2163 default: break;
-
2164 } -
2165 if (sn.pos == -1) {
-
2166 QMessageLogger("tools/qdatetime.cpp", 44114406, __PRETTY_FUNCTION__).warning("QDateTimeParser::sectionPos Internal error (%s)", QString(sectionName(sn.type)).toLocal8Bit().constData()); -
2167 return -1;
-
2168 } -
2169 return sn.pos;
-
2170} -
2171static QString unquote(const QString &str) -
2172{ -
2173 const QChar quote(QLatin1Char('\'')); -
2174 const QChar slash(QLatin1Char('\\')); -
2175 const QChar zero(QLatin1Char('0')); -
2176 QString ret; -
2177 QChar status(zero); -
2178 const int max = str.size(); -
2179 for (int i=0; i<max; ++i) {
-
2180 if (str.at(i) == quote) {
-
2181 if (status != quote) {
-
2182 status = quote; -
2183 } else if (!ret.isEmpty() && str.at(i - 1) == slash) {
-
2184 ret[ret.size() - 1] = quote; -
2185 } else {
-
2186 status = zero; -
2187 }
-
2188 } else { -
2189 ret += str.at(i); -
2190 }
-
2191 } -
2192 return ret;
-
2193} -
2194static inline int countRepeat(const QString &str, int index, int maxCount) -
2195{ -
2196 int count = 1; -
2197 const QChar ch(str.at(index)); -
2198 const int max = qMin(index + maxCount, str.size()); -
2199 while (index + count < max && str.at(index + count) == ch) {
-
2200 ++count; -
2201 }
-
2202 return count;
-
2203} -
2204 -
2205static inline void appendSeparator(QStringList *list, const QString &string, int from, int size, int lastQuote) -
2206{ -
2207 QString str(string.mid(from, size)); -
2208 if (lastQuote >= from)
-
2209 str = unquote(str);
-
2210 list->append(str); -
2211}
-
2212 -
2213 -
2214bool QDateTimeParser::parseFormat(const QString &newFormat) -
2215{ -
2216 const QLatin1Char quote('\''); -
2217 const QLatin1Char slash('\\'); -
2218 const QLatin1Char zero('0'); -
2219 if (newFormat == displayFormat && !newFormat.isEmpty()) {
-
2220 return true;
-
2221 } -
2222 -
2223 if (false) QMessageLogger("tools/qdatetime.cpp", 44864481, __PRETTY_FUNCTION__).debug("parseFormat: %s", newFormat.toLatin1().constData());
-
2224 -
2225 QVector<SectionNode> newSectionNodes; -
2226 Sections newDisplay = 0; -
2227 QStringList newSeparators; -
2228 int i, index = 0; -
2229 int add = 0; -
2230 QChar status(zero); -
2231 const int max = newFormat.size(); -
2232 int lastQuote = -1; -
2233 for (i = 0; i<max; ++i) {
-
2234 if (newFormat.at(i) == quote) {
-
2235 lastQuote = i; -
2236 ++add; -
2237 if (status != quote) {
-
2238 status = quote; -
2239 } else if (newFormat.at(i - 1) != slash) {
-
2240 status = zero; -
2241 }
-
2242 } else if (status != quote) {
-
2243 const char sect = newFormat.at(i).toLatin1(); -
2244 switch (sect) { -
2245 case 'H': -
2246 case 'h': -
2247 if (parserType != QVariant::Date) {
-
2248 const Section hour = (sect == 'h') ? Hour12Section : Hour24Section;
-
2249 const SectionNode sn = { hour, i - add, countRepeat(newFormat, i, 2), 0 }; -
2250 newSectionNodes.append(sn); -
2251 appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote); -
2252 i += sn.count - 1; -
2253 index = i + 1; -
2254 newDisplay |= hour; -
2255 }
-
2256 break;
-
2257 case 'm': -
2258 if (parserType != QVariant::Date) {
-
2259 const SectionNode sn = { MinuteSection, i - add, countRepeat(newFormat, i, 2), 0 }; -
2260 newSectionNodes.append(sn); -
2261 appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote); -
2262 i += sn.count - 1; -
2263 index = i + 1; -
2264 newDisplay |= MinuteSection; -
2265 }
-
2266 break;
-
2267 case 's': -
2268 if (parserType != QVariant::Date) {
-
2269 const SectionNode sn = { SecondSection, i - add, countRepeat(newFormat, i, 2), 0 }; -
2270 newSectionNodes.append(sn); -
2271 appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote); -
2272 i += sn.count - 1; -
2273 index = i + 1; -
2274 newDisplay |= SecondSection; -
2275 }
-
2276 break;
-
2277 -
2278 case 'z': -
2279 if (parserType != QVariant::Date) {
-
2280 const SectionNode sn = { MSecSection, i - add, countRepeat(newFormat, i, 3) < 3 ? 1 : 3, 0 }; -
2281 newSectionNodes.append(sn); -
2282 appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote); -
2283 i += sn.count - 1; -
2284 index = i + 1; -
2285 newDisplay |= MSecSection; -
2286 }
-
2287 break;
-
2288 case 'A': -
2289 case 'a': -
2290 if (parserType != QVariant::Date) {
-
2291 const bool cap = (sect == 'A'); -
2292 const SectionNode sn = { AmPmSection, i - add, (cap ? 1 : 0), 0 }; -
2293 newSectionNodes.append(sn); -
2294 appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote); -
2295 newDisplay |= AmPmSection; -
2296 if (i + 1 < newFormat.size()
-
2297 && newFormat.at(i+1) == (cap ? QLatin1Char('P') : QLatin1Char('p'))) {
-
2298 ++i; -
2299 }
-
2300 index = i + 1; -
2301 }
-
2302 break;
-
2303 case 'y': -
2304 if (parserType != QVariant::Time) {
-
2305 const int repeat = countRepeat(newFormat, i, 4); -
2306 if (repeat >= 2) {
-
2307 const SectionNode sn = { repeat == 4 ? YearSection : YearSection2Digits, -
2308 i - add, repeat == 4 ? 4 : 2, 0 }; -
2309 newSectionNodes.append(sn); -
2310 appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote); -
2311 i += sn.count - 1; -
2312 index = i + 1; -
2313 newDisplay |= sn.type; -
2314 }
-
2315 }
-
2316 break;
-
2317 case 'M': -
2318 if (parserType != QVariant::Time) {
-
2319 const SectionNode sn = { MonthSection, i - add, countRepeat(newFormat, i, 4), 0 }; -
2320 newSectionNodes.append(sn); -
2321 newSeparators.append(unquote(newFormat.mid(index, i - index))); -
2322 i += sn.count - 1; -
2323 index = i + 1; -
2324 newDisplay |= MonthSection; -
2325 }
-
2326 break;
-
2327 case 'd': -
2328 if (parserType != QVariant::Time) {
-
2329 const int repeat = countRepeat(newFormat, i, 4); -
2330 const Section sectionType = (repeat == 4 ? DayOfWeekSectionLong
-
2331 : (repeat == 3 ? DayOfWeekSectionShort : DaySection)); -
2332 const SectionNode sn = { sectionType, i - add, repeat, 0 }; -
2333 newSectionNodes.append(sn); -
2334 appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote); -
2335 i += sn.count - 1; -
2336 index = i + 1; -
2337 newDisplay |= sn.type; -
2338 }
-
2339 break;
-
2340 -
2341 default: -
2342 break;
-
2343 } -
2344 }
-
2345 } -
2346 if (newSectionNodes.isEmpty() && context == DateTimeEdit) {
-
2347 return false;
-
2348 } -
2349 -
2350 if ((newDisplay & (AmPmSection|Hour12Section)) == Hour12Section) {
-
2351 const int max = newSectionNodes.size(); -
2352 for (int i=0; i<max; ++i) {
-
2353 SectionNode &node = newSectionNodes[i]; -
2354 if (node.type == Hour12Section)
-
2355 node.type = Hour24Section;
-
2356 }
-
2357 }
-
2358 -
2359 if (index < newFormat.size()) {
-
2360 appendSeparator(&newSeparators, newFormat, index, index - max, lastQuote); -
2361 } else {
-
2362 newSeparators.append(QString()); -
2363 }
-
2364 -
2365 displayFormat = newFormat; -
2366 separators = newSeparators; -
2367 sectionNodes = newSectionNodes; -
2368 display = newDisplay; -
2369 last.pos = -1; -
2370 -
2371 -
2372 -
2373 -
2374 -
2375 if (false) QMessageLogger("tools/qdatetime.cpp", 46384633, __PRETTY_FUNCTION__).debug() << newFormat << displayFormat;
-
2376 if (false) QMessageLogger("tools/qdatetime.cpp", 46394634, __PRETTY_FUNCTION__).debug("separators:\n'%s'", separators.join(QLatin1String("\n")).toLatin1().constData());
-
2377 -
2378 return true;
-
2379} -
2380 -
2381 -
2382 -
2383 -
2384 -
2385 -
2386 -
2387int QDateTimeParser::sectionSize(int sectionIndex) const -
2388{ -
2389 if (sectionIndex < 0)
-
2390 return 0;
-
2391 -
2392 if (sectionIndex >= sectionNodes.size()) {
-
2393 QMessageLogger("tools/qdatetime.cpp", 46564651, __PRETTY_FUNCTION__).warning("QDateTimeParser::sectionSize Internal error (%d)", sectionIndex); -
2394 return -1;
-
2395 } -
2396 -
2397 if (sectionIndex == sectionNodes.size() - 1) {
-
2398 -
2399 -
2400 -
2401 -
2402 int sizeAdjustment = 0; -
2403 if (displayText().size() != text.size()) {
-
2404 -
2405 int preceedingZeroesAdded = 0; -
2406 if (sectionNodes.size() > 1 && context == DateTimeEdit) {
-
2407 for (QVector<SectionNode>::ConstIterator sectionIt = sectionNodes.constBegin(); -
2408 sectionIt != sectionNodes.constBegin() + sectionIndex; ++sectionIt) {
-
2409 preceedingZeroesAdded += sectionIt->zeroesAdded; -
2410 }
-
2411 }
-
2412 sizeAdjustment = preceedingZeroesAdded; -
2413 }
-
2414 -
2415 return displayText().size() + sizeAdjustment - sectionPos(sectionIndex) - separators.last().size();
-
2416 } else { -
2417 return sectionPos(sectionIndex + 1) - sectionPos(sectionIndex) -
2418 - separators.at(sectionIndex + 1).size();
-
2419 } -
2420} -
2421 -
2422 -
2423int QDateTimeParser::sectionMaxSize(Section s, int count) const -
2424{ -
2425 -
2426 int mcount = 12; -
2427 -
2428 -
2429 switch (s) { -
2430 case FirstSection: -
2431 case NoSection: -
2432 case LastSection: return 0;
-
2433 -
2434 case AmPmSection: { -
2435 const int lowerMax = qMin(getAmPmText(AmText, LowerCase).size(), -
2436 getAmPmText(PmText, LowerCase).size()); -
2437 const int upperMax = qMin(getAmPmText(AmText, UpperCase).size(), -
2438 getAmPmText(PmText, UpperCase).size()); -
2439 return qMin(4, qMin(lowerMax, upperMax));
-
2440 } -
2441 -
2442 case Hour24Section: -
2443 case Hour12Section: -
2444 case MinuteSection: -
2445 case SecondSection: -
2446 case DaySection: return 2;
-
2447 case DayOfWeekSectionShort: -
2448 case DayOfWeekSectionLong: -
2449 -
2450 -
2451 -
2452 mcount = 7; -
2453 -
2454 -
2455 case MonthSection:
-
2456 if (count <= 2)
-
2457 return 2;
-
2458 -
2459 -
2460 -
2461 -
2462 { -
2463 int ret = 0; -
2464 const QLocale l = locale(); -
2465 for (int i=1; i<=mcount; ++i) {
-
2466 const QString str = (s == MonthSection
-
2467 ? l.monthName(i, count == 4 ? QLocale::LongFormat : QLocale::ShortFormat) -
2468 : l.dayName(i, count == 4 ? QLocale::LongFormat : QLocale::ShortFormat)); -
2469 ret = qMax(str.size(), ret); -
2470 }
-
2471 return ret;
-
2472 } -
2473 -
2474 case MSecSection: return 3;
-
2475 case YearSection: return 4;
-
2476 case YearSection2Digits: return 2;
-
2477 -
2478 case CalendarPopupSection: -
2479 case Internal: -
2480 case TimeSectionMask: -
2481 case DateSectionMask: -
2482 QMessageLogger("tools/qdatetime.cpp", 47454740, __PRETTY_FUNCTION__).warning("QDateTimeParser::sectionMaxSize: Invalid section %s", -
2483 sectionName(s).toLatin1().constData()); -
2484 -
2485 case NoSectionIndex: -
2486 case FirstSectionIndex: -
2487 case LastSectionIndex: -
2488 case CalendarPopupIndex: -
2489 -
2490 break;
-
2491 } -
2492 return -1;
-
2493} -
2494 -
2495 -
2496int QDateTimeParser::sectionMaxSize(int index) const -
2497{ -
2498 const SectionNode &sn = sectionNode(index); -
2499 return sectionMaxSize(sn.type, sn.count);
-
2500} -
2501QString QDateTimeParser::sectionText(const QString &text, int sectionIndex, int index) const -
2502{ -
2503 const SectionNode &sn = sectionNode(sectionIndex); -
2504 switch (sn.type) { -
2505 case NoSectionIndex: -
2506 case FirstSectionIndex: -
2507 case LastSectionIndex: -
2508 return QString();
-
2509 default: break;
-
2510 } -
2511 -
2512 return text.mid(index, sectionSize(sectionIndex));
-
2513} -
2514 -
2515QString QDateTimeParser::sectionText(int sectionIndex) const -
2516{ -
2517 const SectionNode &sn = sectionNode(sectionIndex); -
2518 switch (sn.type) { -
2519 case NoSectionIndex: -
2520 case FirstSectionIndex: -
2521 case LastSectionIndex: -
2522 return QString();
-
2523 default: break;
-
2524 } -
2525 -
2526 return displayText().mid(sn.pos, sectionSize(sectionIndex));
-
2527} -
2528int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionIndex, -
2529 QString &text, int &cursorPosition, int index, -
2530 State &state, int *usedptr) const -
2531{ -
2532 state = Invalid; -
2533 int num = 0; -
2534 const SectionNode &sn = sectionNode(sectionIndex); -
2535 if ((sn.type & Internal) == Internal) {
-
2536 QMessageLogger("tools/qdatetime.cpp", 48194814, __PRETTY_FUNCTION__).warning("QDateTimeParser::parseSection Internal error (%s %d)", -
2537 QString(sectionName(sn.type)).toLocal8Bit().constData(), sectionIndex); -
2538 return -1;
-
2539 } -
2540 -
2541 const int sectionmaxsize = sectionMaxSize(sectionIndex); -
2542 QString sectiontext = text.mid(index, sectionmaxsize); -
2543 int sectiontextSize = sectiontext.size(); -
2544 -
2545 if (false) QMessageLogger("tools/qdatetime.cpp", 48284823, __PRETTY_FUNCTION__).debug() << "sectionValue for" << sectionName(sn.type)
-
2546 << "with text" << text << "and st" << sectiontext -
2547 << text.mid(index, sectionmaxsize) -
2548 << index;
-
2549 -
2550 int used = 0; -
2551 switch (sn.type) { -
2552 case AmPmSection: { -
2553 const int ampm = findAmPm(sectiontext, sectionIndex, &used); -
2554 switch (ampm) { -
2555 case AM: -
2556 case PM: -
2557 num = ampm; -
2558 state = Acceptable; -
2559 break;
-
2560 case PossibleAM: -
2561 case PossiblePM: -
2562 num = ampm - 2; -
2563 state = Intermediate; -
2564 break;
-
2565 case PossibleBoth: -
2566 num = 0; -
2567 state = Intermediate; -
2568 break;
-
2569 case Neither: -
2570 state = Invalid; -
2571 if (false) QMessageLogger("tools/qdatetime.cpp", 48544849, __PRETTY_FUNCTION__).debug() << "invalid because findAmPm(" << sectiontext << ") returned -1";
-
2572 break;
-
2573 default: -
2574 if (false) QMessageLogger("tools/qdatetime.cpp", 48574852, __PRETTY_FUNCTION__).debug("This should never happen (findAmPm returned %d)", ampm);
-
2575 break;
-
2576 } -
2577 if (state != Invalid) {
-
2578 QString str = text; -
2579 text.replace(index, used, sectiontext.left(used)); -
2580 }
-
2581 break; }
-
2582 case MonthSection: -
2583 case DayOfWeekSectionShort: -
2584 case DayOfWeekSectionLong: -
2585 if (sn.count >= 3) {
-
2586 if (sn.type == MonthSection) {
-
2587 int min = 1; -
2588 const QDate minDate = getMinimum().date(); -
2589 if (currentValue.date().year() == minDate.year()) {
-
2590 min = minDate.month(); -
2591 }
-
2592 num = findMonth(sectiontext.toLower(), min, sectionIndex, &sectiontext, &used); -
2593 } else {
-
2594 num = findDay(sectiontext.toLower(), 1, sectionIndex, &sectiontext, &used); -
2595 }
-
2596 -
2597 if (num != -1) {
-
2598 state = (used == sectiontext.size() ? Acceptable : Intermediate);
-
2599 QString str = text; -
2600 text.replace(index, used, sectiontext.left(used)); -
2601 } else {
-
2602 state = Intermediate; -
2603 }
-
2604 break; }
-
2605 -
2606 case DaySection: -
2607 case YearSection: -
2608 case YearSection2Digits: -
2609 case Hour12Section: -
2610 case Hour24Section: -
2611 case MinuteSection: -
2612 case SecondSection: -
2613 case MSecSection: { -
2614 if (sectiontextSize == 0) {
-
2615 num = 0; -
2616 used = 0; -
2617 state = Intermediate; -
2618 } else {
-
2619 const int absMax = absoluteMax(sectionIndex); -
2620 QLocale loc; -
2621 bool ok = true; -
2622 int last = -1; -
2623 used = -1; -
2624 -
2625 QString digitsStr(sectiontext); -
2626 for (int i = 0; i < sectiontextSize; ++i) {
-
2627 if (digitsStr.at(i).isSpace()) {
-
2628 sectiontextSize = i; -
2629 break;
-
2630 } -
2631 }
-
2632 -
2633 const int max = qMin(sectionmaxsize, sectiontextSize); -
2634 for (int digits = max; digits >= 1; --digits) {
-
2635 digitsStr.truncate(digits); -
2636 int tmp = (int)loc.toUInt(digitsStr, &ok); -
2637 if (ok && sn.type == Hour12Section) {
-
2638 if (tmp > 12) {
-
2639 tmp = -1; -
2640 ok = false; -
2641 } else if (tmp == 12) {
-
2642 tmp = 0; -
2643 }
-
2644 } -
2645 if (ok && tmp <= absMax) {
-
2646 if (false) QMessageLogger("tools/qdatetime.cpp", 49294924, __PRETTY_FUNCTION__).debug() << sectiontext.left(digits) << tmp << digits;
-
2647 last = tmp; -
2648 used = digits; -
2649 break;
-
2650 } -
2651 }
-
2652 -
2653 if (last == -1) {
-
2654 QChar first(sectiontext.at(0)); -
2655 if (separators.at(sectionIndex + 1).startsWith(first)) {
-
2656 used = 0; -
2657 state = Intermediate; -
2658 } else {
-
2659 state = Invalid; -
2660 if (false) QMessageLogger("tools/qdatetime.cpp", 49434938, __PRETTY_FUNCTION__).debug() << "invalid because" << sectiontext << "can't become a uint" << last << ok;
-
2661 }
-
2662 } else { -
2663 num += last; -
2664 const FieldInfo fi = fieldInfo(sectionIndex); -
2665 const bool done = (used == sectionmaxsize); -
2666 if (!done && fi & Fraction) {
-
2667 for (int i=used; i<sectionmaxsize; ++i) {
-
2668 num *= 10; -
2669 }
-
2670 }
-
2671 const int absMin = absoluteMin(sectionIndex); -
2672 if (num < absMin) {
-
2673 state = done ? Invalid : Intermediate;
-
2674 if (done)
-
2675 if (false) QMessageLogger("tools/qdatetime.cpp", 49584953, __PRETTY_FUNCTION__).debug() << "invalid because" << num << "is less than absoluteMin" << absMin;
-
2676 } else if (num > absMax) {
-
2677 state = Intermediate; -
2678 } else if (!done && (fi & (FixedWidth|Numeric)) == (FixedWidth|Numeric)) {
-
2679 if (skipToNextSection(sectionIndex, currentValue, digitsStr)) {
-
2680 state = Acceptable; -
2681 const int missingZeroes = sectionmaxsize - digitsStr.size(); -
2682 text.insert(index, QString().fill(QLatin1Char('0'), missingZeroes)); -
2683 used = sectionmaxsize; -
2684 cursorPosition += missingZeroes; -
2685 ++(const_cast<QDateTimeParser*>(this)->sectionNodes[sectionIndex].zeroesAdded); -
2686 } else {
-
2687 state = Intermediate;; -
2688 }
-
2689 } else { -
2690 state = Acceptable; -
2691 }
-
2692 } -
2693 } -
2694 break; }
-
2695 default: -
2696 QMessageLogger("tools/qdatetime.cpp", 49794974, __PRETTY_FUNCTION__).warning("QDateTimeParser::parseSection Internal error (%s %d)", -
2697 QString(sectionName(sn.type)).toLocal8Bit().constData(), sectionIndex); -
2698 return -1;
-
2699 } -
2700 -
2701 if (usedptr)
-
2702 *usedptr = used;
-
2703 -
2704 return (state != Invalid ? num : -1);
-
2705} -
2706 -
2707 -
2708 -
2709 -
2710 -
2711 -
2712 -
2713QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPosition, -
2714 const QDateTime &currentValue, bool fixup) const -
2715{ -
2716 const QDateTime minimum = getMinimum(); -
2717 const QDateTime maximum = getMaximum(); -
2718 -
2719 State state = Acceptable; -
2720 -
2721 QDateTime newCurrentValue; -
2722 int pos = 0; -
2723 bool conflicts = false; -
2724 const int sectionNodesCount = sectionNodes.size(); -
2725 -
2726 if (false) QMessageLogger("tools/qdatetime.cpp", 50095004, __PRETTY_FUNCTION__).debug() << "parse" << input;
-
2727 { -
2728 int year, month, day, hour12, hour, minute, second, msec, ampm, dayofweek, year2digits; -
2729 getDateFromJulianDay(currentValue.date().toJulianDay(), &year, &month, &day); -
2730 year2digits = year % 100; -
2731 hour = currentValue.time().hour(); -
2732 hour12 = -1; -
2733 minute = currentValue.time().minute(); -
2734 second = currentValue.time().second(); -
2735 msec = currentValue.time().msec(); -
2736 dayofweek = currentValue.date().dayOfWeek(); -
2737 -
2738 ampm = -1; -
2739 Sections isSet = NoSection; -
2740 int num; -
2741 State tmpstate; -
2742 -
2743 for (int index=0; state != Invalid && index<sectionNodesCount; ++index) {
-
2744 if (QStringRef(&input, pos, separators.at(index).size()) != separators.at(index)) {
-
2745 if (false) QMessageLogger("tools/qdatetime.cpp", 50285023, __PRETTY_FUNCTION__).debug() << "invalid because" << input.mid(pos, separators.at(index).size())
-
2746 << "!=" << separators.at(index) -
2747 << index << pos << currentSectionIndex;
-
2748 state = Invalid; -
2749 goto end;
-
2750 } -
2751 pos += separators.at(index).size(); -
2752 sectionNodes[index].pos = pos; -
2753 int *current = 0; -
2754 const SectionNode sn = sectionNodes.at(index); -
2755 int used; -
2756 -
2757 num = parseSection(currentValue, index, input, cursorPosition, pos, tmpstate, &used); -
2758 if (false) QMessageLogger("tools/qdatetime.cpp", 50415036, __PRETTY_FUNCTION__).debug() << "sectionValue" << sectionName(sectionType(index)) << input
-
2759 << "pos" << pos << "used" << used << stateName(tmpstate);
-
2760 if (fixup && tmpstate == Intermediate && used < sn.count) {
-
2761 const FieldInfo fi = fieldInfo(index); -
2762 if ((fi & (Numeric|FixedWidth)) == (Numeric|FixedWidth)) {
-
2763 const QString newText = QString::fromLatin1("%1").arg(num, sn.count, 10, QLatin1Char('0')); -
2764 input.replace(pos, used, newText); -
2765 used = sn.count; -
2766 }
-
2767 }
-
2768 pos += qMax(0, used); -
2769 -
2770 state = qMin<State>(state, tmpstate); -
2771 if (state == Intermediate && context == FromString) {
-
2772 state = Invalid; -
2773 break;
-
2774 } -
2775 -
2776 if (false) QMessageLogger("tools/qdatetime.cpp", 50595054, __PRETTY_FUNCTION__).debug() << index << sectionName(sectionType(index)) << "is set to"
-
2777 << pos << "state is" << stateName(state);
-
2778 -
2779 -
2780 if (state != Invalid) {
-
2781 switch (sn.type) { -
2782 case Hour24Section: current = &hour; break;
-
2783 case Hour12Section: current = &hour12; break;
-
2784 case MinuteSection: current = &minute; break;
-
2785 case SecondSection: current = &second; break;
-
2786 case MSecSection: current = &msec; break;
-
2787 case YearSection: current = &year; break;
-
2788 case YearSection2Digits: current = &year2digits; break;
-
2789 case MonthSection: current = &month; break;
-
2790 case DayOfWeekSectionShort: -
2791 case DayOfWeekSectionLong: current = &dayofweek; break;
-
2792 case DaySection: current = &day; num = qMax<int>(1, num); break;
-
2793 case AmPmSection: current = &ampm; break;
-
2794 default: -
2795 QMessageLogger("tools/qdatetime.cpp", 50785073, __PRETTY_FUNCTION__).warning("QDateTimeParser::parse Internal error (%s)", -
2796 QString(sectionName(sn.type)).toLocal8Bit().constData()); -
2797 break;
-
2798 } -
2799 if (!current) {
-
2800 QMessageLogger("tools/qdatetime.cpp", 50835078, __PRETTY_FUNCTION__).warning("QDateTimeParser::parse Internal error 2"); -
2801 return StateNode();
-
2802 } -
2803 if (isSet & sn.type && *current != num) {
-
2804 if (false) QMessageLogger("tools/qdatetime.cpp", 50875082, __PRETTY_FUNCTION__).debug() << "CONFLICT " << sectionName(sn.type) << *current << num;
-
2805 conflicts = true; -
2806 if (index != currentSectionIndex || num == -1) {
-
2807 continue;
-
2808 } -
2809 }
-
2810 if (num != -1)
-
2811 *current = num;
-
2812 isSet |= sn.type; -
2813 }
-
2814 }
-
2815 -
2816 if (state != Invalid && QStringRef(&input, pos, input.size() - pos) != separators.last()) {
-
2817 if (false) QMessageLogger("tools/qdatetime.cpp", 51005095, __PRETTY_FUNCTION__).debug() << "invalid because" << input.mid(pos)
-
2818 << "!=" << separators.last() << pos;
-
2819 state = Invalid; -
2820 }
-
2821 -
2822 if (state != Invalid) {
-
2823 if (parserType != QVariant::Time) {
-
2824 if (year % 100 != year2digits) {
-
2825 switch (isSet & (YearSection2Digits|YearSection)) { -
2826 case YearSection2Digits: -
2827 year = (year / 100) * 100; -
2828 year += year2digits; -
2829 break;
-
2830 case ((uint)YearSection2Digits|(uint)YearSection): { -
2831 conflicts = true; -
2832 const SectionNode &sn = sectionNode(currentSectionIndex); -
2833 if (sn.type == YearSection2Digits) {
-
2834 year = (year / 100) * 100; -
2835 year += year2digits; -
2836 }
-
2837 break; }
-
2838 default: -
2839 break;
-
2840 } -
2841 }
-
2842 -
2843 const QDate date(year, month, day); -
2844 const int diff = dayofweek - date.dayOfWeek(); -
2845 if (diff != 0 && state == Acceptable && isSet & (DayOfWeekSectionShort|DayOfWeekSectionLong)) {
-
2846 conflicts = isSet & DaySection; -
2847 const SectionNode &sn = sectionNode(currentSectionIndex); -
2848 if (sn.type & (DayOfWeekSectionShort|DayOfWeekSectionLong) || currentSectionIndex == -1) {
-
2849 -
2850 day += diff; -
2851 if (day <= 0) {
-
2852 day += 7; -
2853 } else if (day > date.daysInMonth()) {
-
2854 day -= 7; -
2855 }
-
2856 if (false) QMessageLogger("tools/qdatetime.cpp", 51395134, __PRETTY_FUNCTION__).debug() << year << month << day << dayofweek
-
2857 << diff << QDate(year, month, day).dayOfWeek();
-
2858 }
-
2859 }
-
2860 bool needfixday = false; -
2861 if (sectionType(currentSectionIndex) & (DaySection|DayOfWeekSectionShort|DayOfWeekSectionLong)) {
-
2862 cachedDay = day; -
2863 } else if (cachedDay > day) {
-
2864 day = cachedDay; -
2865 needfixday = true; -
2866 }
-
2867 -
2868 if (!QDate::isValid(year, month, day)) {
-
2869 if (day < 32) {
-
2870 cachedDay = day; -
2871 }
-
2872 if (day > 28 && QDate::isValid(year, month, 1)) {
-
2873 needfixday = true; -
2874 }
-
2875 }
-
2876 if (needfixday) {
-
2877 if (context == FromString) {
-
2878 state = Invalid; -
2879 goto end;
-
2880 } -
2881 if (state == Acceptable && fixday) {
-
2882 day = qMin<int>(day, QDate(year, month, 1).daysInMonth()); -
2883 -
2884 const QLocale loc = locale(); -
2885 for (int i=0; i<sectionNodesCount; ++i) {
-
2886 const Section thisSectionType = sectionType(i); -
2887 if (thisSectionType & (DaySection)) {
-
2888 input.replace(sectionPos(i), sectionSize(i), loc.toString(day)); -
2889 } else if (thisSectionType & (DayOfWeekSectionShort|DayOfWeekSectionLong)) {
-
2890 const int dayOfWeek = QDate(year, month, day).dayOfWeek(); -
2891 const QLocale::FormatType dayFormat = (thisSectionType == DayOfWeekSectionShort
-
2892 ? QLocale::ShortFormat : QLocale::LongFormat); -
2893 const QString dayName(loc.dayName(dayOfWeek, dayFormat)); -
2894 input.replace(sectionPos(i), sectionSize(i), dayName); -
2895 }
-
2896 } -
2897 } else {
-
2898 state = qMin(Intermediate, state); -
2899 }
-
2900 } -
2901 }
-
2902 -
2903 if (parserType != QVariant::Date) {
-
2904 if (isSet & Hour12Section) {
-
2905 const bool hasHour = isSet & Hour24Section; -
2906 if (ampm == -1) {
-
2907 if (hasHour) {
-
2908 ampm = (hour < 12 ? 0 : 1);
-
2909 } else {
-
2910 ampm = 0; -
2911 }
-
2912 } -
2913 hour12 = (ampm == 0 ? hour12 % 12 : (hour12 % 12) + 12);
-
2914 if (!hasHour) {
-
2915 hour = hour12; -
2916 } else if (hour != hour12) {
-
2917 conflicts = true; -
2918 }
-
2919 } else if (ampm != -1) {
-
2920 if (!(isSet & (Hour24Section))) {
-
2921 hour = (12 * ampm); -
2922 } else if ((ampm == 0) != (hour < 12)) {
-
2923 conflicts = true; -
2924 }
-
2925 } -
2926 -
2927 } -
2928 -
2929 newCurrentValue = QDateTime(QDate(year, month, day), QTime(hour, minute, second, msec), spec); -
2930 if (false) QMessageLogger("tools/qdatetime.cpp", 52135208, __PRETTY_FUNCTION__).debug() << year << month << day << hour << minute << second << msec;
-
2931 }
-
2932 if (false) QMessageLogger("tools/qdatetime.cpp", 52155210, __PRETTY_FUNCTION__).debug("'%s' => '%s'(%s)", input.toLatin1().constData(),
-
2933 newCurrentValue.toString(QLatin1String("yyyy/MM/dd hh:mm:ss.zzz")).toLatin1().constData(), -
2934 stateName(state).toLatin1().constData());
-
2935 } -
2936end:
-
2937 if (newCurrentValue.isValid()) {
-
2938 if (context != FromString && state != Invalid && newCurrentValue < minimum) {
-
2939 const QLatin1Char space(' '); -
2940 if (newCurrentValue >= minimum)
-
2941 QMessageLogger("tools/qdatetime.cpp", 52245219, __PRETTY_FUNCTION__).warning("QDateTimeParser::parse Internal error 3 (%s %s)", -
2942 QString(newCurrentValue.toString()).toLocal8Bit().constData(), QString(minimum.toString()).toLocal8Bit().constData());
-
2943 -
2944 bool done = false; -
2945 state = Invalid; -
2946 for (int i=0; i<sectionNodesCount && !done; ++i) {
-
2947 const SectionNode &sn = sectionNodes.at(i); -
2948 QString t = sectionText(input, i, sn.pos).toLower(); -
2949 if ((t.size() < sectionMaxSize(i) && (((int)fieldInfo(i) & (FixedWidth|Numeric)) != Numeric))
-
2950 || t.contains(space)) {
-
2951 switch (sn.type) { -
2952 case AmPmSection: -
2953 switch (findAmPm(t, i)) { -
2954 case AM: -
2955 case PM: -
2956 state = Acceptable; -
2957 done = true; -
2958 break;
-
2959 case Neither: -
2960 state = Invalid; -
2961 done = true; -
2962 break;
-
2963 case PossibleAM: -
2964 case PossiblePM: -
2965 case PossibleBoth: { -
2966 const QDateTime copy(newCurrentValue.addSecs(12 * 60 * 60)); -
2967 if (copy >= minimum && copy <= maximum) {
-
2968 state = Intermediate; -
2969 done = true; -
2970 }
-
2971 break; }
-
2972 } -
2973 case MonthSection:
-
2974 if (sn.count >= 3) {
-
2975 int tmp = newCurrentValue.date().month(); -
2976 -
2977 while ((tmp = findMonth(t, tmp + 1, i)) != -1) {
-
2978 const QDateTime copy(newCurrentValue.addMonths(tmp - newCurrentValue.date().month())); -
2979 if (copy >= minimum && copy <= maximum)
-
2980 break;
-
2981 }
-
2982 if (tmp == -1) {
-
2983 break;
-
2984 } -
2985 state = Intermediate; -
2986 done = true; -
2987 break;
-
2988 } -
2989 -
2990 default: { -
2991 int toMin; -
2992 int toMax; -
2993 -
2994 if (sn.type & TimeSectionMask) {
-
2995 if (newCurrentValue.daysTo(minimum) != 0) {
-
2996 break;
-
2997 } -
2998 toMin = newCurrentValue.time().msecsTo(minimum.time()); -
2999 if (newCurrentValue.daysTo(maximum) > 0) {
-
3000 toMax = -1; -
3001 } else {
-
3002 toMax = newCurrentValue.time().msecsTo(maximum.time()); -
3003 }
-
3004 } else { -
3005 toMin = newCurrentValue.daysTo(minimum); -
3006 toMax = newCurrentValue.daysTo(maximum); -
3007 }
-
3008 const int maxChange = QDateTimeParser::maxChange(i); -
3009 if (toMin > maxChange) {
-
3010 if (false) QMessageLogger("tools/qdatetime.cpp", 52935288, __PRETTY_FUNCTION__).debug() << "invalid because toMin > maxChange" << toMin
-
3011 << maxChange << t << newCurrentValue << minimum;
-
3012 state = Invalid; -
3013 done = true; -
3014 break;
-
3015 } else if (toMax > maxChange) {
-
3016 toMax = -1; -
3017 }
-
3018 -
3019 const int min = getDigit(minimum, i); -
3020 if (min == -1) {
-
3021 QMessageLogger("tools/qdatetime.cpp", 53045299, __PRETTY_FUNCTION__).warning("QDateTimeParser::parse Internal error 4 (%s)", -
3022 QString(sectionName(sn.type)).toLocal8Bit().constData()); -
3023 state = Invalid; -
3024 done = true; -
3025 break;
-
3026 } -
3027 -
3028 int max = toMax != -1 ? getDigit(maximum, i) : absoluteMax(i, newCurrentValue);
-
3029 int pos = cursorPosition - sn.pos; -
3030 if (pos < 0 || pos >= t.size())
-
3031 pos = -1;
-
3032 if (!potentialValue(t.simplified(), min, max, i, newCurrentValue, pos)) {
-
3033 if (false) QMessageLogger("tools/qdatetime.cpp", 53165311, __PRETTY_FUNCTION__).debug() << "invalid because potentialValue(" << t.simplified() << min << max
-
3034 << sectionName(sn.type) << "returned" << toMax << toMin << pos;
-
3035 state = Invalid; -
3036 done = true; -
3037 break;
-
3038 } -
3039 state = Intermediate; -
3040 done = true; -
3041 break; }
-
3042 } -
3043 }
-
3044 }
-
3045 } else {
-
3046 if (context == FromString) {
-
3047 -
3048 qt_noop(); -
3049 if (newCurrentValue.date().toJulianDay() > 4642999)
-
3050 state = Invalid;
-
3051 } else {
-
3052 if (newCurrentValue > getMaximum())
-
3053 state = Invalid;
-
3054 }
-
3055 -
3056 if (false) QMessageLogger("tools/qdatetime.cpp", 53395334, __PRETTY_FUNCTION__).debug() << "not checking intermediate because newCurrentValue is" << newCurrentValue << getMinimum() << getMaximum();
-
3057 }
-
3058 } -
3059 StateNode node; -
3060 node.input = input; -
3061 node.state = state; -
3062 node.conflicts = conflicts; -
3063 node.value = newCurrentValue.toTimeSpec(spec); -
3064 text = input; -
3065 return node;
-
3066} -
3067int QDateTimeParser::findMonth(const QString &str1, int startMonth, int sectionIndex, -
3068 QString *usedMonth, int *used) const -
3069{ -
3070 int bestMatch = -1; -
3071 int bestCount = 0; -
3072 if (!str1.isEmpty()) {
-
3073 const SectionNode &sn = sectionNode(sectionIndex); -
3074 if (sn.type != MonthSection) {
-
3075 QMessageLogger("tools/qdatetime.cpp", 53675362, __PRETTY_FUNCTION__).warning("QDateTimeParser::findMonth Internal error"); -
3076 return -1;
-
3077 } -
3078 -
3079 QLocale::FormatType type = sn.count == 3 ? QLocale::ShortFormat : QLocale::LongFormat;
-
3080 QLocale l = locale(); -
3081 -
3082 for (int month=startMonth; month<=12; ++month) {
-
3083 QString str2 = l.monthName(month, type).toLower(); -
3084 -
3085 if (str1.startsWith(str2)) {
-
3086 if (used) {
-
3087 if (false) QMessageLogger("tools/qdatetime.cpp", 53795374, __PRETTY_FUNCTION__).debug() << "used is set to" << str2.size();
-
3088 *used = str2.size(); -
3089 }
-
3090 if (usedMonth)
-
3091 *usedMonth = l.monthName(month, type);
-
3092 -
3093 return month;
-
3094 } -
3095 if (context == FromString)
-
3096 continue;
-
3097 -
3098 const int limit = qMin(str1.size(), str2.size()); -
3099 -
3100 if (false) QMessageLogger("tools/qdatetime.cpp", 53925387, __PRETTY_FUNCTION__).debug() << "limit is" << limit << str1 << str2;
-
3101 bool equal = true; -
3102 for (int i=0; i<limit; ++i) {
-
3103 if (str1.at(i) != str2.at(i)) {
-
3104 equal = false; -
3105 if (i > bestCount) {
-
3106 bestCount = i; -
3107 bestMatch = month; -
3108 }
-
3109 break;
-
3110 } -
3111 }
-
3112 if (equal) {
-
3113 if (used)
-
3114 *used = limit;
-
3115 if (usedMonth)
-
3116 *usedMonth = l.monthName(month, type);
-
3117 return month;
-
3118 } -
3119 }
-
3120 if (usedMonth && bestMatch != -1)
-
3121 *usedMonth = l.monthName(bestMatch, type);
-
3122 }
-
3123 if (used) {
-
3124 if (false) QMessageLogger("tools/qdatetime.cpp", 54165411, __PRETTY_FUNCTION__).debug() << "used is set to" << bestCount;
-
3125 *used = bestCount; -
3126 }
-
3127 return bestMatch;
-
3128} -
3129 -
3130int QDateTimeParser::findDay(const QString &str1, int startDay, int sectionIndex, QString *usedDay, int *used) const -
3131{ -
3132 int bestMatch = -1; -
3133 int bestCount = 0; -
3134 if (!str1.isEmpty()) {
-
3135 const SectionNode &sn = sectionNode(sectionIndex); -
3136 if (!(sn.type & (DaySection|DayOfWeekSectionShort|DayOfWeekSectionLong))) {
-
3137 QMessageLogger("tools/qdatetime.cpp", 54295424, __PRETTY_FUNCTION__).warning("QDateTimeParser::findDay Internal error"); -
3138 return -1;
-
3139 } -
3140 const QLocale l = locale(); -
3141 for (int day=startDay; day<=7; ++day) {
-
3142 const QString str2 = l.dayName(day, sn.count == 4 ? QLocale::LongFormat : QLocale::ShortFormat); -
3143 -
3144 if (str1.startsWith(str2.toLower())) {
-
3145 if (used)
-
3146 *used = str2.size();
-
3147 if (usedDay) {
-
3148 *usedDay = str2; -
3149 }
-
3150 return day;
-
3151 } -
3152 if (context == FromString)
-
3153 continue;
-
3154 -
3155 const int limit = qMin(str1.size(), str2.size()); -
3156 bool found = true; -
3157 for (int i=0; i<limit; ++i) {
-
3158 if (str1.at(i) != str2.at(i) && !str1.at(i).isSpace()) {
-
3159 if (i > bestCount) {
-
3160 bestCount = i; -
3161 bestMatch = day; -
3162 }
-
3163 found = false; -
3164 break;
-
3165 } -
3166 -
3167 }
-
3168 if (found) {
-
3169 if (used)
-
3170 *used = limit;
-
3171 if (usedDay)
-
3172 *usedDay = str2;
-
3173 -
3174 return day;
-
3175 } -
3176 }
-
3177 if (usedDay && bestMatch != -1) {
-
3178 *usedDay = l.dayName(bestMatch, sn.count == 4 ? QLocale::LongFormat : QLocale::ShortFormat); -
3179 }
-
3180 }
-
3181 if (used)
-
3182 *used = bestCount;
-
3183 -
3184 return bestMatch;
-
3185} -
3186int QDateTimeParser::findAmPm(QString &str, int index, int *used) const -
3187{ -
3188 const SectionNode &s = sectionNode(index); -
3189 if (s.type != AmPmSection) {
-
3190 QMessageLogger("tools/qdatetime.cpp", 54975492, __PRETTY_FUNCTION__).warning("QDateTimeParser::findAmPm Internal error"); -
3191 return -1;
-
3192 } -
3193 if (used)
-
3194 *used = str.size();
-
3195 if (str.trimmed().isEmpty()) {
-
3196 return PossibleBoth;
-
3197 } -
3198 const QLatin1Char space(' '); -
3199 int size = sectionMaxSize(index); -
3200 -
3201 enum { -
3202 amindex = 0, -
3203 pmindex = 1 -
3204 }; -
3205 QString ampm[2]; -
3206 ampm[amindex] = getAmPmText(AmText, s.count == 1 ? UpperCase : LowerCase); -
3207 ampm[pmindex] = getAmPmText(PmText, s.count == 1 ? UpperCase : LowerCase); -
3208 for (int i=0; i<2; ++i)
-
3209 ampm[i].truncate(size);
-
3210 -
3211 if (false) QMessageLogger("tools/qdatetime.cpp", 55185513, __PRETTY_FUNCTION__).debug() << "findAmPm" << str << ampm[0] << ampm[1];
-
3212 -
3213 if (str.indexOf(ampm[amindex], 0, Qt::CaseInsensitive) == 0) {
-
3214 str = ampm[amindex]; -
3215 return AM;
-
3216 } else if (str.indexOf(ampm[pmindex], 0, Qt::CaseInsensitive) == 0) {
-
3217 str = ampm[pmindex]; -
3218 return PM;
-
3219 } else if (context == FromString || (str.count(space) == 0 && str.size() >= size)) {
-
3220 return Neither;
-
3221 } -
3222 size = qMin(size, str.size()); -
3223 -
3224 bool broken[2] = {false, false}; -
3225 for (int i=0; i<size; ++i) {
-
3226 if (str.at(i) != space) {
-
3227 for (int j=0; j<2; ++j) {
-
3228 if (!broken[j]) {
-
3229 int index = ampm[j].indexOf(str.at(i)); -
3230 if (false) QMessageLogger("tools/qdatetime.cpp", 55375532, __PRETTY_FUNCTION__).debug() << "looking for" << str.at(i)
-
3231 << "in" << ampm[j] << "and got" << index;
-
3232 if (index == -1) {
-
3233 if (str.at(i).category() == QChar::Letter_Uppercase) {
-
3234 index = ampm[j].indexOf(str.at(i).toLower()); -
3235 if (false) QMessageLogger("tools/qdatetime.cpp", 55425537, __PRETTY_FUNCTION__).debug() << "trying with" << str.at(i).toLower()
-
3236 << "in" << ampm[j] << "and got" << index;
-
3237 } else if (str.at(i).category() == QChar::Letter_Lowercase) {
-
3238 index = ampm[j].indexOf(str.at(i).toUpper()); -
3239 if (false) QMessageLogger("tools/qdatetime.cpp", 55465541, __PRETTY_FUNCTION__).debug() << "trying with" << str.at(i).toUpper()
-
3240 << "in" << ampm[j] << "and got" << index;
-
3241 }
-
3242 if (index == -1) {
-
3243 broken[j] = true; -
3244 if (broken[amindex] && broken[pmindex]) {
-
3245 if (false) QMessageLogger("tools/qdatetime.cpp", 55525547, __PRETTY_FUNCTION__).debug() << str << "didn't make it";
-
3246 return Neither;
-
3247 } -
3248 continue;
-
3249 } else { -
3250 str[i] = ampm[j].at(index); -
3251 }
-
3252 } -
3253 ampm[j].remove(index, 1); -
3254 }
-
3255 }
-
3256 }
-
3257 }
-
3258 if (!broken[pmindex] && !broken[amindex])
-
3259 return PossibleBoth;
-
3260 return (!broken[amindex] ? PossibleAM : PossiblePM);
-
3261} -
3262 -
3263 -
3264 -
3265 -
3266 -
3267 -
3268int QDateTimeParser::maxChange(int index) const -
3269{ -
3270 const SectionNode &sn = sectionNode(index); -
3271 switch (sn.type) { -
3272 -
3273 case MSecSection: return 999;
-
3274 case SecondSection: return 59 * 1000;
-
3275 case MinuteSection: return 59 * 60 * 1000;
-
3276 case Hour24Section: case Hour12Section: return 59 * 60 * 60 * 1000;
-
3277 -
3278 -
3279 case DayOfWeekSectionShort: -
3280 case DayOfWeekSectionLong: return 7;
-
3281 case DaySection: return 30;
-
3282 case MonthSection: return 365 - 31;
-
3283 case YearSection: return 9999 * 365;
-
3284 case YearSection2Digits: return 100 * 365;
-
3285 default: -
3286 QMessageLogger("tools/qdatetime.cpp", 55935588, __PRETTY_FUNCTION__).warning("QDateTimeParser::maxChange() Internal error (%s)", -
3287 QString(sectionName(sectionType(index))).toLocal8Bit().constData()); -
3288 }
-
3289 -
3290 return -1;
-
3291} -
3292 -
3293QDateTimeParser::FieldInfo QDateTimeParser::fieldInfo(int index) const -
3294{ -
3295 FieldInfo ret = 0; -
3296 const SectionNode &sn = sectionNode(index); -
3297 const Section s = sn.type; -
3298 switch (s) { -
3299 case MSecSection: -
3300 ret |= Fraction; -
3301 -
3302 case SecondSection:
-
3303 case MinuteSection: -
3304 case Hour24Section: -
3305 case Hour12Section: -
3306 case YearSection: -
3307 case YearSection2Digits: -
3308 ret |= Numeric; -
3309 if (s != YearSection) {
-
3310 ret |= AllowPartial; -
3311 }
-
3312 if (sn.count != 1) {
-
3313 ret |= FixedWidth; -
3314 }
-
3315 break;
-
3316 case MonthSection: -
3317 case DaySection: -
3318 switch (sn.count) { -
3319 case 2: -
3320 ret |= FixedWidth; -
3321 -
3322 case 1:
-
3323 ret |= (Numeric|AllowPartial); -
3324 break;
-
3325 } -
3326 break;
-
3327 case DayOfWeekSectionShort: -
3328 case DayOfWeekSectionLong: -
3329 if (sn.count == 3)
-
3330 ret |= FixedWidth;
-
3331 break;
-
3332 case AmPmSection: -
3333 ret |= FixedWidth; -
3334 break;
-
3335 default: -
3336 QMessageLogger("tools/qdatetime.cpp", 56435638, __PRETTY_FUNCTION__).warning("QDateTimeParser::fieldInfo Internal error 2 (%d %s %d)", -
3337 index, QString(sectionName(sn.type)).toLocal8Bit().constData(), sn.count); -
3338 break;
-
3339 } -
3340 return ret;
-
3341} -
3342QString QDateTimeParser::sectionFormat(int index) const -
3343{ -
3344 const SectionNode &sn = sectionNode(index); -
3345 return sectionFormat(sn.type, sn.count);
-
3346} -
3347 -
3348QString QDateTimeParser::sectionFormat(Section s, int count) const -
3349{ -
3350 QChar fillChar; -
3351 switch (s) { -
3352 case AmPmSection: return count == 1 ? QLatin1String("AP") : QLatin1String("ap");
-
3353 case MSecSection: fillChar = QLatin1Char('z'); break;
-
3354 case SecondSection: fillChar = QLatin1Char('s'); break;
-
3355 case MinuteSection: fillChar = QLatin1Char('m'); break;
-
3356 case Hour24Section: fillChar = QLatin1Char('H'); break;
-
3357 case Hour12Section: fillChar = QLatin1Char('h'); break;
-
3358 case DayOfWeekSectionShort: -
3359 case DayOfWeekSectionLong: -
3360 case DaySection: fillChar = QLatin1Char('d'); break;
-
3361 case MonthSection: fillChar = QLatin1Char('M'); break;
-
3362 case YearSection2Digits: -
3363 case YearSection: fillChar = QLatin1Char('y'); break;
-
3364 default: -
3365 QMessageLogger("tools/qdatetime.cpp", 56815676, __PRETTY_FUNCTION__).warning("QDateTimeParser::sectionFormat Internal error (%s)", -
3366 QString(sectionName(s)).toLocal8Bit().constData()); -
3367 return QString();
-
3368 } -
3369 if (fillChar.isNull()) {
-
3370 QMessageLogger("tools/qdatetime.cpp", 56865681, __PRETTY_FUNCTION__).warning("QDateTimeParser::sectionFormat Internal error 2"); -
3371 return QString();
-
3372 } -
3373 -
3374 QString str; -
3375 str.fill(fillChar, count); -
3376 return str;
-
3377} -
3378bool QDateTimeParser::potentialValue(const QString &str, int min, int max, int index, -
3379 const QDateTime &currentValue, int insert) const -
3380{ -
3381 if (str.isEmpty()) {
-
3382 return true;
-
3383 } -
3384 const int size = sectionMaxSize(index); -
3385 int val = (int)locale().toUInt(str); -
3386 const SectionNode &sn = sectionNode(index); -
3387 if (sn.type == YearSection2Digits) {
-
3388 val += currentValue.date().year() - (currentValue.date().year() % 100); -
3389 }
-
3390 if (val >= min && val <= max && str.size() == size) {
-
3391 return true;
-
3392 } else if (val > max) {
-
3393 return false;
-
3394 } else if (str.size() == size && val < min) {
-
3395 return false;
-
3396 } -
3397 -
3398 const int len = size - str.size(); -
3399 for (int i=0; i<len; ++i) {
-
3400 for (int j=0; j<10; ++j) {
-
3401 if (potentialValue(str + QLatin1Char('0' + j), min, max, index, currentValue, insert)) {
-
3402 return true;
-
3403 } else if (insert >= 0) {
-
3404 QString tmp = str; -
3405 tmp.insert(insert, QLatin1Char('0' + j)); -
3406 if (potentialValue(tmp, min, max, index, currentValue, insert))
-
3407 return true;
-
3408 }
-
3409 } -
3410 }
-
3411 -
3412 return false;
-
3413} -
3414 -
3415bool QDateTimeParser::skipToNextSection(int index, const QDateTime &current, const QString &text) const -
3416{ -
3417 qt_noop(); -
3418 -
3419 const SectionNode &node = sectionNode(index); -
3420 qt_noop(); -
3421 -
3422 const QDateTime maximum = getMaximum(); -
3423 const QDateTime minimum = getMinimum(); -
3424 QDateTime tmp = current; -
3425 int min = absoluteMin(index); -
3426 setDigit(tmp, index, min); -
3427 if (tmp < minimum) {
-
3428 min = getDigit(minimum, index); -
3429 }
-
3430 -
3431 int max = absoluteMax(index, current); -
3432 setDigit(tmp, index, max); -
3433 if (tmp > maximum) {
-
3434 max = getDigit(maximum, index); -
3435 }
-
3436 int pos = cursorPosition() - node.pos; -
3437 if (pos < 0 || pos >= text.size())
-
3438 pos = -1;
-
3439 -
3440 const bool potential = potentialValue(text, min, max, index, current, pos); -
3441 return !potential;
-
3442 -
3443 -
3444 -
3445 -
3446 -
3447 -
3448} -
3449 -
3450 -
3451 -
3452 -
3453 -
3454 -
3455QString QDateTimeParser::sectionName(int s) const -
3456{ -
3457 switch (s) { -
3458 case QDateTimeParser::AmPmSection: return QLatin1String("AmPmSection");
-
3459 case QDateTimeParser::DaySection: return QLatin1String("DaySection");
-
3460 case QDateTimeParser::DayOfWeekSectionShort: return QLatin1String("DayOfWeekSectionShort");
-
3461 case QDateTimeParser::DayOfWeekSectionLong: return QLatin1String("DayOfWeekSectionLong");
-
3462 case QDateTimeParser::Hour24Section: return QLatin1String("Hour24Section");
-
3463 case QDateTimeParser::Hour12Section: return QLatin1String("Hour12Section");
-
3464 case QDateTimeParser::MSecSection: return QLatin1String("MSecSection");
-
3465 case QDateTimeParser::MinuteSection: return QLatin1String("MinuteSection");
-
3466 case QDateTimeParser::MonthSection: return QLatin1String("MonthSection");
-
3467 case QDateTimeParser::SecondSection: return QLatin1String("SecondSection");
-
3468 case QDateTimeParser::YearSection: return QLatin1String("YearSection");
-
3469 case QDateTimeParser::YearSection2Digits: return QLatin1String("YearSection2Digits");
-
3470 case QDateTimeParser::NoSection: return QLatin1String("NoSection");
-
3471 case QDateTimeParser::FirstSection: return QLatin1String("FirstSection");
-
3472 case QDateTimeParser::LastSection: return QLatin1String("LastSection");
-
3473 default: return QLatin1String("Unknown section ") + QString::number(s);
-
3474 } -
3475}
-
3476 -
3477 -
3478 -
3479 -
3480 -
3481 -
3482QString QDateTimeParser::stateName(int s) const -
3483{ -
3484 switch (s) { -
3485 case Invalid: return QLatin1String("Invalid");
-
3486 case Intermediate: return QLatin1String("Intermediate");
-
3487 case Acceptable: return QLatin1String("Acceptable");
-
3488 default: return QLatin1String("Unknown state ") + QString::number(s);
-
3489 } -
3490}
-
3491 -
3492 -
3493bool QDateTimeParser::fromString(const QString &t, QDate *date, QTime *time) const -
3494{ -
3495 QDateTime val(QDate(1900, 1, 1), QTime(0, 0, 0, 0)); -
3496 QString text = t; -
3497 int copy = -1; -
3498 const StateNode tmp = parse(text, copy, val, false); -
3499 if (tmp.state != Acceptable || tmp.conflicts) {
-
3500 return false;
-
3501 } -
3502 if (time) {
-
3503 const QTime t = tmp.value.time(); -
3504 if (!t.isValid()) {
-
3505 return false;
-
3506 } -
3507 *time = t; -
3508 }
-
3509 -
3510 if (date) {
-
3511 const QDate d = tmp.value.date(); -
3512 if (!d.isValid()) {
-
3513 return false;
-
3514 } -
3515 *date = d; -
3516 }
-
3517 return true;
-
3518} -
3519 -
3520 -
3521QDateTime QDateTimeParser::getMinimum() const -
3522{ -
3523 return QDateTime(QDate(100, 1, 1), QTime(0, 0, 0, 0), spec);
-
3524} -
3525 -
3526QDateTime QDateTimeParser::getMaximum() const -
3527{ -
3528 return QDateTime(QDate(7999, 12, 31), QTime(23, 59, 59, 999), spec);
-
3529} -
3530 -
3531QString QDateTimeParser::getAmPmText(AmPm ap, Case cs) const -
3532{ -
3533 if (ap == AmText) {
-
3534 return (cs == UpperCase ? QLatin1String("AM") : QLatin1String("am"));
-
3535 } else { -
3536 return (cs == UpperCase ? QLatin1String("PM") : QLatin1String("pm"));
-
3537 } -
3538} -
3539 -
3540 -
3541 -
3542 -
3543 -
3544 -
3545 -
3546bool operator==(const QDateTimeParser::SectionNode &s1, const QDateTimeParser::SectionNode &s2) -
3547{ -
3548 return (s1.type == s2.type) && (s1.pos == s2.pos) && (s1.count == s2.count);
-
3549} -
3550 -
3551 -
3552 -
3553 -
3554 -
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial