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