author | mickeyl <mickeyl> | 2004-02-21 12:25:50 (UTC) |
---|---|---|
committer | mickeyl <mickeyl> | 2004-02-21 12:25:50 (UTC) |
commit | 6155f1be5c19fc51a4929c9e173f9e5a24d193e6 (patch) (unidiff) | |
tree | c18ca863f82a559c60728f922519ac696387ec53 /libopie2/opiepim/core/otimezone.cpp | |
parent | a570c2824fcecaf1c278e1b5a592fa14cc0647f8 (diff) | |
download | opie-6155f1be5c19fc51a4929c9e173f9e5a24d193e6.zip opie-6155f1be5c19fc51a4929c9e173f9e5a24d193e6.tar.gz opie-6155f1be5c19fc51a4929c9e173f9e5a24d193e6.tar.bz2 |
the usual cosmetics
Diffstat (limited to 'libopie2/opiepim/core/otimezone.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie2/opiepim/core/otimezone.cpp | 168 |
1 files changed, 105 insertions, 63 deletions
diff --git a/libopie2/opiepim/core/otimezone.cpp b/libopie2/opiepim/core/otimezone.cpp index dab68af..e67715f 100644 --- a/libopie2/opiepim/core/otimezone.cpp +++ b/libopie2/opiepim/core/otimezone.cpp | |||
@@ -24,123 +24,165 @@ | |||
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 "otimezone.h" | ||
31 | |||
32 | /* STD */ | ||
30 | #include <stdio.h> | 33 | #include <stdio.h> |
31 | #include <stdlib.h> | 34 | #include <stdlib.h> |
32 | |||
33 | #include <sys/types.h> | 35 | #include <sys/types.h> |
34 | 36 | ||
35 | #include <opie2/otimezone.h> | 37 | namespace Opie |
36 | 38 | { | |
37 | namespace Opie { | ||
38 | 39 | ||
39 | QDateTime utcTime( time_t t) { | 40 | QDateTime utcTime( time_t t ) |
40 | tm* broken = ::gmtime( &t ); | 41 | { |
41 | QDateTime ret; | 42 | tm * broken = ::gmtime( &t ); |
42 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon +1, broken->tm_mday ) ); | 43 | QDateTime ret; |
43 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); | 44 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon + 1, broken->tm_mday ) ); |
44 | return ret; | 45 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); |
45 | } | 46 | return ret; |
46 | QDateTime utcTime( time_t t, const QString& zone) { | 47 | } |
47 | QCString org = ::getenv( "TZ" ); | 48 | QDateTime utcTime( time_t t, const QString& zone ) |
49 | { | ||
50 | QCString org = ::getenv( "TZ" ); | ||
48 | #ifndef Q_OS_MACX // Following line causes bus errors on Mac | 51 | #ifndef Q_OS_MACX // Following line causes bus errors on Mac |
49 | ::setenv( "TZ", zone.latin1(), true ); | ||
50 | ::tzset(); | ||
51 | 52 | ||
52 | tm* broken = ::localtime( &t ); | 53 | ::setenv( "TZ", zone.latin1(), true ); |
53 | ::setenv( "TZ", org, true ); | 54 | ::tzset(); |
55 | |||
56 | tm* broken = ::localtime( &t ); | ||
57 | ::setenv( "TZ", org, true ); | ||
54 | #else | 58 | #else |
55 | #warning "Need a replacement for MacOSX!!" | 59 | #warning "Need a replacement for MacOSX!!" |
56 | tm* broken = ::localtime( &t ); | 60 | |
61 | tm* broken = ::localtime( &t ); | ||
57 | #endif | 62 | #endif |
58 | 63 | ||
59 | QDateTime ret; | 64 | QDateTime ret; |
60 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon +1, broken->tm_mday ) ); | 65 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon + 1, broken->tm_mday ) ); |
61 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); | 66 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); |
62 | 67 | ||
63 | return ret; | 68 | return ret; |
64 | } | 69 | } |
65 | time_t to_Time_t( const QDateTime& utc, const QString& str ) { | 70 | time_t to_Time_t( const QDateTime& utc, const QString& str ) |
66 | QDate d = utc.date(); | 71 | { |
67 | QTime t = utc.time(); | 72 | QDate d = utc.date(); |
68 | 73 | QTime t = utc.time(); | |
69 | tm broken; | 74 | |
70 | broken.tm_year = d.year() - 1900; | 75 | tm broken; |
71 | broken.tm_mon = d.month() - 1; | 76 | broken.tm_year = d.year() - 1900; |
72 | broken.tm_mday = d.day(); | 77 | broken.tm_mon = d.month() - 1; |
73 | broken.tm_hour = t.hour(); | 78 | broken.tm_mday = d.day(); |
74 | broken.tm_min = t.minute(); | 79 | broken.tm_hour = t.hour(); |
75 | broken.tm_sec = t.second(); | 80 | broken.tm_min = t.minute(); |
76 | 81 | broken.tm_sec = t.second(); | |
77 | QCString org = ::getenv( "TZ" ); | 82 | |
83 | QCString org = ::getenv( "TZ" ); | ||
78 | #ifndef Q_OS_MACX // Following line causes bus errors on Mac | 84 | #ifndef Q_OS_MACX // Following line causes bus errors on Mac |
79 | ::setenv( "TZ", str.latin1(), true ); | ||
80 | ::tzset(); | ||
81 | 85 | ||
82 | time_t ti = ::mktime( &broken ); | 86 | ::setenv( "TZ", str.latin1(), true ); |
83 | ::setenv( "TZ", org, true ); | 87 | ::tzset(); |
88 | |||
89 | time_t ti = ::mktime( &broken ); | ||
90 | ::setenv( "TZ", org, true ); | ||
84 | #else | 91 | #else |
85 | #warning "Need a replacement for MacOSX!!" | 92 | #warning "Need a replacement for MacOSX!!" |
86 | time_t ti = ::mktime( &broken ); | 93 | |
94 | time_t ti = ::mktime( &broken ); | ||
87 | #endif | 95 | #endif |
88 | return ti; | ||
89 | } | ||
90 | } | ||
91 | 96 | ||
92 | namespace Opie { | 97 | return ti; |
93 | OTimeZone::OTimeZone( const ZoneName& zone ) | ||
94 | : m_name(zone) { | ||
95 | } | 98 | } |
96 | OTimeZone::~OTimeZone() { | ||
97 | } | 99 | } |
98 | 100 | ||
99 | bool OTimeZone::isValid()const { | 101 | namespace Opie |
102 | { | ||
103 | OTimeZone::OTimeZone( const ZoneName& zone ) | ||
104 | : m_name( zone ) | ||
105 | {} | ||
106 | |||
107 | |||
108 | OTimeZone::~OTimeZone() | ||
109 | {} | ||
110 | |||
111 | |||
112 | bool OTimeZone::isValid() const | ||
113 | { | ||
100 | return !m_name.isEmpty(); | 114 | return !m_name.isEmpty(); |
101 | } | 115 | } |
102 | 116 | ||
103 | /* | 117 | /* |
104 | * we will get the current timezone | 118 | * we will get the current timezone |
105 | * and ask it to convert to the timezone date | 119 | * and ask it to convert to the timezone date |
106 | */ | 120 | */ |
107 | QDateTime OTimeZone::toLocalDateTime( const QDateTime& dt) { | 121 | QDateTime OTimeZone::toLocalDateTime( const QDateTime& dt ) |
122 | { | ||
108 | return OTimeZone::current().toDateTime( dt, *this ); | 123 | return OTimeZone::current().toDateTime( dt, *this ); |
109 | } | 124 | } |
110 | QDateTime OTimeZone::toUTCDateTime( const QDateTime& dt ) { | 125 | |
126 | |||
127 | QDateTime OTimeZone::toUTCDateTime( const QDateTime& dt ) | ||
128 | { | ||
111 | return OTimeZone::utc().toDateTime( dt, *this ); | 129 | return OTimeZone::utc().toDateTime( dt, *this ); |
112 | } | 130 | } |
113 | QDateTime OTimeZone::fromUTCDateTime( time_t t) { | 131 | |
132 | |||
133 | QDateTime OTimeZone::fromUTCDateTime( time_t t ) | ||
134 | { | ||
114 | return utcTime( t ); | 135 | return utcTime( t ); |
115 | } | 136 | } |
116 | QDateTime OTimeZone::toDateTime( time_t t) { | 137 | |
138 | |||
139 | QDateTime OTimeZone::toDateTime( time_t t ) | ||
140 | { | ||
117 | return utcTime( t, m_name ); | 141 | return utcTime( t, m_name ); |
118 | } | 142 | } |
143 | |||
144 | |||
119 | /* | 145 | /* |
120 | * convert dt to utc using zone.m_name | 146 | * convert dt to utc using zone.m_name |
121 | * convert utc -> timeZoneDT using this->m_name | 147 | * convert utc -> timeZoneDT using this->m_name |
122 | */ | 148 | */ |
123 | QDateTime OTimeZone::toDateTime( const QDateTime& dt, const OTimeZone& zone ) { | 149 | QDateTime OTimeZone::toDateTime( const QDateTime& dt, const OTimeZone& zone ) |
150 | { | ||
124 | time_t utc = to_Time_t( dt, zone.m_name ); | 151 | time_t utc = to_Time_t( dt, zone.m_name ); |
125 | qWarning("%d %s", utc, zone.m_name.latin1() ); | 152 | qWarning( "%d %s", utc, zone.m_name.latin1() ); |
126 | return utcTime( utc, m_name ); | 153 | return utcTime( utc, m_name ); |
127 | } | 154 | } |
128 | time_t OTimeZone::fromDateTime( const QDateTime& time ) { | 155 | |
156 | |||
157 | time_t OTimeZone::fromDateTime( const QDateTime& time ) | ||
158 | { | ||
129 | return to_Time_t( time, m_name ); | 159 | return to_Time_t( time, m_name ); |
130 | } | 160 | } |
131 | time_t OTimeZone::fromUTCDateTime( const QDateTime& time ) { | 161 | |
162 | |||
163 | time_t OTimeZone::fromUTCDateTime( const QDateTime& time ) | ||
164 | { | ||
132 | return to_Time_t( time, "UTC" ); | 165 | return to_Time_t( time, "UTC" ); |
133 | } | 166 | } |
134 | OTimeZone OTimeZone::current() { | 167 | |
135 | QCString str = ::getenv("TZ"); | 168 | |
169 | OTimeZone OTimeZone::current() | ||
170 | { | ||
171 | QCString str = ::getenv( "TZ" ); | ||
136 | OTimeZone zone( str ); | 172 | OTimeZone zone( str ); |
137 | return zone; | 173 | return zone; |
138 | } | 174 | } |
139 | OTimeZone OTimeZone::utc() { | 175 | |
140 | return OTimeZone("UTC"); | 176 | |
177 | OTimeZone OTimeZone::utc() | ||
178 | { | ||
179 | return OTimeZone( "UTC" ); | ||
141 | } | 180 | } |
142 | QString OTimeZone::timeZone()const { | 181 | |
182 | |||
183 | QString OTimeZone::timeZone() const | ||
184 | { | ||
143 | return m_name; | 185 | return m_name; |
144 | } | 186 | } |
145 | 187 | ||
146 | } | 188 | } |