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