summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/core/oconversion.cpp
Unidiff
Diffstat (limited to 'libopie2/opiepim/core/oconversion.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/core/oconversion.cpp213
1 files changed, 112 insertions, 101 deletions
diff --git a/libopie2/opiepim/core/oconversion.cpp b/libopie2/opiepim/core/oconversion.cpp
index b7eebef..160c2c6 100644
--- a/libopie2/opiepim/core/oconversion.cpp
+++ b/libopie2/opiepim/core/oconversion.cpp
@@ -1,30 +1,30 @@
1/* 1/*
2 This file is part of the Opie Project 2 This file is part of the Opie Project
3 Copyright (C) The Main Author <main-author@whereever.org> 3 =. Copyright (C) The Opie Team <opie-devel@handhelds.org>
4 =. Copyright (C) The Opie Team <opie-devel@handhelds.org> 4 .=l.
5 .=l. 5 .>+-=
6 .>+-= 6_;:, .> :=|. This program is free software; you can
7 _;:, .> :=|. This program is free software; you can 7.> <`_, > . <= redistribute it and/or modify it under
8.> <`_, > . <= redistribute it and/or modify it under 8:`=1 )Y*s>-.-- : the terms of the GNU Library General Public
9:`=1 )Y*s>-.-- : the terms of the GNU Library General Public 9.="- .-=="i, .._ License as published by the Free Software
10.="- .-=="i, .._ License as published by the Free Software 10- . .-<_> .<> Foundation; either version 2 of the License,
11 - . .-<_> .<> Foundation; either version 2 of the License, 11 ._= =} : or (at your option) any later version.
12 ._= =} : or (at your option) any later version. 12 .%`+i> _;_.
13 .%`+i> _;_. 13 .i_,=:_. -<s. This program is distributed in the hope that
14 .i_,=:_. -<s. This program is distributed in the hope that 14 + . -:. = it will be useful, but WITHOUT ANY WARRANTY;
15 + . -:. = it will be useful, but WITHOUT ANY WARRANTY; 15 : .. .:, . . . without even the implied warranty of
16 : .. .:, . . . without even the implied warranty of 16 =_ + =;=|` MERCHANTABILITY or FITNESS FOR A
17 =_ + =;=|` MERCHANTABILITY or FITNESS FOR A 17 _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU
18 _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU 18..}^=.= = ; Library General Public License for more
19..}^=.= = ; Library General Public License for more 19++= -. .` .: details.
20++= -. .` .: details. 20: = ...= . :.=-
21 : = ...= . :.=- 21-. .:....=;==+<; You should have received a copy of the GNU
22 -. .:....=;==+<; You should have received a copy of the GNU 22 -_. . . )=. = Library General Public License along with
23 -_. . . )=. = Library General Public License along with 23 -- :-=` this library; see the file COPYING.LIB.
24 -- :-=` this library; see the file COPYING.LIB. 24 If not, write to the Free Software Foundation,
25 If not, write to the Free Software Foundation, 25 Inc., 59 Temple Place - Suite 330,
26 Inc., 59 Temple Place - Suite 330, 26 Boston, MA 02111-1307, USA.
27 Boston, MA 02111-1307, USA.
28*/ 27*/
29 28
29/* OPIE */
30#include <opie2/oconversion.h> 30#include <opie2/oconversion.h>
@@ -32,4 +32,4 @@
32 32
33 33namespace Opie
34namespace Opie { 34{
35 35
@@ -37,89 +37,100 @@ QString OConversion::dateToString( const QDate &d )
37{ 37{
38 if ( d.isNull() || !d.isValid() ) 38 if ( d.isNull() || !d.isValid() )
39 return QString::null; 39 return QString::null;
40 40
41 // ISO format in year, month, day (YYYYMMDD); e.g. 20021231 41 // ISO format in year, month, day (YYYYMMDD); e.g. 20021231
42 QString year = QString::number( d.year() ); 42 QString year = QString::number( d.year() );
43 QString month = QString::number( d.month() ); 43 QString month = QString::number( d.month() );
44 month = month.rightJustify( 2, '0' ); 44 month = month.rightJustify( 2, '0' );
45 QString day = QString::number( d.day() ); 45 QString day = QString::number( d.day() );
46 day = day.rightJustify( 2, '0' ); 46 day = day.rightJustify( 2, '0' );
47 47
48 QString str = year + month + day; 48 QString str = year + month + day;
49 //qDebug( "\tPimContact dateToStr = %s", str.latin1() ); 49 //qDebug( "\tPimContact dateToStr = %s", str.latin1() );
50 50
51 return str; 51 return str;
52} 52}
53 53
54
54QDate OConversion::dateFromString( const QString& s ) 55QDate OConversion::dateFromString( const QString& s )
55{ 56{
56 QDate date; 57 QDate date;
57 58
58 if ( s.isEmpty() ) 59 if ( s.isEmpty() )
59 return date; 60 return date;
60 61
61 // Be backward compatible to old Opie format: 62 // Be backward compatible to old Opie format:
62 // Try to load old format. If it fails, try new ISO-Format! 63 // Try to load old format. If it fails, try new ISO-Format!
63 date = TimeConversion::fromString ( s ); 64 date = TimeConversion::fromString ( s );
64 if ( date.isValid() ) 65 if ( date.isValid() )
65 return date; 66 return date;
66 67
67 // Read ISO-Format (YYYYMMDD) 68 // Read ISO-Format (YYYYMMDD)
68 int year = s.mid(0, 4).toInt(); 69 int year = s.mid( 0, 4 ).toInt();
69 int month = s.mid(4,2).toInt(); 70 int month = s.mid( 4, 2 ).toInt();
70 int day = s.mid(6,2).toInt(); 71 int day = s.mid( 6, 2 ).toInt();
71 72
72 // do some quick sanity checking -eilers 73 // do some quick sanity checking -eilers
73 // but we isValid() again? -zecke 74 // but we isValid() again? -zecke
74 if ( year < 1900 || year > 3000 ) { 75 if ( year < 1900 || year > 3000 )
75 qWarning( "PimContact year is not in range"); 76 {
76 return date; 77 qWarning( "PimContact year is not in range" );
77 } 78 return date;
78 if ( month < 0 || month > 12 ) { 79 }
79 qWarning( "PimContact month is not in range"); 80 if ( month < 0 || month > 12 )
80 return date; 81 {
81 } 82 qWarning( "PimContact month is not in range" );
82 if ( day < 0 || day > 31 ) { 83 return date;
83 qWarning( "PimContact day is not in range"); 84 }
84 return date; 85 if ( day < 0 || day > 31 )
85 } 86 {
86 87 qWarning( "PimContact day is not in range" );
87 date.setYMD( year, month, day ); 88 return date;
88 if ( !date.isValid() ) { 89 }
89 qWarning( "PimContact date is not valid"); 90
90 return date; 91 date.setYMD( year, month, day );
91 } 92 if ( !date.isValid() )
92 93 {
93 return date; 94 qWarning( "PimContact date is not valid" );
95 return date;
96 }
97
98 return date;
94} 99}
95QString OConversion::dateTimeToString( const QDateTime& dt ) {
96 if (!dt.isValid() || dt.isNull() ) return QString::null;
97 100
98 QString year = QString::number( dt.date().year() );
99 QString month = QString::number( dt.date().month() );
100 QString day = QString::number( dt.date().day() );
101 101
102 QString hour = QString::number( dt.time().hour() ); 102QString OConversion::dateTimeToString( const QDateTime& dt )
103 QString min = QString::number( dt.time().minute() ); 103{
104 QString sec = QString::number( dt.time().second() ); 104 if ( !dt.isValid() || dt.isNull() )
105 return QString::null;
105 106
106 month = month.rightJustify( 2, '0' ); 107 QString year = QString::number( dt.date().year() );
107 day = day. rightJustify( 2, '0' ); 108 QString month = QString::number( dt.date().month() );
108 hour = hour. rightJustify( 2, '0' ); 109 QString day = QString::number( dt.date().day() );
109 min = min. rightJustify( 2, '0' );
110 sec = sec. rightJustify( 2, '0' );
111 110
112 QString str = day + month + year + hour + min + sec; 111 QString hour = QString::number( dt.time().hour() );
112 QString min = QString::number( dt.time().minute() );
113 QString sec = QString::number( dt.time().second() );
113 114
114 return str; 115 month = month.rightJustify( 2, '0' );
116 day = day. rightJustify( 2, '0' );
117 hour = hour. rightJustify( 2, '0' );
118 min = min. rightJustify( 2, '0' );
119 sec = sec. rightJustify( 2, '0' );
120
121 return day + month + year + hour + min + sec;
115} 122}
116QDateTime OConversion::dateTimeFromString( const QString& str) {
117 123
118 if ( str.isEmpty() ) return QDateTime(); 124
119 int day = str.mid(0, 2).toInt(); 125QDateTime OConversion::dateTimeFromString( const QString& str )
120 int month = str.mid(2, 2).toInt(); 126{
121 int year = str.mid(4, 4).toInt(); 127
122 int hour = str.mid(8, 2).toInt(); 128 if ( str.isEmpty() )
123 int min = str.mid(10, 2).toInt(); 129 return QDateTime();
124 int sec = str.mid(12, 2).toInt(); 130 int day = str.mid( 0, 2 ).toInt();
131 int month = str.mid( 2, 2 ).toInt();
132 int year = str.mid( 4, 4 ).toInt();
133 int hour = str.mid( 8, 2 ).toInt();
134 int min = str.mid( 10, 2 ).toInt();
135 int sec = str.mid( 12, 2 ).toInt();
125 136
@@ -131,2 +142,2 @@ QDateTime OConversion::dateTimeFromString( const QString& str) {
131 142
132} \ No newline at end of file 143}