-rw-r--r-- | libopie/pim/otimezone.cpp | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/libopie/pim/otimezone.cpp b/libopie/pim/otimezone.cpp index b2bd3aa..34659c3 100644 --- a/libopie/pim/otimezone.cpp +++ b/libopie/pim/otimezone.cpp | |||
@@ -1,97 +1,106 @@ | |||
1 | #include <stdio.h> | 1 | #include <stdio.h> |
2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
3 | 3 | ||
4 | #include <sys/types.h> | 4 | #include <sys/types.h> |
5 | 5 | ||
6 | #include "otimezone.h" | 6 | #include "otimezone.h" |
7 | 7 | ||
8 | namespace { | 8 | namespace { |
9 | 9 | ||
10 | QDateTime utcTime( time_t t) { | 10 | QDateTime utcTime( time_t t) { |
11 | tm* broken = ::gmtime( &t ); | 11 | tm* broken = ::gmtime( &t ); |
12 | QDateTime ret; | 12 | QDateTime ret; |
13 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon +1, broken->tm_mday ) ); | 13 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon +1, broken->tm_mday ) ); |
14 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); | 14 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); |
15 | return ret; | 15 | return ret; |
16 | } | 16 | } |
17 | QDateTime utcTime( time_t t, const QString& zone) { | 17 | QDateTime utcTime( time_t t, const QString& zone) { |
18 | QCString org = ::getenv( "TZ" ); | 18 | QCString org = ::getenv( "TZ" ); |
19 | #ifndef Q_OS_MACX // Following line causes bus errors on Mac | ||
19 | ::setenv( "TZ", zone.latin1(), true ); | 20 | ::setenv( "TZ", zone.latin1(), true ); |
20 | ::tzset(); | 21 | ::tzset(); |
21 | 22 | ||
22 | tm* broken = ::localtime( &t ); | 23 | tm* broken = ::localtime( &t ); |
23 | ::setenv( "TZ", org, true ); | 24 | ::setenv( "TZ", org, true ); |
25 | #else | ||
26 | #warning "Need a replacement for MacOSX!!" | ||
27 | tm* broken = ::localtime( &t ); | ||
28 | #endif | ||
24 | 29 | ||
25 | QDateTime ret; | 30 | QDateTime ret; |
26 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon +1, broken->tm_mday ) ); | 31 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon +1, broken->tm_mday ) ); |
27 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); | 32 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); |
28 | 33 | ||
29 | return ret; | 34 | return ret; |
30 | } | 35 | } |
31 | time_t to_Time_t( const QDateTime& utc, const QString& str ) { | 36 | time_t to_Time_t( const QDateTime& utc, const QString& str ) { |
32 | QDate d = utc.date(); | 37 | QDate d = utc.date(); |
33 | QTime t = utc.time(); | 38 | QTime t = utc.time(); |
34 | 39 | ||
35 | tm broken; | 40 | tm broken; |
36 | broken.tm_year = d.year() - 1900; | 41 | broken.tm_year = d.year() - 1900; |
37 | broken.tm_mon = d.month() - 1; | 42 | broken.tm_mon = d.month() - 1; |
38 | broken.tm_mday = d.day(); | 43 | broken.tm_mday = d.day(); |
39 | broken.tm_hour = t.hour(); | 44 | broken.tm_hour = t.hour(); |
40 | broken.tm_min = t.minute(); | 45 | broken.tm_min = t.minute(); |
41 | broken.tm_sec = t.second(); | 46 | broken.tm_sec = t.second(); |
42 | 47 | ||
43 | QCString org = ::getenv( "TZ" ); | 48 | QCString org = ::getenv( "TZ" ); |
49 | #ifndef Q_OS_MACX // Following line causes bus errors on Mac | ||
44 | ::setenv( "TZ", str.latin1(), true ); | 50 | ::setenv( "TZ", str.latin1(), true ); |
45 | ::tzset(); | 51 | ::tzset(); |
46 | 52 | ||
47 | time_t ti = ::mktime( &broken ); | 53 | time_t ti = ::mktime( &broken ); |
48 | ::setenv( "TZ", org, true ); | 54 | ::setenv( "TZ", org, true ); |
49 | 55 | #else | |
56 | #warning "Need a replacement for MacOSX!!" | ||
57 | time_t ti = ::mktime( &broken ); | ||
58 | #endif | ||
50 | return ti; | 59 | return ti; |
51 | } | 60 | } |
52 | } | 61 | } |
53 | OTimeZone::OTimeZone( const ZoneName& zone ) | 62 | OTimeZone::OTimeZone( const ZoneName& zone ) |
54 | : m_name(zone) { | 63 | : m_name(zone) { |
55 | } | 64 | } |
56 | OTimeZone::~OTimeZone() { | 65 | OTimeZone::~OTimeZone() { |
57 | } | 66 | } |
58 | 67 | ||
59 | bool OTimeZone::isValid()const { | 68 | bool OTimeZone::isValid()const { |
60 | return !m_name.isEmpty(); | 69 | return !m_name.isEmpty(); |
61 | } | 70 | } |
62 | 71 | ||
63 | /* | 72 | /* |
64 | * we will get the current timezone | 73 | * we will get the current timezone |
65 | * and ask it to convert to the timezone date | 74 | * and ask it to convert to the timezone date |
66 | */ | 75 | */ |
67 | QDateTime OTimeZone::toLocalDateTime( const QDateTime& dt) { | 76 | QDateTime OTimeZone::toLocalDateTime( const QDateTime& dt) { |
68 | return OTimeZone::current().toDateTime( dt, *this ); | 77 | return OTimeZone::current().toDateTime( dt, *this ); |
69 | } | 78 | } |
70 | QDateTime OTimeZone::toUTCDateTime( const QDateTime& dt ) { | 79 | QDateTime OTimeZone::toUTCDateTime( const QDateTime& dt ) { |
71 | return OTimeZone::utc().toDateTime( dt, *this ); | 80 | return OTimeZone::utc().toDateTime( dt, *this ); |
72 | } | 81 | } |
73 | QDateTime OTimeZone::fromUTCDateTime( time_t t) { | 82 | QDateTime OTimeZone::fromUTCDateTime( time_t t) { |
74 | return utcTime( t ); | 83 | return utcTime( t ); |
75 | } | 84 | } |
76 | QDateTime OTimeZone::toDateTime( time_t t) { | 85 | QDateTime OTimeZone::toDateTime( time_t t) { |
77 | return utcTime( t, m_name ); | 86 | return utcTime( t, m_name ); |
78 | } | 87 | } |
79 | /* | 88 | /* |
80 | * convert dt to utc using zone.m_name | 89 | * convert dt to utc using zone.m_name |
81 | * convert utc -> timeZoneDT using this->m_name | 90 | * convert utc -> timeZoneDT using this->m_name |
82 | */ | 91 | */ |
83 | QDateTime OTimeZone::toDateTime( const QDateTime& dt, const OTimeZone& zone ) { | 92 | QDateTime OTimeZone::toDateTime( const QDateTime& dt, const OTimeZone& zone ) { |
84 | time_t utc = to_Time_t( dt, zone.m_name ); | 93 | time_t utc = to_Time_t( dt, zone.m_name ); |
85 | qWarning("%d %s", utc, zone.m_name.latin1() ); | 94 | qWarning("%d %s", utc, zone.m_name.latin1() ); |
86 | return utcTime( utc, m_name ); | 95 | return utcTime( utc, m_name ); |
87 | } | 96 | } |
88 | time_t OTimeZone::fromDateTime( const QDateTime& time ) { | 97 | time_t OTimeZone::fromDateTime( const QDateTime& time ) { |
89 | return to_Time_t( time, m_name ); | 98 | return to_Time_t( time, m_name ); |
90 | } | 99 | } |
91 | time_t OTimeZone::fromUTCDateTime( const QDateTime& time ) { | 100 | time_t OTimeZone::fromUTCDateTime( const QDateTime& time ) { |
92 | return to_Time_t( time, "UTC" ); | 101 | return to_Time_t( time, "UTC" ); |
93 | } | 102 | } |
94 | OTimeZone OTimeZone::current() { | 103 | OTimeZone OTimeZone::current() { |
95 | QCString str = ::getenv("TZ"); | 104 | QCString str = ::getenv("TZ"); |
96 | OTimeZone zone( str ); | 105 | OTimeZone zone( str ); |
97 | return zone; | 106 | return zone; |