summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/core/opimtimezone.cpp
Side-by-side diff
Diffstat (limited to 'libopie2/opiepim/core/opimtimezone.cpp') (more/less context) (show whitespace changes)
-rw-r--r--libopie2/opiepim/core/opimtimezone.cpp44
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
@@ -42,3 +42,24 @@ namespace Opie
-QDateTime utcTime( time_t t )
+/*
+ * Save the old timeZone in a secure way (NULL Pointer check),
+ * set the new timeZone from the parameter, call tzset
+ * and then return the old timezone
+ */
+static QString setTimeZone( const QString& zone) {
+ QString old;
+ char *org = ::getenv( "TZ" );
+ if( org )
+ old = QString::fromLocal8Bit( org );
+
+ ::setenv( "TZ", zone.local8Bit(), true );
+ ::tzset();
+
+ return old;
+}
+
+static void resetTimeZone( const QString& zone ) {
+ ::setenv( "TZ", zone.local8Bit(), true );
+}
+
+static QDateTime utcTime( time_t t )
{
@@ -51,12 +72,9 @@ QDateTime utcTime( time_t t )
-QDateTime utcTime( time_t t, const QString& zone )
+static QDateTime utcTime( time_t t, const QString& zone )
{
- QCString org = ::getenv( "TZ" );
-#ifndef Q_OS_MACX // Following line causes bus errors on Mac
-
- ::setenv( "TZ", zone.latin1(), true );
- ::tzset();
+#ifndef Q_OS_MACX // Following line causes bus errors on Mac
+ QString old = setTimeZone( zone );
tm* broken = ::localtime( &t );
- ::setenv( "TZ", org, true );
+ resetTimeZone( old );
#else
@@ -75,3 +93,3 @@ QDateTime utcTime( time_t t, const QString& zone )
-time_t to_Time_t( const QDateTime& utc, const QString& str )
+static time_t to_Time_t( const QDateTime& utc, const QString& str )
{
@@ -88,10 +106,6 @@ time_t to_Time_t( const QDateTime& utc, const QString& str )
- QCString org = ::getenv( "TZ" );
#ifndef Q_OS_MACX // Following line causes bus errors on Mac
-
- ::setenv( "TZ", str.latin1(), true );
- ::tzset();
-
+ QString old = setTimeZone( str );
time_t ti = ::mktime( &broken );
- ::setenv( "TZ", org, true );
+ resetTimeZone( old );
#else