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 | |||
@@ -31,78 +31,92 @@ | |||
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 | { |