-rw-r--r-- | libopie/pim/oconversion.cpp | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/libopie/pim/oconversion.cpp b/libopie/pim/oconversion.cpp new file mode 100644 index 0000000..c3aa89b --- a/dev/null +++ b/libopie/pim/oconversion.cpp | |||
@@ -0,0 +1,76 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2003 by Stefan Eilers (eilers.stefan@epost.de) | ||
3 | ** | ||
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 | ||
6 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
7 | ** packaging of this file. | ||
8 | ** | ||
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. | ||
11 | ** | ||
12 | **********************************************************************/ | ||
13 | |||
14 | #include "oconversion.h" | ||
15 | #include <qpe/timeconversion.h> | ||
16 | |||
17 | |||
18 | QString OConversion::dateToString( const QDate &d ) | ||
19 | { | ||
20 | if ( d.isNull() || !d.isValid() ) | ||
21 | return QString::null; | ||
22 | |||
23 | // ISO format in year, month, day (YYYYMMDD); e.g. 20021231 | ||
24 | QString year = QString::number( d.year() ); | ||
25 | QString month = QString::number( d.month() ); | ||
26 | month = month.rightJustify( 2, '0' ); | ||
27 | QString day = QString::number( d.day() ); | ||
28 | day = day.rightJustify( 2, '0' ); | ||
29 | |||
30 | QString str = year + month + day; | ||
31 | //qDebug( "\tPimContact dateToStr = %s", str.latin1() ); | ||
32 | |||
33 | return str; | ||
34 | } | ||
35 | |||
36 | QDate OConversion::dateFromString( const QString& s ) | ||
37 | { | ||
38 | QDate date; | ||
39 | |||
40 | if ( s.isEmpty() ) | ||
41 | return date; | ||
42 | |||
43 | // Be backward compatible to old Opie format: | ||
44 | // Try to load old format. If it fails, try new ISO-Format! | ||
45 | date = TimeConversion::fromString ( s ); | ||
46 | if ( date.isValid() ) | ||
47 | return date; | ||
48 | |||
49 | // Read ISO-Format (YYYYMMDD) | ||
50 | int year = s.mid(0, 4).toInt(); | ||
51 | int month = s.mid(4,2).toInt(); | ||
52 | int day = s.mid(6,2).toInt(); | ||
53 | |||
54 | // do some quick sanity checking | ||
55 | if ( year < 1900 || year > 3000 ) { | ||
56 | qWarning( "PimContact year is not in range"); | ||
57 | return date; | ||
58 | } | ||
59 | if ( month < 0 || month > 12 ) { | ||
60 | qWarning( "PimContact month is not in range"); | ||
61 | return date; | ||
62 | } | ||
63 | if ( day < 0 || day > 31 ) { | ||
64 | qWarning( "PimContact day is not in range"); | ||
65 | return date; | ||
66 | } | ||
67 | |||
68 | date.setYMD( year, month, day ); | ||
69 | if ( !date.isValid() ) { | ||
70 | qWarning( "PimContact date is not valid"); | ||
71 | return QDate(); | ||
72 | } | ||
73 | |||
74 | return date; | ||
75 | } | ||
76 | |||