author | zecke <zecke> | 2002-09-24 13:37:32 (UTC) |
---|---|---|
committer | zecke <zecke> | 2002-09-24 13:37:32 (UTC) |
commit | 70328f952e475eb7eb0bc961420b875eefd05211 (patch) (unidiff) | |
tree | b9270f878b00dd8f8d525a82897f984ba6f5a484 /library/backend | |
parent | 5d29b14fc85d99d45a32fdaed27653c6f271a5ce (diff) | |
download | opie-70328f952e475eb7eb0bc961420b875eefd05211.zip opie-70328f952e475eb7eb0bc961420b875eefd05211.tar.gz opie-70328f952e475eb7eb0bc961420b875eefd05211.tar.bz2 |
make thetime
not a pointer
and memset it
this makes valgrind shut up
-rw-r--r-- | library/backend/timeconversion.cpp | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/library/backend/timeconversion.cpp b/library/backend/timeconversion.cpp index ef7762d..3c25922 100644 --- a/library/backend/timeconversion.cpp +++ b/library/backend/timeconversion.cpp | |||
@@ -1,237 +1,238 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | 20 | ||
21 | #include <qglobal.h> | 21 | #include <qglobal.h> |
22 | #include <qtopia/timeconversion.h> | 22 | #include <qtopia/timeconversion.h> |
23 | #include <qregexp.h> | 23 | #include <qregexp.h> |
24 | #include <stdlib.h> | 24 | #include <stdlib.h> |
25 | 25 | ||
26 | QString TimeConversion::toString( const QDate &d ) | 26 | QString TimeConversion::toString( const QDate &d ) |
27 | { | 27 | { |
28 | QString r = QString::number( d.day() ) + "." + | 28 | QString r = QString::number( d.day() ) + "." + |
29 | QString::number( d.month() ) + "." + | 29 | QString::number( d.month() ) + "." + |
30 | QString::number( d.year() ); | 30 | QString::number( d.year() ); |
31 | //qDebug("TimeConversion::toString %s", r.latin1()); | 31 | //qDebug("TimeConversion::toString %s", r.latin1()); |
32 | return r; | 32 | return r; |
33 | } | 33 | } |
34 | 34 | ||
35 | QDate TimeConversion::fromString( const QString &datestr ) | 35 | QDate TimeConversion::fromString( const QString &datestr ) |
36 | { | 36 | { |
37 | int monthPos = datestr.find('.'); | 37 | int monthPos = datestr.find('.'); |
38 | int yearPos = datestr.find('.', monthPos+1 ); | 38 | int yearPos = datestr.find('.', monthPos+1 ); |
39 | if ( monthPos == -1 || yearPos == -1 ) { | 39 | if ( monthPos == -1 || yearPos == -1 ) { |
40 | qDebug("fromString didn't find . in str = %s; mpos = %d ypos = %d", datestr.latin1(), monthPos, yearPos ); | 40 | qDebug("fromString didn't find . in str = %s; mpos = %d ypos = %d", datestr.latin1(), monthPos, yearPos ); |
41 | return QDate(); | 41 | return QDate(); |
42 | } | 42 | } |
43 | int d = datestr.left( monthPos ).toInt(); | 43 | int d = datestr.left( monthPos ).toInt(); |
44 | int m = datestr.mid( monthPos+1, yearPos - monthPos - 1 ).toInt(); | 44 | int m = datestr.mid( monthPos+1, yearPos - monthPos - 1 ).toInt(); |
45 | int y = datestr.mid( yearPos+1 ).toInt(); | 45 | int y = datestr.mid( yearPos+1 ).toInt(); |
46 | QDate date ( y,m,d ); | 46 | QDate date ( y,m,d ); |
47 | //qDebug("TimeConversion::fromString ymd = %s => %d %d %d; mpos = %d ypos = %d", datestr.latin1(), y, m, d, monthPos, yearPos); | 47 | //qDebug("TimeConversion::fromString ymd = %s => %d %d %d; mpos = %d ypos = %d", datestr.latin1(), y, m, d, monthPos, yearPos); |
48 | return date; | 48 | return date; |
49 | } | 49 | } |
50 | 50 | ||
51 | time_t TimeConversion::toUTC( const QDateTime& dt ) | 51 | time_t TimeConversion::toUTC( const QDateTime& dt ) |
52 | { | 52 | { |
53 | time_t tmp; | 53 | time_t tmp; |
54 | struct tm *lt; | 54 | struct tm *lt; |
55 | 55 | ||
56 | #if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64) | 56 | #if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64) |
57 | _tzset(); | 57 | _tzset(); |
58 | #else | 58 | #else |
59 | tzset(); | 59 | tzset(); |
60 | #endif | 60 | #endif |
61 | 61 | ||
62 | // get a tm structure from the system to get the correct tz_name | 62 | // get a tm structure from the system to get the correct tz_name |
63 | tmp = time( 0 ); | 63 | tmp = time( 0 ); |
64 | lt = localtime( &tmp ); | 64 | lt = localtime( &tmp ); |
65 | 65 | ||
66 | lt->tm_sec = dt.time().second(); | 66 | lt->tm_sec = dt.time().second(); |
67 | lt->tm_min = dt.time().minute(); | 67 | lt->tm_min = dt.time().minute(); |
68 | lt->tm_hour = dt.time().hour(); | 68 | lt->tm_hour = dt.time().hour(); |
69 | lt->tm_mday = dt.date().day(); | 69 | lt->tm_mday = dt.date().day(); |
70 | lt->tm_mon = dt.date().month() - 1; // 0-11 instead of 1-12 | 70 | lt->tm_mon = dt.date().month() - 1; // 0-11 instead of 1-12 |
71 | lt->tm_year = dt.date().year() - 1900; // year - 1900 | 71 | lt->tm_year = dt.date().year() - 1900; // year - 1900 |
72 | //lt->tm_wday = dt.date().dayOfWeek(); ignored anyway | 72 | //lt->tm_wday = dt.date().dayOfWeek(); ignored anyway |
73 | //lt->tm_yday = dt.date().dayOfYear(); ignored anyway | 73 | //lt->tm_yday = dt.date().dayOfYear(); ignored anyway |
74 | lt->tm_wday = -1; | 74 | lt->tm_wday = -1; |
75 | lt->tm_yday = -1; | 75 | lt->tm_yday = -1; |
76 | // tm_isdst negative -> mktime will find out about DST | 76 | // tm_isdst negative -> mktime will find out about DST |
77 | lt->tm_isdst = -1; | 77 | lt->tm_isdst = -1; |
78 | // keep tm_zone and tm_gmtoff | 78 | // keep tm_zone and tm_gmtoff |
79 | tmp = mktime( lt ); | 79 | tmp = mktime( lt ); |
80 | return tmp; | 80 | return tmp; |
81 | } | 81 | } |
82 | 82 | ||
83 | QDateTime TimeConversion::fromUTC( time_t time ) | 83 | QDateTime TimeConversion::fromUTC( time_t time ) |
84 | { | 84 | { |
85 | struct tm *lt; | 85 | struct tm *lt; |
86 | 86 | ||
87 | #if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64) | 87 | #if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64) |
88 | _tzset(); | 88 | _tzset(); |
89 | #else | 89 | #else |
90 | tzset(); | 90 | tzset(); |
91 | #endif | 91 | #endif |
92 | lt = localtime( &time ); | 92 | lt = localtime( &time ); |
93 | QDateTime dt; | 93 | QDateTime dt; |
94 | dt.setDate( QDate( lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday ) ); | 94 | dt.setDate( QDate( lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday ) ); |
95 | dt.setTime( QTime( lt->tm_hour, lt->tm_min, lt->tm_sec ) ); | 95 | dt.setTime( QTime( lt->tm_hour, lt->tm_min, lt->tm_sec ) ); |
96 | return dt; | 96 | return dt; |
97 | } | 97 | } |
98 | 98 | ||
99 | 99 | ||
100 | int TimeConversion::secsTo( const QDateTime &from, const QDateTime &to ) | 100 | int TimeConversion::secsTo( const QDateTime &from, const QDateTime &to ) |
101 | { | 101 | { |
102 | return toUTC( to ) - toUTC( from ); | 102 | return toUTC( to ) - toUTC( from ); |
103 | } | 103 | } |
104 | 104 | ||
105 | QCString TimeConversion::toISO8601( const QDate &d ) | 105 | QCString TimeConversion::toISO8601( const QDate &d ) |
106 | { | 106 | { |
107 | time_t tmp = toUTC( d ); | 107 | time_t tmp = toUTC( d ); |
108 | struct tm *utc = gmtime( &tmp ); | 108 | struct tm *utc = gmtime( &tmp ); |
109 | 109 | ||
110 | QCString str; | 110 | QCString str; |
111 | str.sprintf("%04d%02d%02d", (utc->tm_year + 1900), utc->tm_mon+1, utc->tm_mday ); | 111 | str.sprintf("%04d%02d%02d", (utc->tm_year + 1900), utc->tm_mon+1, utc->tm_mday ); |
112 | return str; | 112 | return str; |
113 | } | 113 | } |
114 | 114 | ||
115 | QCString TimeConversion::toISO8601( const QDateTime &dt ) | 115 | QCString TimeConversion::toISO8601( const QDateTime &dt ) |
116 | { | 116 | { |
117 | time_t tmp = toUTC( dt ); | 117 | time_t tmp = toUTC( dt ); |
118 | struct tm *utc = gmtime( &tmp ); | 118 | struct tm *utc = gmtime( &tmp ); |
119 | 119 | ||
120 | QCString str; | 120 | QCString str; |
121 | str.sprintf("%04d%02d%02dT%02d%02d%02dZ", | 121 | str.sprintf("%04d%02d%02dT%02d%02d%02dZ", |
122 | (utc->tm_year + 1900), utc->tm_mon+1, utc->tm_mday, | 122 | (utc->tm_year + 1900), utc->tm_mon+1, utc->tm_mday, |
123 | utc->tm_hour, utc->tm_min, utc->tm_sec ); | 123 | utc->tm_hour, utc->tm_min, utc->tm_sec ); |
124 | return str; | 124 | return str; |
125 | } | 125 | } |
126 | 126 | ||
127 | QDateTime TimeConversion::fromISO8601( const QCString &s ) | 127 | QDateTime TimeConversion::fromISO8601( const QCString &s ) |
128 | { | 128 | { |
129 | 129 | ||
130 | #if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64) | 130 | #if defined(_OS_WIN32) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64) |
131 | _tzset(); | 131 | _tzset(); |
132 | #else | 132 | #else |
133 | tzset(); | 133 | tzset(); |
134 | #endif | 134 | #endif |
135 | 135 | ||
136 | struct tm *thetime = new tm; | 136 | struct tm thetime; |
137 | 137 | ||
138 | QCString str = s.copy(); | 138 | QCString str = s.copy(); |
139 | str.replace(QRegExp("-"), "" ); | 139 | str.replace(QRegExp("-"), "" ); |
140 | str.replace(QRegExp(":"), "" ); | 140 | str.replace(QRegExp(":"), "" ); |
141 | str.stripWhiteSpace(); | 141 | str.stripWhiteSpace(); |
142 | str = str.lower(); | 142 | str = str.lower(); |
143 | 143 | ||
144 | int i = str.find( "t" ); | 144 | int i = str.find( "t" ); |
145 | QCString date; | 145 | QCString date; |
146 | QCString timestr; | 146 | QCString timestr; |
147 | if ( i != -1 ) { | 147 | if ( i != -1 ) { |
148 | date = str.left( i ); | 148 | date = str.left( i ); |
149 | timestr = str.mid( i+1 ); | 149 | timestr = str.mid( i+1 ); |
150 | } else { | 150 | } else { |
151 | date = str; | 151 | date = str; |
152 | } | 152 | } |
153 | 153 | ||
154 | // qDebug("--- parsing ISO time---"); | 154 | // qDebug("--- parsing ISO time---"); |
155 | thetime->tm_year = 100; | 155 | memset( &thetime, 0, sizeof(tm) ); |
156 | thetime->tm_mon = 0; | 156 | thetime.tm_year = 100; |
157 | thetime->tm_mday = 0; | 157 | thetime.tm_mon = 0; |
158 | thetime->tm_hour = 0; | 158 | thetime.tm_mday = 0; |
159 | thetime->tm_min = 0; | 159 | thetime.tm_hour = 0; |
160 | thetime->tm_sec = 0; | 160 | thetime.tm_min = 0; |
161 | thetime.tm_sec = 0; | ||
161 | 162 | ||
162 | // qDebug("date = %s", date.data() ); | 163 | // qDebug("date = %s", date.data() ); |
163 | 164 | ||
164 | switch( date.length() ) { | 165 | switch( date.length() ) { |
165 | case 8: | 166 | case 8: |
166 | thetime->tm_mday = date.right( 2 ).toInt(); | 167 | thetime.tm_mday = date.right( 2 ).toInt(); |
167 | case 6: | 168 | case 6: |
168 | thetime->tm_mon = date.mid( 4, 2 ).toInt() - 1; | 169 | thetime.tm_mon = date.mid( 4, 2 ).toInt() - 1; |
169 | case 4: | 170 | case 4: |
170 | thetime->tm_year = date.left( 4 ).toInt(); | 171 | thetime.tm_year = date.left( 4 ).toInt(); |
171 | thetime->tm_year -= 1900; | 172 | thetime.tm_year -= 1900; |
172 | break; | 173 | break; |
173 | default: | 174 | default: |
174 | break; | 175 | break; |
175 | } | 176 | } |
176 | 177 | ||
177 | int tzoff = 0; | 178 | int tzoff = 0; |
178 | bool inLocalTime = FALSE; | 179 | bool inLocalTime = FALSE; |
179 | if ( timestr.find( 'z' ) == (int)timestr.length() - 1 ) | 180 | if ( timestr.find( 'z' ) == (int)timestr.length() - 1 ) |
180 | // UTC | 181 | // UTC |
181 | timestr = timestr.left( timestr.length() -1 ); | 182 | timestr = timestr.left( timestr.length() -1 ); |
182 | else { | 183 | else { |
183 | int plus = timestr.find( "+" ); | 184 | int plus = timestr.find( "+" ); |
184 | int minus = timestr.find( "-" ); | 185 | int minus = timestr.find( "-" ); |
185 | if ( plus != -1 || minus != -1 ) { | 186 | if ( plus != -1 || minus != -1 ) { |
186 | // have a timezone offset | 187 | // have a timezone offset |
187 | plus = (plus != -1) ? plus : minus; | 188 | plus = (plus != -1) ? plus : minus; |
188 | QCString off = timestr.mid( plus ); | 189 | QCString off = timestr.mid( plus ); |
189 | timestr = timestr.left( plus ); | 190 | timestr = timestr.left( plus ); |
190 | 191 | ||
191 | int tzoffhour = 0; | 192 | int tzoffhour = 0; |
192 | int tzoffmin = 0; | 193 | int tzoffmin = 0; |
193 | switch( off.length() ) { | 194 | switch( off.length() ) { |
194 | case 5: | 195 | case 5: |
195 | tzoffmin = off.mid(3).toInt(); | 196 | tzoffmin = off.mid(3).toInt(); |
196 | case 3: | 197 | case 3: |
197 | tzoffhour = off.left(3).toInt(); | 198 | tzoffhour = off.left(3).toInt(); |
198 | default: | 199 | default: |
199 | break; | 200 | break; |
200 | } | 201 | } |
201 | tzoff = 60*tzoffhour + tzoffmin; | 202 | tzoff = 60*tzoffhour + tzoffmin; |
202 | } else | 203 | } else |
203 | inLocalTime = TRUE; | 204 | inLocalTime = TRUE; |
204 | } | 205 | } |
205 | 206 | ||
206 | // get the time: | 207 | // get the time: |
207 | switch( timestr.length() ) { | 208 | switch( timestr.length() ) { |
208 | case 6: | 209 | case 6: |
209 | thetime->tm_sec = timestr.mid( 4 ).toInt(); | 210 | thetime.tm_sec = timestr.mid( 4 ).toInt(); |
210 | case 4: | 211 | case 4: |
211 | thetime->tm_min = timestr.mid( 2, 2 ).toInt(); | 212 | thetime.tm_min = timestr.mid( 2, 2 ).toInt(); |
212 | case 2: | 213 | case 2: |
213 | thetime->tm_hour = timestr.left( 2 ).toInt(); | 214 | thetime.tm_hour = timestr.left( 2 ).toInt(); |
214 | default: | 215 | default: |
215 | break; | 216 | break; |
216 | } | 217 | } |
217 | 218 | ||
218 | int tzloc = 0; | 219 | int tzloc = 0; |
219 | time_t tmp = time( 0 ); | 220 | time_t tmp = time( 0 ); |
220 | if ( !inLocalTime ) { | 221 | if ( !inLocalTime ) { |
221 | // have to get the offset between gmt and local time | 222 | // have to get the offset between gmt and local time |
222 | struct tm *lt = localtime( &tmp ); | 223 | struct tm *lt = localtime( &tmp ); |
223 | tzloc = mktime( lt ); | 224 | tzloc = mktime( lt ); |
224 | struct tm *ut = gmtime( &tmp ); | 225 | struct tm *ut = gmtime( &tmp ); |
225 | tzloc -= mktime( ut ); | 226 | tzloc -= mktime( ut ); |
226 | } | 227 | } |
227 | // qDebug("time: %d %d %d, tzloc=%d, tzoff=%d", thetime->tm_hour, thetime->tm_min, thetime->tm_sec, | 228 | // qDebug("time: %d %d %d, tzloc=%d, tzoff=%d", thetime->tm_hour, thetime->tm_min, thetime->tm_sec, |
228 | // tzloc, tzoff ); | 229 | // tzloc, tzoff ); |
229 | 230 | ||
230 | tmp = mktime( thetime ); | 231 | tmp = mktime( &thetime ); |
231 | tmp += 60*(-tzloc + tzoff); | 232 | tmp += 60*(-tzloc + tzoff); |
232 | 233 | ||
233 | delete thetime; | 234 | |
234 | 235 | ||
235 | return fromUTC( tmp ); | 236 | return fromUTC( tmp ); |
236 | } | 237 | } |
237 | 238 | ||