author | zecke <zecke> | 2003-05-12 13:09:39 (UTC) |
---|---|---|
committer | zecke <zecke> | 2003-05-12 13:09:39 (UTC) |
commit | 64f9f964ef690806378d639118f79539a8934d40 (patch) (unidiff) | |
tree | d49f33713aaa4078a9d51ab6f099671c72226eb5 /libopie | |
parent | 41ce7d75884224eb6fff131cc950744cb5adc0e2 (diff) | |
download | opie-64f9f964ef690806378d639118f79539a8934d40.zip opie-64f9f964ef690806378d639118f79539a8934d40.tar.gz opie-64f9f964ef690806378d639118f79539a8934d40.tar.bz2 |
dateTimeFromString
and toString added
-rw-r--r-- | libopie/pim/oconversion.cpp | 43 | ||||
-rw-r--r-- | libopie/pim/oconversion.h | 9 |
2 files changed, 48 insertions, 4 deletions
diff --git a/libopie/pim/oconversion.cpp b/libopie/pim/oconversion.cpp index c3aa89b..0d15414 100644 --- a/libopie/pim/oconversion.cpp +++ b/libopie/pim/oconversion.cpp | |||
@@ -1,76 +1,113 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2003 by Stefan Eilers (eilers.stefan@epost.de) | 2 | ** Copyright (C) 2003 by Stefan Eilers (eilers.stefan@epost.de) |
3 | ** | 3 | ** |
4 | ** This file may be distributed and/or modified under the terms of the | 4 | ** This file may be distributed and/or modified under the terms of the |
5 | ** GNU Lesser General Public License version 2 as published by the Free Software | 5 | ** GNU Lesser General Public License version 2 as published by the Free Software |
6 | ** Foundation and appearing in the file LICENSE.GPL included in the | 6 | ** Foundation and appearing in the file LICENSE.GPL included in the |
7 | ** packaging of this file. | 7 | ** packaging of this file. |
8 | ** | 8 | ** |
9 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 9 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
10 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 10 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
11 | ** | 11 | ** |
12 | **********************************************************************/ | 12 | **********************************************************************/ |
13 | 13 | ||
14 | #include "oconversion.h" | 14 | #include "oconversion.h" |
15 | #include <qpe/timeconversion.h> | 15 | #include <qpe/timeconversion.h> |
16 | 16 | ||
17 | 17 | ||
18 | QString OConversion::dateToString( const QDate &d ) | 18 | QString OConversion::dateToString( const QDate &d ) |
19 | { | 19 | { |
20 | if ( d.isNull() || !d.isValid() ) | 20 | if ( d.isNull() || !d.isValid() ) |
21 | return QString::null; | 21 | return QString::null; |
22 | 22 | ||
23 | // ISO format in year, month, day (YYYYMMDD); e.g. 20021231 | 23 | // ISO format in year, month, day (YYYYMMDD); e.g. 20021231 |
24 | QString year = QString::number( d.year() ); | 24 | QString year = QString::number( d.year() ); |
25 | QString month = QString::number( d.month() ); | 25 | QString month = QString::number( d.month() ); |
26 | month = month.rightJustify( 2, '0' ); | 26 | month = month.rightJustify( 2, '0' ); |
27 | QString day = QString::number( d.day() ); | 27 | QString day = QString::number( d.day() ); |
28 | day = day.rightJustify( 2, '0' ); | 28 | day = day.rightJustify( 2, '0' ); |
29 | 29 | ||
30 | QString str = year + month + day; | 30 | QString str = year + month + day; |
31 | //qDebug( "\tPimContact dateToStr = %s", str.latin1() ); | 31 | //qDebug( "\tPimContact dateToStr = %s", str.latin1() ); |
32 | 32 | ||
33 | return str; | 33 | return str; |
34 | } | 34 | } |
35 | 35 | ||
36 | QDate OConversion::dateFromString( const QString& s ) | 36 | QDate OConversion::dateFromString( const QString& s ) |
37 | { | 37 | { |
38 | QDate date; | 38 | QDate date; |
39 | 39 | ||
40 | if ( s.isEmpty() ) | 40 | if ( s.isEmpty() ) |
41 | return date; | 41 | return date; |
42 | 42 | ||
43 | // Be backward compatible to old Opie format: | 43 | // Be backward compatible to old Opie format: |
44 | // Try to load old format. If it fails, try new ISO-Format! | 44 | // Try to load old format. If it fails, try new ISO-Format! |
45 | date = TimeConversion::fromString ( s ); | 45 | date = TimeConversion::fromString ( s ); |
46 | if ( date.isValid() ) | 46 | if ( date.isValid() ) |
47 | return date; | 47 | return date; |
48 | 48 | ||
49 | // Read ISO-Format (YYYYMMDD) | 49 | // Read ISO-Format (YYYYMMDD) |
50 | int year = s.mid(0, 4).toInt(); | 50 | int year = s.mid(0, 4).toInt(); |
51 | int month = s.mid(4,2).toInt(); | 51 | int month = s.mid(4,2).toInt(); |
52 | int day = s.mid(6,2).toInt(); | 52 | int day = s.mid(6,2).toInt(); |
53 | 53 | ||
54 | // do some quick sanity checking | 54 | // do some quick sanity checking -eilers |
55 | // but we isValid() again? -zecke | ||
55 | if ( year < 1900 || year > 3000 ) { | 56 | if ( year < 1900 || year > 3000 ) { |
56 | qWarning( "PimContact year is not in range"); | 57 | qWarning( "PimContact year is not in range"); |
57 | return date; | 58 | return date; |
58 | } | 59 | } |
59 | if ( month < 0 || month > 12 ) { | 60 | if ( month < 0 || month > 12 ) { |
60 | qWarning( "PimContact month is not in range"); | 61 | qWarning( "PimContact month is not in range"); |
61 | return date; | 62 | return date; |
62 | } | 63 | } |
63 | if ( day < 0 || day > 31 ) { | 64 | if ( day < 0 || day > 31 ) { |
64 | qWarning( "PimContact day is not in range"); | 65 | qWarning( "PimContact day is not in range"); |
65 | return date; | 66 | return date; |
66 | } | 67 | } |
67 | 68 | ||
68 | date.setYMD( year, month, day ); | 69 | date.setYMD( year, month, day ); |
69 | if ( !date.isValid() ) { | 70 | if ( !date.isValid() ) { |
70 | qWarning( "PimContact date is not valid"); | 71 | qWarning( "PimContact date is not valid"); |
71 | return QDate(); | 72 | return date; |
72 | } | 73 | } |
73 | 74 | ||
74 | return date; | 75 | return date; |
75 | } | 76 | } |
77 | QString OConversion::dateTimeToString( const QDateTime& dt ) { | ||
78 | if (!dt.isValid() || dt.isNull() ) return QString::null; | ||
79 | |||
80 | QString year = QString::number( dt.date().year() ); | ||
81 | QString month = QString::number( dt.date().month() ); | ||
82 | QString day = QString::number( dt.date().day() ); | ||
83 | |||
84 | QString hour = QString::number( dt.time().hour() ); | ||
85 | QString min = QString::number( dt.time().minute() ); | ||
86 | QString sec = QString::number( dt.time().second() ); | ||
87 | |||
88 | month = month.rightJustify( 2, '0' ); | ||
89 | day = day. rightJustify( 2, '0' ); | ||
90 | hour = hour. rightJustify( 2, '0' ); | ||
91 | min = min. rightJustify( 2, '0' ); | ||
92 | sec = sec. rightJustify( 2, '0' ); | ||
93 | |||
94 | QString str = day + month + year + hour + min + sec; | ||
95 | |||
96 | return str; | ||
97 | } | ||
98 | QDateTime OConversion::dateTimeFromString( const QString& str) { | ||
99 | |||
100 | if ( str.isEmpty() ) return QDateTime(); | ||
101 | int day = str.mid(0, 2).toInt(); | ||
102 | int month = str.mid(2, 2).toInt(); | ||
103 | int year = str.mid(4, 4).toInt(); | ||
104 | int hour = str.mid(8, 2).toInt(); | ||
105 | int min = str.mid(10, 2).toInt(); | ||
106 | int sec = str.mid(12, 2).toInt(); | ||
107 | |||
108 | QDate date( year, month, day ); | ||
109 | QTime time( hour, min, sec ); | ||
110 | QDateTime dt( date, time ); | ||
111 | return dt; | ||
112 | } | ||
76 | 113 | ||
diff --git a/libopie/pim/oconversion.h b/libopie/pim/oconversion.h index 6540889..13367e1 100644 --- a/libopie/pim/oconversion.h +++ b/libopie/pim/oconversion.h | |||
@@ -1,36 +1,43 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. |
3 | ** Copyright (C) 2002-2003 by Stefan Eilers (eilers.stefan@epost.de) | 3 | ** Copyright (C) 2002-2003 by Stefan Eilers (eilers.stefan@epost.de) |
4 | ** | 4 | ** |
5 | ** This file may be distributed and/or modified under the terms of the | 5 | ** This file may be distributed and/or modified under the terms of the |
6 | ** GNU General Public License version 2 as published by the Free Software | 6 | ** GNU General Public License version 2 as published by the Free Software |
7 | ** Foundation and appearing in the file LICENSE.GPL included in the | 7 | ** Foundation and appearing in the file LICENSE.GPL included in the |
8 | ** packaging of this file. | 8 | ** packaging of this file. |
9 | ** | 9 | ** |
10 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 10 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
11 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 11 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
12 | ** | 12 | ** |
13 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 13 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
14 | ** | 14 | ** |
15 | ** Contact info@trolltech.com if any conditions of this licensing are | 15 | ** Contact info@trolltech.com if any conditions of this licensing are |
16 | ** not clear to you. | 16 | ** not clear to you. |
17 | **********************************************************************/ | 17 | **********************************************************************/ |
18 | 18 | ||
19 | #ifndef __oconversion_h__ | 19 | #ifndef __oconversion_h__ |
20 | #define __oconversion_h__ | 20 | #define __oconversion_h__ |
21 | 21 | ||
22 | /* #include <time.h> */ | 22 | /* #include <time.h> */ |
23 | /* #include <sys/types.h> */ | 23 | /* #include <sys/types.h> */ |
24 | #include <qdatetime.h> | 24 | #include <qdatetime.h> |
25 | 25 | ||
26 | 26 | /* FIXME namespace? -zecke */ | |
27 | class OConversion | 27 | class OConversion |
28 | { | 28 | { |
29 | public: | 29 | public: |
30 | static QString dateToString( const QDate &d ); | 30 | static QString dateToString( const QDate &d ); |
31 | static QDate dateFromString( const QString &datestr ); | 31 | static QDate dateFromString( const QString &datestr ); |
32 | 32 | ||
33 | /** | ||
34 | * simple function to store DateTime as string and read from string | ||
35 | * no timezone changing is done | ||
36 | * DDMMYYYYHHMMSS is the simple format | ||
37 | */ | ||
38 | static QString dateTimeToString( const QDateTime& ); | ||
39 | static QDateTime dateTimeFromString( const QString& ); | ||
33 | }; | 40 | }; |
34 | 41 | ||
35 | #endif // __oconversion_h__ | 42 | #endif // __oconversion_h__ |
36 | 43 | ||