otimezone.cpp
Go to the documentation of this file.00001 #include <stdio.h>
00002 #include <stdlib.h>
00003
00004 #include <sys/types.h>
00005
00006 #include "otimezone.h"
00007
00008 namespace {
00009
00010 QDateTime utcTime( time_t t) {
00011 tm* broken = ::gmtime( &t );
00012 QDateTime ret;
00013 ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon +1, broken->tm_mday ) );
00014 ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) );
00015 return ret;
00016 }
00017 QDateTime utcTime( time_t t, const QString& zone) {
00018 QCString org = ::getenv( "TZ" );
00019 #ifndef Q_OS_MACX // Following line causes bus errors on Mac
00020 ::setenv( "TZ", zone.latin1(), true );
00021 ::tzset();
00022
00023 tm* broken = ::localtime( &t );
00024 ::setenv( "TZ", org, true );
00025 #else
00026 #warning "Need a replacement for MacOSX!!"
00027 tm* broken = ::localtime( &t );
00028 #endif
00029
00030 QDateTime ret;
00031 ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon +1, broken->tm_mday ) );
00032 ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) );
00033
00034 return ret;
00035 }
00036 time_t to_Time_t( const QDateTime& utc, const QString& str ) {
00037 QDate d = utc.date();
00038 QTime t = utc.time();
00039
00040 tm broken;
00041 broken.tm_year = d.year() - 1900;
00042 broken.tm_mon = d.month() - 1;
00043 broken.tm_mday = d.day();
00044 broken.tm_hour = t.hour();
00045 broken.tm_min = t.minute();
00046 broken.tm_sec = t.second();
00047
00048 QCString org = ::getenv( "TZ" );
00049 #ifndef Q_OS_MACX // Following line causes bus errors on Mac
00050 ::setenv( "TZ", str.latin1(), true );
00051 ::tzset();
00052
00053 time_t ti = ::mktime( &broken );
00054 ::setenv( "TZ", org, true );
00055 #else
00056 #warning "Need a replacement for MacOSX!!"
00057 time_t ti = ::mktime( &broken );
00058 #endif
00059 return ti;
00060 }
00061 }
00062 OTimeZone::OTimeZone( const ZoneName& zone )
00063 : m_name(zone) {
00064 }
00065 OTimeZone::~OTimeZone() {
00066 }
00067
00068 bool OTimeZone::isValid()const {
00069 return !m_name.isEmpty();
00070 }
00071
00072
00073
00074
00075
00076 QDateTime OTimeZone::toLocalDateTime( const QDateTime& dt) {
00077 return OTimeZone::current().toDateTime( dt, *this );
00078 }
00079 QDateTime OTimeZone::toUTCDateTime( const QDateTime& dt ) {
00080 return OTimeZone::utc().toDateTime( dt, *this );
00081 }
00082 QDateTime OTimeZone::fromUTCDateTime( time_t t) {
00083 return utcTime( t );
00084 }
00085 QDateTime OTimeZone::toDateTime( time_t t) {
00086 return utcTime( t, m_name );
00087 }
00088
00089
00090
00091
00092 QDateTime OTimeZone::toDateTime( const QDateTime& dt, const OTimeZone& zone ) {
00093 time_t utc = to_Time_t( dt, zone.m_name );
00094 qWarning("%d %s", utc, zone.m_name.latin1() );
00095 return utcTime( utc, m_name );
00096 }
00097 time_t OTimeZone::fromDateTime( const QDateTime& time ) {
00098 return to_Time_t( time, m_name );
00099 }
00100 time_t OTimeZone::fromUTCDateTime( const QDateTime& time ) {
00101 return to_Time_t( time, "UTC" );
00102 }
00103 OTimeZone OTimeZone::current() {
00104 QCString str = ::getenv("TZ");
00105 OTimeZone zone( str );
00106 return zone;
00107 }
00108 OTimeZone OTimeZone::utc() {
00109 return OTimeZone("UTC");
00110 }
00111 QString OTimeZone::timeZone()const {
00112 return m_name;
00113 }
This file is part of the documentation for OPIE Version 1.1.