-rw-r--r-- | microkde/kdecore/klocale.cpp | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/microkde/kdecore/klocale.cpp b/microkde/kdecore/klocale.cpp index 8b4513e..27acfec 100644 --- a/microkde/kdecore/klocale.cpp +++ b/microkde/kdecore/klocale.cpp | |||
@@ -294,98 +294,102 @@ QString KLocale::formatDate(const QDate &pDate, bool shortFormat, IntDateFormat | |||
294 | if ( number / 10 ) | 294 | if ( number / 10 ) |
295 | buffer[index++] = number / 10 + '0'; | 295 | buffer[index++] = number / 10 + '0'; |
296 | buffer[index++] = number % 10 + '0'; | 296 | buffer[index++] = number % 10 + '0'; |
297 | break; | 297 | break; |
298 | case 'm': | 298 | case 'm': |
299 | put_it_in( buffer, index, pDate.month() ); | 299 | put_it_in( buffer, index, pDate.month() ); |
300 | break; | 300 | break; |
301 | case 'b': | 301 | case 'b': |
302 | put_it_in( buffer, index, monthName(pDate.month(), true) ); | 302 | put_it_in( buffer, index, monthName(pDate.month(), true) ); |
303 | break; | 303 | break; |
304 | case 'B': | 304 | case 'B': |
305 | put_it_in( buffer, index, monthName(pDate.month(), false) ); | 305 | put_it_in( buffer, index, monthName(pDate.month(), false) ); |
306 | break; | 306 | break; |
307 | case 'd': | 307 | case 'd': |
308 | put_it_in( buffer, index, pDate.day() ); | 308 | put_it_in( buffer, index, pDate.day() ); |
309 | break; | 309 | break; |
310 | case 'a': | 310 | case 'a': |
311 | put_it_in( buffer, index, weekDayName(pDate.dayOfWeek(), true) ); | 311 | put_it_in( buffer, index, weekDayName(pDate.dayOfWeek(), true) ); |
312 | break; | 312 | break; |
313 | case 'A': | 313 | case 'A': |
314 | put_it_in( buffer, index, weekDayName(pDate.dayOfWeek(), false) ); | 314 | put_it_in( buffer, index, weekDayName(pDate.dayOfWeek(), false) ); |
315 | break; | 315 | break; |
316 | default: | 316 | default: |
317 | buffer[index++] = rst.at( format_index ); | 317 | buffer[index++] = rst.at( format_index ); |
318 | break; | 318 | break; |
319 | } | 319 | } |
320 | escape = false; | 320 | escape = false; |
321 | } | 321 | } |
322 | } | 322 | } |
323 | QString ret( buffer, index ); | 323 | QString ret( buffer, index ); |
324 | delete [] buffer; | 324 | delete [] buffer; |
325 | return ret; | 325 | return ret; |
326 | } | 326 | } |
327 | 327 | ||
328 | QString KLocale::formatDateTime(const QDateTime &pDateTime, | 328 | QString KLocale::formatDateTime(const QDateTime &pDateTime, |
329 | bool shortFormat, | 329 | bool shortFormat, |
330 | bool includeSeconds, | 330 | bool includeSeconds, |
331 | IntDateFormat intIntDateFormat) const | 331 | IntDateFormat intIntDateFormat) const |
332 | { | 332 | { |
333 | QString format("%1 %2"); | 333 | QString format("%1 %2"); |
334 | 334 | ||
335 | if ( intIntDateFormat == Default ) | 335 | if ( intIntDateFormat == Default ) |
336 | format = "%1 %2"; | 336 | format = "%1 %2"; |
337 | else if ( intIntDateFormat == Format1 ) | 337 | else if ( intIntDateFormat == Format1 ) |
338 | format = "%1 %2"; | 338 | format = "%1 %2"; |
339 | else if ( intIntDateFormat == ISODate ) | 339 | else if ( intIntDateFormat == ISODate ) |
340 | format = "%1T%2"; | 340 | format = "%1T%2"; |
341 | 341 | ||
342 | return format.arg(formatDate( pDateTime.date(), shortFormat, intIntDateFormat )) | 342 | QString res = format.arg(formatDate( pDateTime.date(), shortFormat, intIntDateFormat )) |
343 | .arg(formatTime( pDateTime.time(), includeSeconds , intIntDateFormat )); | 343 | .arg(formatTime( pDateTime.time(), includeSeconds , intIntDateFormat )); |
344 | |||
345 | //qDebug("KLocale::formatDateTime transformed %s, into %s", pDateTime.toString().latin1(), res.latin1() ); | ||
346 | |||
347 | return res; | ||
344 | } | 348 | } |
345 | 349 | ||
346 | QString KLocale::formatDateTime(const QDateTime &pDateTime, IntDateFormat intIntDateFormat) const | 350 | QString KLocale::formatDateTime(const QDateTime &pDateTime, IntDateFormat intIntDateFormat) const |
347 | { | 351 | { |
348 | return formatDateTime(pDateTime, true, true, intIntDateFormat); | 352 | return formatDateTime(pDateTime, true, true, intIntDateFormat); |
349 | } | 353 | } |
350 | 354 | ||
351 | QDate KLocale::readDate(const QString &intstr, bool* ok) const | 355 | QDate KLocale::readDate(const QString &intstr, bool* ok) const |
352 | { | 356 | { |
353 | QDate date; | 357 | QDate date; |
354 | date = readDate(intstr, true, ok); | 358 | date = readDate(intstr, true, ok); |
355 | if (date.isValid()) return date; | 359 | if (date.isValid()) return date; |
356 | return readDate(intstr, false, ok); | 360 | return readDate(intstr, false, ok); |
357 | } | 361 | } |
358 | 362 | ||
359 | QDate KLocale::readDate(const QString &intstr, bool shortFormat, bool* ok) const | 363 | QDate KLocale::readDate(const QString &intstr, bool shortFormat, bool* ok) const |
360 | { | 364 | { |
361 | QString fmt = (shortFormat ? dateFormatShort() : dateFormat()).simplifyWhiteSpace(); | 365 | QString fmt = (shortFormat ? dateFormatShort() : dateFormat()).simplifyWhiteSpace(); |
362 | return readDate( intstr, fmt, ok ); | 366 | return readDate( intstr, fmt, ok ); |
363 | } | 367 | } |
364 | 368 | ||
365 | QDate KLocale::readDate(const QString &intstr, const QString &fmt, bool* ok) const | 369 | QDate KLocale::readDate(const QString &intstr, const QString &fmt, bool* ok) const |
366 | { | 370 | { |
367 | //kdDebug(173) << "KLocale::readDate intstr=" << intstr << " fmt=" << fmt << endl; | 371 | //kdDebug(173) << "KLocale::readDate intstr=" << intstr << " fmt=" << fmt << endl; |
368 | QString str = intstr.simplifyWhiteSpace().lower(); | 372 | QString str = intstr.simplifyWhiteSpace().lower(); |
369 | int day = -1, month = -1; | 373 | int day = -1, month = -1; |
370 | // allow the year to be omitted if not in the format | 374 | // allow the year to be omitted if not in the format |
371 | int year = QDate::currentDate().year(); | 375 | int year = QDate::currentDate().year(); |
372 | uint strpos = 0; | 376 | uint strpos = 0; |
373 | uint fmtpos = 0; | 377 | uint fmtpos = 0; |
374 | 378 | ||
375 | while (fmt.length() > fmtpos || str.length() > strpos) | 379 | while (fmt.length() > fmtpos || str.length() > strpos) |
376 | { | 380 | { |
377 | if ( !(fmt.length() > fmtpos && str.length() > strpos) ) | 381 | if ( !(fmt.length() > fmtpos && str.length() > strpos) ) |
378 | goto error; | 382 | goto error; |
379 | 383 | ||
380 | QChar c = fmt.at(fmtpos++); | 384 | QChar c = fmt.at(fmtpos++); |
381 | 385 | ||
382 | if (c != '%') { | 386 | if (c != '%') { |
383 | if (c.isSpace()) | 387 | if (c.isSpace()) |
384 | strpos++; | 388 | strpos++; |
385 | else if (c != str.at(strpos++)) | 389 | else if (c != str.at(strpos++)) |
386 | goto error; | 390 | goto error; |
387 | continue; | 391 | continue; |
388 | } | 392 | } |
389 | 393 | ||
390 | // remove space at the begining | 394 | // remove space at the begining |
391 | if (str.length() > strpos && str.at(strpos).isSpace()) | 395 | if (str.length() > strpos && str.at(strpos).isSpace()) |
@@ -556,108 +560,114 @@ QTime KLocale::readTime(const QString &intstr, bool seconds, bool *ok) const | |||
556 | 560 | ||
557 | case 'S': | 561 | case 'S': |
558 | second = readInt(str, strpos); | 562 | second = readInt(str, strpos); |
559 | if (second < 0 || second > 59) | 563 | if (second < 0 || second > 59) |
560 | goto error; | 564 | goto error; |
561 | 565 | ||
562 | break; | 566 | break; |
563 | } | 567 | } |
564 | } | 568 | } |
565 | if (g_12h) | 569 | if (g_12h) |
566 | { | 570 | { |
567 | hour %= 12; | 571 | hour %= 12; |
568 | if (pm) hour += 12; | 572 | if (pm) hour += 12; |
569 | } | 573 | } |
570 | 574 | ||
571 | if (ok) *ok = true; | 575 | if (ok) *ok = true; |
572 | return QTime(hour, minute, second); | 576 | return QTime(hour, minute, second); |
573 | 577 | ||
574 | error: | 578 | error: |
575 | if (ok) *ok = false; | 579 | if (ok) *ok = false; |
576 | return QTime(-1, -1, -1); // return invalid date if it didn't work | 580 | return QTime(-1, -1, -1); // return invalid date if it didn't work |
577 | // This will be removed in the near future, since it gives a warning on stderr. | 581 | // This will be removed in the near future, since it gives a warning on stderr. |
578 | // The presence of the bool* (since KDE-3.0) removes the need for an invalid QTime. | 582 | // The presence of the bool* (since KDE-3.0) removes the need for an invalid QTime. |
579 | } | 583 | } |
580 | 584 | ||
581 | QDateTime KLocale::readDateTime(const QString &intstr, | 585 | QDateTime KLocale::readDateTime(const QString &intstr, |
582 | IntDateFormat intIntDateFormat, | 586 | IntDateFormat intIntDateFormat, |
583 | bool* ok) const | 587 | bool* ok) const |
584 | { | 588 | { |
585 | bool ok1, ok2; | 589 | bool ok1, ok2; |
586 | 590 | ||
587 | // AT the moment we can not read any other format then ISODate | 591 | // AT the moment we can not read any other format then ISODate |
588 | if ( intIntDateFormat != ISODate ) | 592 | if ( intIntDateFormat != ISODate ) |
589 | { | 593 | { |
590 | qDebug("KLocale::readDateTime, only ISODate is supported."); | 594 | qDebug("KLocale::readDateTime, only ISODate is supported."); |
591 | return QDateTime(); | 595 | return QDateTime(); |
592 | } | 596 | } |
593 | 597 | ||
594 | int pos = intstr.find("T"); | 598 | int pos = intstr.find("T"); |
595 | QString date = intstr.left(pos); | 599 | QString date = intstr.left(pos); |
596 | QString time = intstr.mid(pos+1); | 600 | QString time = intstr.mid(pos+1); |
597 | 601 | ||
598 | QString dformat = dateFormat(intIntDateFormat); | 602 | QString dformat = dateFormat(intIntDateFormat); |
599 | QString tformat = timeFormat(intIntDateFormat); | 603 | QString tformat = timeFormat(intIntDateFormat); |
600 | 604 | ||
601 | QDate m_date = readDate(date, dformat, &ok1); | 605 | QDate m_date = readDate(date, dformat, &ok1); |
602 | QTime m_time = readTime(time, tformat, &ok2); | 606 | QTime m_time = readTime(time, tformat, &ok2); |
603 | 607 | ||
608 | QDateTime m_dt; | ||
609 | |||
604 | if (ok) | 610 | if (ok) |
605 | { | 611 | { |
606 | if ((ok1 == false) || (ok2 == false)) | 612 | if ((ok1 == false) || (ok2 == false)) |
607 | *ok = false; | 613 | *ok = false; |
608 | else | 614 | else |
609 | *ok = true; | 615 | *ok = true; |
610 | } | 616 | } |
611 | QDateTime m_dt; | ||
612 | m_dt.setDate(m_date); | ||
613 | m_dt.setTime(m_time); | ||
614 | 617 | ||
615 | qDebug("KLocale::readDateTime() transformed %s into %s (%s), %s (%s) : err1=%i, err2=%i", intstr.latin1(), date.latin1(), dformat.latin1(), time.latin1(), tformat.latin1(), ok1, ok2); | 618 | //only set values if both operations returned true. |
619 | if ((ok1 == true) && (ok2 == true)) | ||
620 | { | ||
621 | m_dt.setDate(m_date); | ||
622 | m_dt.setTime(m_time); | ||
623 | } | ||
624 | |||
625 | //qDebug("KLocale::readDateTime() transformed %s into %s (%s), %s (%s) : err1=%i, err2=%i", intstr.latin1(), date.latin1(), dformat.latin1(), time.latin1(), tformat.latin1(), ok1, ok2); | ||
616 | return m_dt; | 626 | return m_dt; |
617 | } | 627 | } |
618 | 628 | ||
619 | QDate KLocale::readDate(const QString &intstr, | 629 | QDate KLocale::readDate(const QString &intstr, |
620 | IntDateFormat intIntDateFormat, | 630 | IntDateFormat intIntDateFormat, |
621 | bool* ok) const | 631 | bool* ok) const |
622 | { | 632 | { |
623 | bool ok1; | 633 | bool ok1; |
624 | 634 | ||
625 | QString dformat = dateFormat(intIntDateFormat); | 635 | QString dformat = dateFormat(intIntDateFormat); |
626 | 636 | ||
627 | QDate m_date = readDate(intstr, dformat, &ok1); | 637 | QDate m_date = readDate(intstr, dformat, &ok1); |
628 | 638 | ||
629 | if (ok) | 639 | if (ok) |
630 | *ok = ok1; | 640 | *ok = ok1; |
631 | 641 | ||
632 | //qDebug("KLocale::readDate() transformed %s into %s (%s), %s (%s) : err1=%i, err2=%i", intstr.latin1(), date.latin1(), dformat.latin1(), time.latin1(), tformat.latin1(), ok1, ok2); | 642 | //qDebug("KLocale::readDate() transformed %s into %s (%s), %s (%s) : err1=%i, err2=%i", intstr.latin1(), date.latin1(), dformat.latin1(), time.latin1(), tformat.latin1(), ok1, ok2); |
633 | return m_date; | 643 | return m_date; |
634 | } | 644 | } |
635 | 645 | ||
636 | 646 | ||
637 | bool KLocale::use12Clock() const | 647 | bool KLocale::use12Clock() const |
638 | { | 648 | { |
639 | return !mHourF24Format ;; | 649 | return !mHourF24Format ;; |
640 | } | 650 | } |
641 | 651 | ||
642 | bool KLocale::weekStartsMonday() const | 652 | bool KLocale::weekStartsMonday() const |
643 | { | 653 | { |
644 | return mWeekStartsMonday; | 654 | return mWeekStartsMonday; |
645 | } | 655 | } |
646 | 656 | ||
647 | int KLocale::weekStartDay() const | 657 | int KLocale::weekStartDay() const |
648 | { | 658 | { |
649 | if ( mWeekStartsMonday ) | 659 | if ( mWeekStartsMonday ) |
650 | return 1; | 660 | return 1; |
651 | return 7; | 661 | return 7; |
652 | } | 662 | } |
653 | 663 | ||
654 | QString KLocale::weekDayName(int i,bool shortName) const | 664 | QString KLocale::weekDayName(int i,bool shortName) const |
655 | { | 665 | { |
656 | if ( shortName ) | 666 | if ( shortName ) |
657 | switch ( i ) | 667 | switch ( i ) |
658 | { | 668 | { |
659 | case 1: return i18n("Monday", "Mon"); | 669 | case 1: return i18n("Monday", "Mon"); |
660 | case 2: return i18n("Tuesday", "Tue"); | 670 | case 2: return i18n("Tuesday", "Tue"); |
661 | case 3: return i18n("Wednesday", "Wed"); | 671 | case 3: return i18n("Wednesday", "Wed"); |
662 | case 4: return i18n("Thursday", "Thu"); | 672 | case 4: return i18n("Thursday", "Thu"); |
663 | case 5: return i18n("Friday", "Fri"); | 673 | case 5: return i18n("Friday", "Fri"); |