Diffstat (limited to 'libopie2/opiepim/core/otimezone.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | libopie2/opiepim/core/otimezone.cpp | 88 |
1 files changed, 65 insertions, 23 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 | |||
@@ -27,25 +27,29 @@ | |||
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 |
38 | { | ||
36 | 39 | ||
37 | namespace Opie { | 40 | QDateTime utcTime( time_t t ) |
38 | 41 | { | |
39 | QDateTime utcTime( time_t t) { | ||
40 | tm* broken = ::gmtime( &t ); | 42 | tm* broken = ::gmtime( &t ); |
41 | QDateTime ret; | 43 | QDateTime ret; |
42 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon +1, broken->tm_mday ) ); | 44 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon +1, broken->tm_mday ) ); |
43 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); | 45 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); |
44 | return ret; | 46 | return ret; |
45 | } | 47 | } |
46 | QDateTime utcTime( time_t t, const QString& zone) { | 48 | QDateTime utcTime( time_t t, const QString& zone ) |
49 | { | ||
47 | QCString org = ::getenv( "TZ" ); | 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 |
52 | |||
49 | ::setenv( "TZ", zone.latin1(), true ); | 53 | ::setenv( "TZ", zone.latin1(), true ); |
50 | ::tzset(); | 54 | ::tzset(); |
51 | 55 | ||
@@ -53,6 +57,7 @@ namespace Opie { | |||
53 | ::setenv( "TZ", org, true ); | 57 | ::setenv( "TZ", org, true ); |
54 | #else | 58 | #else |
55 | #warning "Need a replacement for MacOSX!!" | 59 | #warning "Need a replacement for MacOSX!!" |
60 | |||
56 | tm* broken = ::localtime( &t ); | 61 | tm* broken = ::localtime( &t ); |
57 | #endif | 62 | #endif |
58 | 63 | ||
@@ -62,7 +67,8 @@ namespace Opie { | |||
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 ) |
71 | { | ||
66 | QDate d = utc.date(); | 72 | QDate d = utc.date(); |
67 | QTime t = utc.time(); | 73 | QTime t = utc.time(); |
68 | 74 | ||
@@ -76,6 +82,7 @@ namespace Opie { | |||
76 | 82 | ||
77 | QCString org = ::getenv( "TZ" ); | 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 |
85 | |||
79 | ::setenv( "TZ", str.latin1(), true ); | 86 | ::setenv( "TZ", str.latin1(), true ); |
80 | ::tzset(); | 87 | ::tzset(); |
81 | 88 | ||
@@ -83,20 +90,27 @@ namespace Opie { | |||
83 | ::setenv( "TZ", org, true ); | 90 | ::setenv( "TZ", org, true ); |
84 | #else | 91 | #else |
85 | #warning "Need a replacement for MacOSX!!" | 92 | #warning "Need a replacement for MacOSX!!" |
93 | |||
86 | time_t ti = ::mktime( &broken ); | 94 | time_t ti = ::mktime( &broken ); |
87 | #endif | 95 | #endif |
96 | |||
88 | return ti; | 97 | return ti; |
89 | } | 98 | } |
90 | } | 99 | } |
91 | 100 | ||
92 | namespace Opie { | 101 | namespace Opie |
102 | { | ||
93 | OTimeZone::OTimeZone( const ZoneName& zone ) | 103 | OTimeZone::OTimeZone( const ZoneName& zone ) |
94 | : m_name(zone) { | 104 | : m_name( zone ) |
95 | } | 105 | {} |
96 | OTimeZone::~OTimeZone() { | 106 | |
97 | } | 107 | |
108 | OTimeZone::~OTimeZone() | ||
109 | {} | ||
98 | 110 | ||
99 | bool OTimeZone::isValid()const { | 111 | |
112 | bool OTimeZone::isValid() const | ||
113 | { | ||
100 | return !m_name.isEmpty(); | 114 | return !m_name.isEmpty(); |
101 | } | 115 | } |
102 | 116 | ||
@@ -104,42 +118,70 @@ bool OTimeZone::isValid()const { | |||
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 | |
168 | |||
169 | OTimeZone OTimeZone::current() | ||
170 | { | ||
135 | QCString str = ::getenv("TZ"); | 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 | |
176 | |||
177 | OTimeZone OTimeZone::utc() | ||
178 | { | ||
140 | return OTimeZone("UTC"); | 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 | ||