summaryrefslogtreecommitdiffabout
authorulf69 <ulf69>2004-10-07 01:12:08 (UTC)
committer ulf69 <ulf69>2004-10-07 01:12:08 (UTC)
commitf1eb5b74c962909851607c4b4cb05ee18a347d37 (patch) (unidiff)
treefa98d9fc97e5d75810936917dc6c6039e7c79b45
parentf434ef382b60c1a420dd0e037d119d082e478491 (diff)
downloadkdepimpi-f1eb5b74c962909851607c4b4cb05ee18a347d37.zip
kdepimpi-f1eb5b74c962909851607c4b4cb05ee18a347d37.tar.gz
kdepimpi-f1eb5b74c962909851607c4b4cb05ee18a347d37.tar.bz2
added easy to use method to read Date with certain format
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--microkde/kdecore/klocale.cpp17
-rw-r--r--microkde/kdecore/klocale.h2
2 files changed, 19 insertions, 0 deletions
diff --git a/microkde/kdecore/klocale.cpp b/microkde/kdecore/klocale.cpp
index 17031c7..8b4513e 100644
--- a/microkde/kdecore/klocale.cpp
+++ b/microkde/kdecore/klocale.cpp
@@ -523,192 +523,209 @@ QTime KLocale::readTime(const QString &intstr, bool seconds, bool *ok) const
523 pm = false; 523 pm = false;
524 strpos += len; 524 strpos += len;
525 } 525 }
526 else 526 else
527 goto error; 527 goto error;
528 } 528 }
529 } 529 }
530 break; 530 break;
531 531
532 case 'k': 532 case 'k':
533 case 'H': 533 case 'H':
534 g_12h = false; 534 g_12h = false;
535 hour = readInt(str, strpos); 535 hour = readInt(str, strpos);
536 if (hour < 0 || hour > 23) 536 if (hour < 0 || hour > 23)
537 goto error; 537 goto error;
538 538
539 break; 539 break;
540 540
541 case 'l': 541 case 'l':
542 case 'I': 542 case 'I':
543 g_12h = true; 543 g_12h = true;
544 hour = readInt(str, strpos); 544 hour = readInt(str, strpos);
545 if (hour < 1 || hour > 12) 545 if (hour < 1 || hour > 12)
546 goto error; 546 goto error;
547 547
548 break; 548 break;
549 549
550 case 'M': 550 case 'M':
551 minute = readInt(str, strpos); 551 minute = readInt(str, strpos);
552 if (minute < 0 || minute > 59) 552 if (minute < 0 || minute > 59)
553 goto error; 553 goto error;
554 554
555 break; 555 break;
556 556
557 case 'S': 557 case 'S':
558 second = readInt(str, strpos); 558 second = readInt(str, strpos);
559 if (second < 0 || second > 59) 559 if (second < 0 || second > 59)
560 goto error; 560 goto error;
561 561
562 break; 562 break;
563 } 563 }
564 } 564 }
565 if (g_12h) 565 if (g_12h)
566 { 566 {
567 hour %= 12; 567 hour %= 12;
568 if (pm) hour += 12; 568 if (pm) hour += 12;
569 } 569 }
570 570
571 if (ok) *ok = true; 571 if (ok) *ok = true;
572 return QTime(hour, minute, second); 572 return QTime(hour, minute, second);
573 573
574 error: 574 error:
575 if (ok) *ok = false; 575 if (ok) *ok = false;
576 return QTime(-1, -1, -1); // return invalid date if it didn't work 576 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. 577 // 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. 578 // The presence of the bool* (since KDE-3.0) removes the need for an invalid QTime.
579} 579}
580 580
581QDateTime KLocale::readDateTime(const QString &intstr, 581QDateTime KLocale::readDateTime(const QString &intstr,
582 IntDateFormat intIntDateFormat, 582 IntDateFormat intIntDateFormat,
583 bool* ok) const 583 bool* ok) const
584{ 584{
585 bool ok1, ok2; 585 bool ok1, ok2;
586 586
587 // AT the moment we can not read any other format then ISODate 587 // AT the moment we can not read any other format then ISODate
588 if ( intIntDateFormat != ISODate ) 588 if ( intIntDateFormat != ISODate )
589 { 589 {
590 qDebug("KLocale::readDateTime, only ISODate is supported."); 590 qDebug("KLocale::readDateTime, only ISODate is supported.");
591 return QDateTime(); 591 return QDateTime();
592 } 592 }
593 593
594 int pos = intstr.find("T"); 594 int pos = intstr.find("T");
595 QString date = intstr.left(pos); 595 QString date = intstr.left(pos);
596 QString time = intstr.mid(pos+1); 596 QString time = intstr.mid(pos+1);
597 597
598 QString dformat = dateFormat(intIntDateFormat); 598 QString dformat = dateFormat(intIntDateFormat);
599 QString tformat = timeFormat(intIntDateFormat); 599 QString tformat = timeFormat(intIntDateFormat);
600 600
601 QDate m_date = readDate(date, dformat, &ok1); 601 QDate m_date = readDate(date, dformat, &ok1);
602 QTime m_time = readTime(time, tformat, &ok2); 602 QTime m_time = readTime(time, tformat, &ok2);
603 603
604 if (ok) 604 if (ok)
605 { 605 {
606 if ((ok1 == false) || (ok2 == false)) 606 if ((ok1 == false) || (ok2 == false))
607 *ok = false; 607 *ok = false;
608 else 608 else
609 *ok = true; 609 *ok = true;
610 } 610 }
611 QDateTime m_dt; 611 QDateTime m_dt;
612 m_dt.setDate(m_date); 612 m_dt.setDate(m_date);
613 m_dt.setTime(m_time); 613 m_dt.setTime(m_time);
614 614
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); 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);
616 return m_dt; 616 return m_dt;
617} 617}
618 618
619QDate KLocale::readDate(const QString &intstr,
620 IntDateFormat intIntDateFormat,
621 bool* ok) const
622{
623 bool ok1;
624
625 QString dformat = dateFormat(intIntDateFormat);
626
627 QDate m_date = readDate(intstr, dformat, &ok1);
628
629 if (ok)
630 *ok = ok1;
631
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);
633 return m_date;
634}
635
619 636
620bool KLocale::use12Clock() const 637bool KLocale::use12Clock() const
621{ 638{
622 return !mHourF24Format ;; 639 return !mHourF24Format ;;
623} 640}
624 641
625bool KLocale::weekStartsMonday() const 642bool KLocale::weekStartsMonday() const
626{ 643{
627 return mWeekStartsMonday; 644 return mWeekStartsMonday;
628} 645}
629 646
630int KLocale::weekStartDay() const 647int KLocale::weekStartDay() const
631{ 648{
632 if ( mWeekStartsMonday ) 649 if ( mWeekStartsMonday )
633 return 1; 650 return 1;
634 return 7; 651 return 7;
635} 652}
636 653
637QString KLocale::weekDayName(int i,bool shortName) const 654QString KLocale::weekDayName(int i,bool shortName) const
638{ 655{
639 if ( shortName ) 656 if ( shortName )
640 switch ( i ) 657 switch ( i )
641 { 658 {
642 case 1: return i18n("Monday", "Mon"); 659 case 1: return i18n("Monday", "Mon");
643 case 2: return i18n("Tuesday", "Tue"); 660 case 2: return i18n("Tuesday", "Tue");
644 case 3: return i18n("Wednesday", "Wed"); 661 case 3: return i18n("Wednesday", "Wed");
645 case 4: return i18n("Thursday", "Thu"); 662 case 4: return i18n("Thursday", "Thu");
646 case 5: return i18n("Friday", "Fri"); 663 case 5: return i18n("Friday", "Fri");
647 case 6: return i18n("Saturday", "Sat"); 664 case 6: return i18n("Saturday", "Sat");
648 case 7: return i18n("Sunday", "Sun"); 665 case 7: return i18n("Sunday", "Sun");
649 } 666 }
650 else 667 else
651 switch ( i ) 668 switch ( i )
652 { 669 {
653 case 1: return i18n("Monday"); 670 case 1: return i18n("Monday");
654 case 2: return i18n("Tuesday"); 671 case 2: return i18n("Tuesday");
655 case 3: return i18n("Wednesday"); 672 case 3: return i18n("Wednesday");
656 case 4: return i18n("Thursday"); 673 case 4: return i18n("Thursday");
657 case 5: return i18n("Friday"); 674 case 5: return i18n("Friday");
658 case 6: return i18n("Saturday"); 675 case 6: return i18n("Saturday");
659 case 7: return i18n("Sunday"); 676 case 7: return i18n("Sunday");
660 } 677 }
661 678
662 return QString::null; 679 return QString::null;
663} 680}
664 681
665QString KLocale::monthName(int i,bool shortName) const 682QString KLocale::monthName(int i,bool shortName) const
666{ 683{
667 if ( shortName ) 684 if ( shortName )
668 switch ( i ) 685 switch ( i )
669 { 686 {
670 case 1: return i18n("January", "Jan"); 687 case 1: return i18n("January", "Jan");
671 case 2: return i18n("February", "Feb"); 688 case 2: return i18n("February", "Feb");
672 case 3: return i18n("March", "Mar"); 689 case 3: return i18n("March", "Mar");
673 case 4: return i18n("April", "Apr"); 690 case 4: return i18n("April", "Apr");
674 case 5: return i18n("May short", "May"); 691 case 5: return i18n("May short", "May");
675 case 6: return i18n("June", "Jun"); 692 case 6: return i18n("June", "Jun");
676 case 7: return i18n("July", "Jul"); 693 case 7: return i18n("July", "Jul");
677 case 8: return i18n("August", "Aug"); 694 case 8: return i18n("August", "Aug");
678 case 9: return i18n("September", "Sep"); 695 case 9: return i18n("September", "Sep");
679 case 10: return i18n("October", "Oct"); 696 case 10: return i18n("October", "Oct");
680 case 11: return i18n("November", "Nov"); 697 case 11: return i18n("November", "Nov");
681 case 12: return i18n("December", "Dec"); 698 case 12: return i18n("December", "Dec");
682 } 699 }
683 else 700 else
684 switch (i) 701 switch (i)
685 { 702 {
686 case 1: return i18n("January"); 703 case 1: return i18n("January");
687 case 2: return i18n("February"); 704 case 2: return i18n("February");
688 case 3: return i18n("March"); 705 case 3: return i18n("March");
689 case 4: return i18n("April"); 706 case 4: return i18n("April");
690 case 5: return i18n("May long", "May"); 707 case 5: return i18n("May long", "May");
691 case 6: return i18n("June"); 708 case 6: return i18n("June");
692 case 7: return i18n("July"); 709 case 7: return i18n("July");
693 case 8: return i18n("August"); 710 case 8: return i18n("August");
694 case 9: return i18n("September"); 711 case 9: return i18n("September");
695 case 10: return i18n("October"); 712 case 10: return i18n("October");
696 case 11: return i18n("November"); 713 case 11: return i18n("November");
697 case 12: return i18n("December"); 714 case 12: return i18n("December");
698 } 715 }
699 716
700 return QString::null; 717 return QString::null;
701} 718}
702 719
703QString KLocale::country() const 720QString KLocale::country() const
704{ 721{
705 return QString::null; 722 return QString::null;
706} 723}
707 724
708QString KLocale::dateFormat(IntDateFormat intIntDateFormat) const 725QString KLocale::dateFormat(IntDateFormat intIntDateFormat) const
709{ 726{
710 const IntDateFormat dformat = (intIntDateFormat == Undefined)?mIntDateFormat:intIntDateFormat; 727 const IntDateFormat dformat = (intIntDateFormat == Undefined)?mIntDateFormat:intIntDateFormat;
711 728
712 if ( dformat == ISODate ) 729 if ( dformat == ISODate )
713 return "%Y-%m-%d"; 730 return "%Y-%m-%d";
714 731
diff --git a/microkde/kdecore/klocale.h b/microkde/kdecore/klocale.h
index 949301a..5783530 100644
--- a/microkde/kdecore/klocale.h
+++ b/microkde/kdecore/klocale.h
@@ -1,116 +1,118 @@
1#ifndef MINIKDE_KLOCALE_H 1#ifndef MINIKDE_KLOCALE_H
2#define MINIKDE_KLOCALE_H 2#define MINIKDE_KLOCALE_H
3 3
4#include <qstring.h> 4#include <qstring.h>
5#include <qstringlist.h> 5#include <qstringlist.h>
6#include <qdatetime.h> 6#include <qdatetime.h>
7#include <qdict.h> 7#include <qdict.h>
8 8
9#ifndef I18N_NOOP 9#ifndef I18N_NOOP
10#define I18N_NOOP(x) (x) 10#define I18N_NOOP(x) (x)
11#endif 11#endif
12 12
13class KCalendarSystem; 13class KCalendarSystem;
14void setLocaleDict( QDict<QString> * dict ); 14void setLocaleDict( QDict<QString> * dict );
15QString i18n(const char *text); 15QString i18n(const char *text);
16QString i18n(const char *hint, const char *text); 16QString i18n(const char *hint, const char *text);
17QString i18n(const char *text1, const char *textn, int num); 17QString i18n(const char *text1, const char *textn, int num);
18 18
19// Qt3's uic generates i18n( "msg", "comment" ) calls which conflict 19// Qt3's uic generates i18n( "msg", "comment" ) calls which conflict
20// with our i18n method. we use uic -tr tr2i18n to redirect 20// with our i18n method. we use uic -tr tr2i18n to redirect
21// to the right i18n() function 21// to the right i18n() function
22inline QString tr2i18n(const char* message, const char* =0) { 22inline QString tr2i18n(const char* message, const char* =0) {
23 return i18n( message); 23 return i18n( message);
24} 24}
25 25
26class KLocale 26class KLocale
27{ 27{
28 public: 28 public:
29 KLocale(); 29 KLocale();
30 30
31 QString formatNumber(double num, int precision = -1) const; 31 QString formatNumber(double num, int precision = -1) const;
32 QString formatNumber(const QString &numStr) const; 32 QString formatNumber(const QString &numStr) const;
33 double readNumber(const QString &numStr, bool * ok = 0) const; 33 double readNumber(const QString &numStr, bool * ok = 0) const;
34 34
35 QString decimalSymbol() const; 35 QString decimalSymbol() const;
36 QString thousandsSeparator() const; 36 QString thousandsSeparator() const;
37 QString positiveSign() const; 37 QString positiveSign() const;
38 QString negativeSign() const; 38 QString negativeSign() const;
39 39
40 40
41 QString translate( const char *index ) const; 41 QString translate( const char *index ) const;
42 QString translate( const char *index, const char *fallback) const; 42 QString translate( const char *index, const char *fallback) const;
43 43
44 enum IntDateFormat { Undefined=-1, Default=0, Format1=1, ISODate=2, Userdefined=3 }; 44 enum IntDateFormat { Undefined=-1, Default=0, Format1=1, ISODate=2, Userdefined=3 };
45 45
46 QString formatDate(const QDate &pDate, bool shortFormat = false, IntDateFormat intIntDateFormat = Undefined) const; 46 QString formatDate(const QDate &pDate, bool shortFormat = false, IntDateFormat intIntDateFormat = Undefined) const;
47 QString formatTime(const QTime &pTime, bool includeSecs = false, IntDateFormat intIntDateFormat = Undefined) const; 47 QString formatTime(const QTime &pTime, bool includeSecs = false, IntDateFormat intIntDateFormat = Undefined) const;
48 QString formatDateTime(const QDateTime &pDateTime, IntDateFormat intIntDateFormat = Undefined) const; 48 QString formatDateTime(const QDateTime &pDateTime, IntDateFormat intIntDateFormat = Undefined) const;
49 QString formatDateTime(const QDateTime &pDateTime, 49 QString formatDateTime(const QDateTime &pDateTime,
50 bool shortFormat, 50 bool shortFormat,
51 bool includeSecs = false, IntDateFormat intIntDateFormat = Undefined) const; 51 bool includeSecs = false, IntDateFormat intIntDateFormat = Undefined) const;
52 52
53 QDate readDate(const QString &str, bool* ok = 0) const; 53 QDate readDate(const QString &str, bool* ok = 0) const;
54 QDate readDate( const QString &intstr, const QString &fmt, bool* ok = 0) const; 54 QDate readDate( const QString &intstr, const QString &fmt, bool* ok = 0) const;
55 QTime readTime(const QString &str, bool* ok = 0) const; 55 QTime readTime(const QString &str, bool* ok = 0) const;
56 QDate readDate(const QString &intstr, IntDateFormat intIntDateFormat, bool* ok) const;
57
56 QDateTime readDateTime(const QString &intstr, IntDateFormat intIntDateFormat, bool* ok) const; 58 QDateTime readDateTime(const QString &intstr, IntDateFormat intIntDateFormat, bool* ok) const;
57 59
58 bool use12Clock() const; 60 bool use12Clock() const;
59 bool weekStartsMonday() const; 61 bool weekStartsMonday() const;
60 int weekStartDay() const; 62 int weekStartDay() const;
61 63
62 QString weekDayName(int,bool=false) const; 64 QString weekDayName(int,bool=false) const;
63 QString monthName(int,bool=false) const; 65 QString monthName(int,bool=false) const;
64 66
65 QString country() const; 67 QString country() const;
66 68
67 QString dateFormat(IntDateFormat intIntDateFormat = Undefined) const; 69 QString dateFormat(IntDateFormat intIntDateFormat = Undefined) const;
68 QString dateFormatShort(IntDateFormat intIntDateFormat = Undefined) const; 70 QString dateFormatShort(IntDateFormat intIntDateFormat = Undefined) const;
69 QString timeFormat(IntDateFormat intIntDateFormat = Undefined) const; 71 QString timeFormat(IntDateFormat intIntDateFormat = Undefined) const;
70 72
71 void insertCatalogue ( const QString & ); 73 void insertCatalogue ( const QString & );
72 74
73 KCalendarSystem *calendar(); 75 KCalendarSystem *calendar();
74 void setHore24Format ( bool ); 76 void setHore24Format ( bool );
75 void setWeekStartMonday( bool ); 77 void setWeekStartMonday( bool );
76 void setIntDateFormat( IntDateFormat ); 78 void setIntDateFormat( IntDateFormat );
77 void setIntTimeFormat( IntDateFormat ); 79 void setIntTimeFormat( IntDateFormat );
78 IntDateFormat getIntDateFormat( ); 80 IntDateFormat getIntDateFormat( );
79 IntDateFormat getIntTimeFormat( ); 81 IntDateFormat getIntTimeFormat( );
80 void setLanguage( int ); 82 void setLanguage( int );
81 void setDateFormat( QString ); 83 void setDateFormat( QString );
82 void setDateFormatShort( QString ); 84 void setDateFormatShort( QString );
83 85
84 QString m_decimalSymbol; 86 QString m_decimalSymbol;
85 QString m_thousandsSeparator; 87 QString m_thousandsSeparator;
86 QString m_currencySymbol; 88 QString m_currencySymbol;
87 QString m_monetaryDecimalSymbol; 89 QString m_monetaryDecimalSymbol;
88 QString m_monetaryThousandsSeparator; 90 QString m_monetaryThousandsSeparator;
89 QString m_positiveSign; 91 QString m_positiveSign;
90 QString m_negativeSign; 92 QString m_negativeSign;
91 93
92 int timezoneOffset( QString ); 94 int timezoneOffset( QString );
93 QStringList timeZoneList() const; 95 QStringList timeZoneList() const;
94 void setDaylightSaving( bool, int , int ); 96 void setDaylightSaving( bool, int , int );
95 int localTimeOffset(const QDateTime &); 97 int localTimeOffset(const QDateTime &);
96 void setTimezone( const QString &timeZone ); 98 void setTimezone( const QString &timeZone );
97 private: 99 private:
98 QTime readTime(const QString &str, bool seconds, bool *ok) const; 100 QTime readTime(const QString &str, bool seconds, bool *ok) const;
99 QDate readDate(const QString &str, bool shortFormat, bool *ok) const; 101 QDate readDate(const QString &str, bool shortFormat, bool *ok) const;
100 KCalendarSystem *mCalendarSystem; 102 KCalendarSystem *mCalendarSystem;
101 bool mWeekStartsMonday; 103 bool mWeekStartsMonday;
102 bool mHourF24Format; 104 bool mHourF24Format;
103 IntDateFormat mIntDateFormat; 105 IntDateFormat mIntDateFormat;
104 IntDateFormat mIntTimeFormat; 106 IntDateFormat mIntTimeFormat;
105 int mLanguage; 107 int mLanguage;
106 QString mDateFormat; 108 QString mDateFormat;
107 QString mDateFormatShort; 109 QString mDateFormatShort;
108 QStringList mTimeZoneList; 110 QStringList mTimeZoneList;
109 bool daylightEnabled; 111 bool daylightEnabled;
110 int mDaylightTZoffset; 112 int mDaylightTZoffset;
111 int mNondaylightTZoffset; 113 int mNondaylightTZoffset;
112 bool mSouthDaylight; 114 bool mSouthDaylight;
113 int daylightStart, daylightEnd, mTimeZoneOffset; 115 int daylightStart, daylightEnd, mTimeZoneOffset;
114}; 116};
115 117
116#endif 118#endif