Diffstat (limited to 'libopie2/opiepim/core/otimezone.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | libopie2/opiepim/core/otimezone.cpp | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/libopie2/opiepim/core/otimezone.cpp b/libopie2/opiepim/core/otimezone.cpp index 34659c3..dab68af 100644 --- a/libopie2/opiepim/core/otimezone.cpp +++ b/libopie2/opiepim/core/otimezone.cpp | |||
@@ -1,32 +1,61 @@ | |||
1 | /* | ||
2 | This file is part of the Opie Project | ||
3 | Copyright (C) The Main Author <main-author@whereever.org> | ||
4 | =. Copyright (C) The Opie Team <opie-devel@handhelds.org> | ||
5 | .=l. | ||
6 | .>+-= | ||
7 | _;:, .> :=|. This program is free software; you can | ||
8 | .> <`_, > . <= redistribute it and/or modify it under | ||
9 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | ||
10 | .="- .-=="i, .._ License as published by the Free Software | ||
11 | - . .-<_> .<> Foundation; either version 2 of the License, | ||
12 | ._= =} : or (at your option) any later version. | ||
13 | .%`+i> _;_. | ||
14 | .i_,=:_. -<s. This program is distributed in the hope that | ||
15 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | ||
16 | : .. .:, . . . without even the implied warranty of | ||
17 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | ||
18 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | ||
19 | ..}^=.= = ; Library General Public License for more | ||
20 | ++= -. .` .: details. | ||
21 | : = ...= . :.=- | ||
22 | -. .:....=;==+<; You should have received a copy of the GNU | ||
23 | -_. . . )=. = Library General Public License along with | ||
24 | -- :-=` this library; see the file COPYING.LIB. | ||
25 | If not, write to the Free Software Foundation, | ||
26 | Inc., 59 Temple Place - Suite 330, | ||
27 | Boston, MA 02111-1307, USA. | ||
28 | */ | ||
29 | |||
1 | #include <stdio.h> | 30 | #include <stdio.h> |
2 | #include <stdlib.h> | 31 | #include <stdlib.h> |
3 | 32 | ||
4 | #include <sys/types.h> | 33 | #include <sys/types.h> |
5 | 34 | ||
6 | #include "otimezone.h" | 35 | #include <opie2/otimezone.h> |
7 | 36 | ||
8 | namespace { | 37 | namespace Opie { |
9 | 38 | ||
10 | QDateTime utcTime( time_t t) { | 39 | QDateTime utcTime( time_t t) { |
11 | tm* broken = ::gmtime( &t ); | 40 | tm* broken = ::gmtime( &t ); |
12 | QDateTime ret; | 41 | QDateTime ret; |
13 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon +1, broken->tm_mday ) ); | 42 | 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 ) ); | 43 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); |
15 | return ret; | 44 | return ret; |
16 | } | 45 | } |
17 | QDateTime utcTime( time_t t, const QString& zone) { | 46 | QDateTime utcTime( time_t t, const QString& zone) { |
18 | QCString org = ::getenv( "TZ" ); | 47 | QCString org = ::getenv( "TZ" ); |
19 | #ifndef Q_OS_MACX // Following line causes bus errors on Mac | 48 | #ifndef Q_OS_MACX // Following line causes bus errors on Mac |
20 | ::setenv( "TZ", zone.latin1(), true ); | 49 | ::setenv( "TZ", zone.latin1(), true ); |
21 | ::tzset(); | 50 | ::tzset(); |
22 | 51 | ||
23 | tm* broken = ::localtime( &t ); | 52 | tm* broken = ::localtime( &t ); |
24 | ::setenv( "TZ", org, true ); | 53 | ::setenv( "TZ", org, true ); |
25 | #else | 54 | #else |
26 | #warning "Need a replacement for MacOSX!!" | 55 | #warning "Need a replacement for MacOSX!!" |
27 | tm* broken = ::localtime( &t ); | 56 | tm* broken = ::localtime( &t ); |
28 | #endif | 57 | #endif |
29 | 58 | ||
30 | QDateTime ret; | 59 | QDateTime ret; |
31 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon +1, broken->tm_mday ) ); | 60 | 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 ) ); | 61 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); |
@@ -38,48 +67,50 @@ namespace { | |||
38 | QTime t = utc.time(); | 67 | QTime t = utc.time(); |
39 | 68 | ||
40 | tm broken; | 69 | tm broken; |
41 | broken.tm_year = d.year() - 1900; | 70 | broken.tm_year = d.year() - 1900; |
42 | broken.tm_mon = d.month() - 1; | 71 | broken.tm_mon = d.month() - 1; |
43 | broken.tm_mday = d.day(); | 72 | broken.tm_mday = d.day(); |
44 | broken.tm_hour = t.hour(); | 73 | broken.tm_hour = t.hour(); |
45 | broken.tm_min = t.minute(); | 74 | broken.tm_min = t.minute(); |
46 | broken.tm_sec = t.second(); | 75 | broken.tm_sec = t.second(); |
47 | 76 | ||
48 | QCString org = ::getenv( "TZ" ); | 77 | QCString org = ::getenv( "TZ" ); |
49 | #ifndef Q_OS_MACX // Following line causes bus errors on Mac | 78 | #ifndef Q_OS_MACX // Following line causes bus errors on Mac |
50 | ::setenv( "TZ", str.latin1(), true ); | 79 | ::setenv( "TZ", str.latin1(), true ); |
51 | ::tzset(); | 80 | ::tzset(); |
52 | 81 | ||
53 | time_t ti = ::mktime( &broken ); | 82 | time_t ti = ::mktime( &broken ); |
54 | ::setenv( "TZ", org, true ); | 83 | ::setenv( "TZ", org, true ); |
55 | #else | 84 | #else |
56 | #warning "Need a replacement for MacOSX!!" | 85 | #warning "Need a replacement for MacOSX!!" |
57 | time_t ti = ::mktime( &broken ); | 86 | time_t ti = ::mktime( &broken ); |
58 | #endif | 87 | #endif |
59 | return ti; | 88 | return ti; |
60 | } | 89 | } |
61 | } | 90 | } |
91 | |||
92 | namespace Opie { | ||
62 | OTimeZone::OTimeZone( const ZoneName& zone ) | 93 | OTimeZone::OTimeZone( const ZoneName& zone ) |
63 | : m_name(zone) { | 94 | : m_name(zone) { |
64 | } | 95 | } |
65 | OTimeZone::~OTimeZone() { | 96 | OTimeZone::~OTimeZone() { |
66 | } | 97 | } |
67 | 98 | ||
68 | bool OTimeZone::isValid()const { | 99 | bool OTimeZone::isValid()const { |
69 | return !m_name.isEmpty(); | 100 | return !m_name.isEmpty(); |
70 | } | 101 | } |
71 | 102 | ||
72 | /* | 103 | /* |
73 | * we will get the current timezone | 104 | * we will get the current timezone |
74 | * and ask it to convert to the timezone date | 105 | * and ask it to convert to the timezone date |
75 | */ | 106 | */ |
76 | QDateTime OTimeZone::toLocalDateTime( const QDateTime& dt) { | 107 | QDateTime OTimeZone::toLocalDateTime( const QDateTime& dt) { |
77 | return OTimeZone::current().toDateTime( dt, *this ); | 108 | return OTimeZone::current().toDateTime( dt, *this ); |
78 | } | 109 | } |
79 | QDateTime OTimeZone::toUTCDateTime( const QDateTime& dt ) { | 110 | QDateTime OTimeZone::toUTCDateTime( const QDateTime& dt ) { |
80 | return OTimeZone::utc().toDateTime( dt, *this ); | 111 | return OTimeZone::utc().toDateTime( dt, *this ); |
81 | } | 112 | } |
82 | QDateTime OTimeZone::fromUTCDateTime( time_t t) { | 113 | QDateTime OTimeZone::fromUTCDateTime( time_t t) { |
83 | return utcTime( t ); | 114 | return utcTime( t ); |
84 | } | 115 | } |
85 | QDateTime OTimeZone::toDateTime( time_t t) { | 116 | QDateTime OTimeZone::toDateTime( time_t t) { |
@@ -90,24 +121,26 @@ QDateTime OTimeZone::toDateTime( time_t t) { | |||
90 | * convert utc -> timeZoneDT using this->m_name | 121 | * convert utc -> timeZoneDT using this->m_name |
91 | */ | 122 | */ |
92 | QDateTime OTimeZone::toDateTime( const QDateTime& dt, const OTimeZone& zone ) { | 123 | QDateTime OTimeZone::toDateTime( const QDateTime& dt, const OTimeZone& zone ) { |
93 | time_t utc = to_Time_t( dt, zone.m_name ); | 124 | time_t utc = to_Time_t( dt, zone.m_name ); |
94 | qWarning("%d %s", utc, zone.m_name.latin1() ); | 125 | qWarning("%d %s", utc, zone.m_name.latin1() ); |
95 | return utcTime( utc, m_name ); | 126 | return utcTime( utc, m_name ); |
96 | } | 127 | } |
97 | time_t OTimeZone::fromDateTime( const QDateTime& time ) { | 128 | time_t OTimeZone::fromDateTime( const QDateTime& time ) { |
98 | return to_Time_t( time, m_name ); | 129 | return to_Time_t( time, m_name ); |
99 | } | 130 | } |
100 | time_t OTimeZone::fromUTCDateTime( const QDateTime& time ) { | 131 | time_t OTimeZone::fromUTCDateTime( const QDateTime& time ) { |
101 | return to_Time_t( time, "UTC" ); | 132 | return to_Time_t( time, "UTC" ); |
102 | } | 133 | } |
103 | OTimeZone OTimeZone::current() { | 134 | OTimeZone OTimeZone::current() { |
104 | QCString str = ::getenv("TZ"); | 135 | QCString str = ::getenv("TZ"); |
105 | OTimeZone zone( str ); | 136 | OTimeZone zone( str ); |
106 | return zone; | 137 | return zone; |
107 | } | 138 | } |
108 | OTimeZone OTimeZone::utc() { | 139 | OTimeZone OTimeZone::utc() { |
109 | return OTimeZone("UTC"); | 140 | return OTimeZone("UTC"); |
110 | } | 141 | } |
111 | QString OTimeZone::timeZone()const { | 142 | QString OTimeZone::timeZone()const { |
112 | return m_name; | 143 | return m_name; |
113 | } | 144 | } |
145 | |||
146 | } | ||