summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/core/opimtimezone.cpp
Unidiff
Diffstat (limited to 'libopie2/opiepim/core/opimtimezone.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/core/opimtimezone.cpp188
1 files changed, 188 insertions, 0 deletions
diff --git a/libopie2/opiepim/core/opimtimezone.cpp b/libopie2/opiepim/core/opimtimezone.cpp
new file mode 100644
index 0000000..be21b1b
--- a/dev/null
+++ b/libopie2/opiepim/core/opimtimezone.cpp
@@ -0,0 +1,188 @@
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
30#include "opimtimezone.h"
31
32/* STD */
33#include <stdio.h>
34#include <stdlib.h>
35#include <sys/types.h>
36
37namespace Opie
38{
39
40QDateTime utcTime( time_t t )
41{
42 tm * broken = ::gmtime( &t );
43 QDateTime ret;
44 ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon + 1, broken->tm_mday ) );
45 ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) );
46 return ret;
47}
48QDateTime utcTime( time_t t, const QString& zone )
49{
50 QCString org = ::getenv( "TZ" );
51#ifndef Q_OS_MACX // Following line causes bus errors on Mac
52
53 ::setenv( "TZ", zone.latin1(), true );
54 ::tzset();
55
56 tm* broken = ::localtime( &t );
57 ::setenv( "TZ", org, true );
58#else
59#warning "Need a replacement for MacOSX!!"
60
61 tm* broken = ::localtime( &t );
62#endif
63
64 QDateTime ret;
65 ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon + 1, broken->tm_mday ) );
66 ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) );
67
68 return ret;
69}
70time_t to_Time_t( const QDateTime& utc, const QString& str )
71{
72 QDate d = utc.date();
73 QTime t = utc.time();
74
75 tm broken;
76 broken.tm_year = d.year() - 1900;
77 broken.tm_mon = d.month() - 1;
78 broken.tm_mday = d.day();
79 broken.tm_hour = t.hour();
80 broken.tm_min = t.minute();
81 broken.tm_sec = t.second();
82
83 QCString org = ::getenv( "TZ" );
84#ifndef Q_OS_MACX // Following line causes bus errors on Mac
85
86 ::setenv( "TZ", str.latin1(), true );
87 ::tzset();
88
89 time_t ti = ::mktime( &broken );
90 ::setenv( "TZ", org, true );
91#else
92#warning "Need a replacement for MacOSX!!"
93
94 time_t ti = ::mktime( &broken );
95#endif
96
97 return ti;
98}
99}
100
101namespace Opie
102{
103OPimTimeZone::OPimTimeZone( const ZoneName& zone )
104 : m_name( zone )
105{}
106
107
108OPimTimeZone::~OPimTimeZone()
109{}
110
111
112bool OPimTimeZone::isValid() const
113{
114 return !m_name.isEmpty();
115}
116
117/*
118 * we will get the current timezone
119 * and ask it to convert to the timezone date
120 */
121QDateTime OPimTimeZone::toLocalDateTime( const QDateTime& dt )
122{
123 return OPimTimeZone::current().toDateTime( dt, *this );
124}
125
126
127QDateTime OPimTimeZone::toUTCDateTime( const QDateTime& dt )
128{
129 return OPimTimeZone::utc().toDateTime( dt, *this );
130}
131
132
133QDateTime OPimTimeZone::fromUTCDateTime( time_t t )
134{
135 return utcTime( t );
136}
137
138
139QDateTime OPimTimeZone::toDateTime( time_t t )
140{
141 return utcTime( t, m_name );
142}
143
144
145/*
146 * convert dt to utc using zone.m_name
147 * convert utc -> timeZoneDT using this->m_name
148 */
149QDateTime OPimTimeZone::toDateTime( const QDateTime& dt, const OPimTimeZone& zone )
150{
151 time_t utc = to_Time_t( dt, zone.m_name );
152 qWarning( "%d %s", utc, zone.m_name.latin1() );
153 return utcTime( utc, m_name );
154}
155
156
157time_t OPimTimeZone::fromDateTime( const QDateTime& time )
158{
159 return to_Time_t( time, m_name );
160}
161
162
163time_t OPimTimeZone::fromUTCDateTime( const QDateTime& time )
164{
165 return to_Time_t( time, "UTC" );
166}
167
168
169OPimTimeZone OPimTimeZone::current()
170{
171 QCString str = ::getenv( "TZ" );
172 OPimTimeZone zone( str );
173 return zone;
174}
175
176
177OPimTimeZone OPimTimeZone::utc()
178{
179 return OPimTimeZone( "UTC" );
180}
181
182
183QString OPimTimeZone::timeZone() const
184{
185 return m_name;
186}
187
188}