summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/core/otimezone.cpp
Unidiff
Diffstat (limited to 'libopie2/opiepim/core/otimezone.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/core/otimezone.cpp168
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
@@ -27,76 +27,90 @@
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> 37namespace Opie
36 38{
37namespace Opie {
38 39
39 QDateTime utcTime( time_t t) { 40QDateTime 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" ); 48QDateTime 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 ) { 70time_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
92namespace Opie { 97 return ti;
93OTimeZone::OTimeZone( const ZoneName& zone )
94 : m_name(zone) {
95} 98}
96OTimeZone::~OTimeZone() {
97} 99}
98 100
99bool OTimeZone::isValid()const { 101namespace Opie
102{
103OTimeZone::OTimeZone( const ZoneName& zone )
104 : m_name( zone )
105{}
106
107
108OTimeZone::~OTimeZone()
109{}
110
111
112bool 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 */
107QDateTime OTimeZone::toLocalDateTime( const QDateTime& dt) { 121QDateTime OTimeZone::toLocalDateTime( const QDateTime& dt )
122{
108 return OTimeZone::current().toDateTime( dt, *this ); 123 return OTimeZone::current().toDateTime( dt, *this );
109} 124}
110QDateTime OTimeZone::toUTCDateTime( const QDateTime& dt ) { 125
126
127QDateTime OTimeZone::toUTCDateTime( const QDateTime& dt )
128{
111 return OTimeZone::utc().toDateTime( dt, *this ); 129 return OTimeZone::utc().toDateTime( dt, *this );
112} 130}
113QDateTime OTimeZone::fromUTCDateTime( time_t t) { 131
132
133QDateTime OTimeZone::fromUTCDateTime( time_t t )
134{
114 return utcTime( t ); 135 return utcTime( t );
115} 136}
116QDateTime OTimeZone::toDateTime( time_t t) { 137
138
139QDateTime 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 */
123QDateTime OTimeZone::toDateTime( const QDateTime& dt, const OTimeZone& zone ) { 149QDateTime 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}
128time_t OTimeZone::fromDateTime( const QDateTime& time ) { 155
156
157time_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}
131time_t OTimeZone::fromUTCDateTime( const QDateTime& time ) { 161
162
163time_t OTimeZone::fromUTCDateTime( const QDateTime& time )
164{
132 return to_Time_t( time, "UTC" ); 165 return to_Time_t( time, "UTC" );
133} 166}
134OTimeZone OTimeZone::current() { 167
135 QCString str = ::getenv("TZ"); 168
169OTimeZone OTimeZone::current()
170{
171 QCString str = ::getenv( "TZ" );
136 OTimeZone zone( str ); 172 OTimeZone zone( str );
137 return zone; 173 return zone;
138} 174}
139OTimeZone OTimeZone::utc() { 175
140 return OTimeZone("UTC"); 176
177OTimeZone OTimeZone::utc()
178{
179 return OTimeZone( "UTC" );
141} 180}
142QString OTimeZone::timeZone()const { 181
182
183QString OTimeZone::timeZone() const
184{
143 return m_name; 185 return m_name;
144} 186}
145 187