author | zecke <zecke> | 2004-11-18 21:45:49 (UTC) |
---|---|---|
committer | zecke <zecke> | 2004-11-18 21:45:49 (UTC) |
commit | 7484344ff5be1f7c54e51715776d0e3cadeb1ed0 (patch) (unidiff) | |
tree | 96f427f7a1fb1c8ca0a6efbd72b51e916cb1651d /libopie2/opiepim/core/opimtimezone.cpp | |
parent | 3302eb30390e6053637929316670da3e8fbe279e (diff) | |
download | opie-7484344ff5be1f7c54e51715776d0e3cadeb1ed0.zip opie-7484344ff5be1f7c54e51715776d0e3cadeb1ed0.tar.gz opie-7484344ff5be1f7c54e51715776d0e3cadeb1ed0.tar.bz2 |
Big PIM API Update Core Part (1/2 of what should be implemented):
OPimRecords:
-Add a so called safeCast using the rtti value
OPimTodo:
-Fix memleak with OPimState
OPimOccurrence:
-New class. Every 'Access' can give occurrences
for a period of time
Move Documentation
Diffstat (limited to 'libopie2/opiepim/core/opimtimezone.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie2/opiepim/core/opimtimezone.cpp | 44 |
1 files changed, 29 insertions, 15 deletions
diff --git a/libopie2/opiepim/core/opimtimezone.cpp b/libopie2/opiepim/core/opimtimezone.cpp index 5b32b1f..1dc36b4 100644 --- a/libopie2/opiepim/core/opimtimezone.cpp +++ b/libopie2/opiepim/core/opimtimezone.cpp | |||
@@ -1,193 +1,207 @@ | |||
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 Main Author <main-author@whereever.org> |
4 | =. Copyright (C) The Opie Team <opie-devel@handhelds.org> | 4 | =. Copyright (C) The Opie Team <opie-devel@handhelds.org> |
5 | .=l. | 5 | .=l. |
6 | .>+-= | 6 | .>+-= |
7 | _;:, .> :=|. This program is free software; you can | 7 | _;:, .> :=|. This program is free software; you can |
8 | .> <`_, > . <= redistribute it and/or modify it under | 8 | .> <`_, > . <= redistribute it and/or modify it under |
9 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | 9 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public |
10 | .="- .-=="i, .._ License as published by the Free Software | 10 | .="- .-=="i, .._ License as published by the Free Software |
11 | - . .-<_> .<> Foundation; either version 2 of the License, | 11 | - . .-<_> .<> Foundation; either version 2 of the License, |
12 | ._= =} : or (at your option) any later version. | 12 | ._= =} : or (at your option) any later version. |
13 | .%`+i> _;_. | 13 | .%`+i> _;_. |
14 | .i_,=:_. -<s. This program is distributed in the hope that | 14 | .i_,=:_. -<s. This program is distributed in the hope that |
15 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | 15 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; |
16 | : .. .:, . . . without even the implied warranty of | 16 | : .. .:, . . . without even the implied warranty of |
17 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | 17 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A |
18 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | 18 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU |
19 | ..}^=.= = ; Library General Public License for more | 19 | ..}^=.= = ; Library General Public License for more |
20 | ++= -. .` .: details. | 20 | ++= -. .` .: details. |
21 | : = ...= . :.=- | 21 | : = ...= . :.=- |
22 | -. .:....=;==+<; You should have received a copy of the GNU | 22 | -. .:....=;==+<; You should have received a copy of the GNU |
23 | -_. . . )=. = Library General Public License along with | 23 | -_. . . )=. = Library General Public License along with |
24 | -- :-=` this library; see the file COPYING.LIB. | 24 | -- :-=` this library; see the file COPYING.LIB. |
25 | If not, write to the Free Software Foundation, | 25 | If not, write to the Free Software Foundation, |
26 | Inc., 59 Temple Place - Suite 330, | 26 | Inc., 59 Temple Place - Suite 330, |
27 | Boston, MA 02111-1307, USA. | 27 | Boston, MA 02111-1307, USA. |
28 | */ | 28 | */ |
29 | 29 | ||
30 | #include "opimtimezone.h" | 30 | #include "opimtimezone.h" |
31 | 31 | ||
32 | /* OPIE */ | 32 | /* OPIE */ |
33 | #include <opie2/odebug.h> | 33 | #include <opie2/odebug.h> |
34 | 34 | ||
35 | /* STD */ | 35 | /* STD */ |
36 | #include <stdio.h> | 36 | #include <stdio.h> |
37 | #include <stdlib.h> | 37 | #include <stdlib.h> |
38 | #include <sys/types.h> | 38 | #include <sys/types.h> |
39 | 39 | ||
40 | namespace Opie | 40 | namespace Opie |
41 | { | 41 | { |
42 | 42 | ||
43 | QDateTime utcTime( time_t t ) | 43 | /* |
44 | * Save the old timeZone in a secure way (NULL Pointer check), | ||
45 | * set the new timeZone from the parameter, call tzset | ||
46 | * and then return the old timezone | ||
47 | */ | ||
48 | static QString setTimeZone( const QString& zone) { | ||
49 | QString old; | ||
50 | char *org = ::getenv( "TZ" ); | ||
51 | if( org ) | ||
52 | old = QString::fromLocal8Bit( org ); | ||
53 | |||
54 | ::setenv( "TZ", zone.local8Bit(), true ); | ||
55 | ::tzset(); | ||
56 | |||
57 | return old; | ||
58 | } | ||
59 | |||
60 | static void resetTimeZone( const QString& zone ) { | ||
61 | ::setenv( "TZ", zone.local8Bit(), true ); | ||
62 | } | ||
63 | |||
64 | static QDateTime utcTime( time_t t ) | ||
44 | { | 65 | { |
45 | tm * broken = ::gmtime( &t ); | 66 | tm * broken = ::gmtime( &t ); |
46 | QDateTime ret; | 67 | QDateTime ret; |
47 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon + 1, broken->tm_mday ) ); | 68 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon + 1, broken->tm_mday ) ); |
48 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); | 69 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); |
49 | return ret; | 70 | return ret; |
50 | } | 71 | } |
51 | 72 | ||
52 | QDateTime utcTime( time_t t, const QString& zone ) | 73 | static QDateTime utcTime( time_t t, const QString& zone ) |
53 | { | 74 | { |
54 | QCString org = ::getenv( "TZ" ); | ||
55 | #ifndef Q_OS_MACX // Following line causes bus errors on Mac | ||
56 | |||
57 | ::setenv( "TZ", zone.latin1(), true ); | ||
58 | ::tzset(); | ||
59 | 75 | ||
76 | #ifndef Q_OS_MACX // Following line causes bus errors on Mac | ||
77 | QString old = setTimeZone( zone ); | ||
60 | tm* broken = ::localtime( &t ); | 78 | tm* broken = ::localtime( &t ); |
61 | ::setenv( "TZ", org, true ); | 79 | resetTimeZone( old ); |
62 | #else | 80 | #else |
63 | #warning "Need a replacement for MacOSX!!" | 81 | #warning "Need a replacement for MacOSX!!" |
64 | 82 | ||
65 | tm* broken = ::localtime( &t ); | 83 | tm* broken = ::localtime( &t ); |
66 | #endif | 84 | #endif |
67 | 85 | ||
68 | QDateTime ret; | 86 | QDateTime ret; |
69 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon + 1, broken->tm_mday ) ); | 87 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon + 1, broken->tm_mday ) ); |
70 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); | 88 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); |
71 | 89 | ||
72 | return ret; | 90 | return ret; |
73 | } | 91 | } |
74 | 92 | ||
75 | 93 | ||
76 | time_t to_Time_t( const QDateTime& utc, const QString& str ) | 94 | static time_t to_Time_t( const QDateTime& utc, const QString& str ) |
77 | { | 95 | { |
78 | QDate d = utc.date(); | 96 | QDate d = utc.date(); |
79 | QTime t = utc.time(); | 97 | QTime t = utc.time(); |
80 | 98 | ||
81 | tm broken; | 99 | tm broken; |
82 | broken.tm_year = d.year() - 1900; | 100 | broken.tm_year = d.year() - 1900; |
83 | broken.tm_mon = d.month() - 1; | 101 | broken.tm_mon = d.month() - 1; |
84 | broken.tm_mday = d.day(); | 102 | broken.tm_mday = d.day(); |
85 | broken.tm_hour = t.hour(); | 103 | broken.tm_hour = t.hour(); |
86 | broken.tm_min = t.minute(); | 104 | broken.tm_min = t.minute(); |
87 | broken.tm_sec = t.second(); | 105 | broken.tm_sec = t.second(); |
88 | 106 | ||
89 | QCString org = ::getenv( "TZ" ); | ||
90 | #ifndef Q_OS_MACX // Following line causes bus errors on Mac | 107 | #ifndef Q_OS_MACX // Following line causes bus errors on Mac |
91 | 108 | QString old = setTimeZone( str ); | |
92 | ::setenv( "TZ", str.latin1(), true ); | ||
93 | ::tzset(); | ||
94 | |||
95 | time_t ti = ::mktime( &broken ); | 109 | time_t ti = ::mktime( &broken ); |
96 | ::setenv( "TZ", org, true ); | 110 | resetTimeZone( old ); |
97 | #else | 111 | #else |
98 | #warning "Need a replacement for MacOSX!!" | 112 | #warning "Need a replacement for MacOSX!!" |
99 | 113 | ||
100 | time_t ti = ::mktime( &broken ); | 114 | time_t ti = ::mktime( &broken ); |
101 | #endif | 115 | #endif |
102 | 116 | ||
103 | return ti; | 117 | return ti; |
104 | } | 118 | } |
105 | } | 119 | } |
106 | 120 | ||
107 | namespace Opie | 121 | namespace Opie |
108 | { | 122 | { |
109 | OPimTimeZone::OPimTimeZone( const ZoneName& zone ) | 123 | OPimTimeZone::OPimTimeZone( const ZoneName& zone ) |
110 | : m_name( zone ) | 124 | : m_name( zone ) |
111 | {} | 125 | {} |
112 | 126 | ||
113 | 127 | ||
114 | OPimTimeZone::~OPimTimeZone() | 128 | OPimTimeZone::~OPimTimeZone() |
115 | {} | 129 | {} |
116 | 130 | ||
117 | 131 | ||
118 | bool OPimTimeZone::isValid() const | 132 | bool OPimTimeZone::isValid() const |
119 | { | 133 | { |
120 | return !m_name.isEmpty(); | 134 | return !m_name.isEmpty(); |
121 | } | 135 | } |
122 | 136 | ||
123 | /* | 137 | /* |
124 | * we will get the current timezone | 138 | * we will get the current timezone |
125 | * and ask it to convert to the timezone date | 139 | * and ask it to convert to the timezone date |
126 | */ | 140 | */ |
127 | QDateTime OPimTimeZone::toLocalDateTime( const QDateTime& dt ) | 141 | QDateTime OPimTimeZone::toLocalDateTime( const QDateTime& dt ) |
128 | { | 142 | { |
129 | return OPimTimeZone::current().toDateTime( dt, *this ); | 143 | return OPimTimeZone::current().toDateTime( dt, *this ); |
130 | } | 144 | } |
131 | 145 | ||
132 | 146 | ||
133 | QDateTime OPimTimeZone::toUTCDateTime( const QDateTime& dt ) | 147 | QDateTime OPimTimeZone::toUTCDateTime( const QDateTime& dt ) |
134 | { | 148 | { |
135 | return OPimTimeZone::utc().toDateTime( dt, *this ); | 149 | return OPimTimeZone::utc().toDateTime( dt, *this ); |
136 | } | 150 | } |
137 | 151 | ||
138 | 152 | ||
139 | QDateTime OPimTimeZone::fromUTCDateTime( time_t t ) | 153 | QDateTime OPimTimeZone::fromUTCDateTime( time_t t ) |
140 | { | 154 | { |
141 | return utcTime( t ); | 155 | return utcTime( t ); |
142 | } | 156 | } |
143 | 157 | ||
144 | 158 | ||
145 | QDateTime OPimTimeZone::toDateTime( time_t t ) | 159 | QDateTime OPimTimeZone::toDateTime( time_t t ) |
146 | { | 160 | { |
147 | return utcTime( t, m_name ); | 161 | return utcTime( t, m_name ); |
148 | } | 162 | } |
149 | 163 | ||
150 | 164 | ||
151 | /* | 165 | /* |
152 | * convert dt to utc using zone.m_name | 166 | * convert dt to utc using zone.m_name |
153 | * convert utc -> timeZoneDT using this->m_name | 167 | * convert utc -> timeZoneDT using this->m_name |
154 | */ | 168 | */ |
155 | QDateTime OPimTimeZone::toDateTime( const QDateTime& dt, const OPimTimeZone& zone ) | 169 | QDateTime OPimTimeZone::toDateTime( const QDateTime& dt, const OPimTimeZone& zone ) |
156 | { | 170 | { |
157 | time_t utc = to_Time_t( dt, m_name ); | 171 | time_t utc = to_Time_t( dt, m_name ); |
158 | return utcTime( utc, zone.m_name ); | 172 | return utcTime( utc, zone.m_name ); |
159 | } | 173 | } |
160 | 174 | ||
161 | 175 | ||
162 | time_t OPimTimeZone::fromDateTime( const QDateTime& time ) | 176 | time_t OPimTimeZone::fromDateTime( const QDateTime& time ) |
163 | { | 177 | { |
164 | return to_Time_t( time, m_name ); | 178 | return to_Time_t( time, m_name ); |
165 | } | 179 | } |
166 | 180 | ||
167 | 181 | ||
168 | time_t OPimTimeZone::fromUTCDateTime( const QDateTime& time ) | 182 | time_t OPimTimeZone::fromUTCDateTime( const QDateTime& time ) |
169 | { | 183 | { |
170 | return to_Time_t( time, "Europe/London" ); | 184 | return to_Time_t( time, "Europe/London" ); |
171 | } | 185 | } |
172 | 186 | ||
173 | 187 | ||
174 | OPimTimeZone OPimTimeZone::current() | 188 | OPimTimeZone OPimTimeZone::current() |
175 | { | 189 | { |
176 | QCString str = ::getenv( "TZ" ); | 190 | QCString str = ::getenv( "TZ" ); |
177 | OPimTimeZone zone( str ); | 191 | OPimTimeZone zone( str ); |
178 | return zone; | 192 | return zone; |
179 | } | 193 | } |
180 | 194 | ||
181 | 195 | ||
182 | OPimTimeZone OPimTimeZone::utc() | 196 | OPimTimeZone OPimTimeZone::utc() |
183 | { | 197 | { |
184 | return OPimTimeZone( "Europe/London" ); | 198 | return OPimTimeZone( "Europe/London" ); |
185 | } | 199 | } |
186 | 200 | ||
187 | 201 | ||
188 | QString OPimTimeZone::timeZone() const | 202 | QString OPimTimeZone::timeZone() const |
189 | { | 203 | { |
190 | return m_name; | 204 | return m_name; |
191 | } | 205 | } |
192 | 206 | ||
193 | } | 207 | } |