summaryrefslogtreecommitdiff
path: root/library/timestring.cpp
authorkergoth <kergoth>2002-01-25 22:14:26 (UTC)
committer kergoth <kergoth>2002-01-25 22:14:26 (UTC)
commit15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff)
treec2fa0399a2c47fda8e2cd0092c73a809d17f68eb /library/timestring.cpp
downloadopie-15318cad33835e4e2dc620d033e43cd930676cdd.zip
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2
Initial revision
Diffstat (limited to 'library/timestring.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--library/timestring.cpp360
1 files changed, 360 insertions, 0 deletions
diff --git a/library/timestring.cpp b/library/timestring.cpp
new file mode 100644
index 0000000..d5d78ae
--- a/dev/null
+++ b/library/timestring.cpp
@@ -0,0 +1,360 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "timestring.h"
22#include <qobject.h>
23#include "qpeapplication.h" //for qApp
24#include "config.h"
25
26
27class TimeStringFormatKeeper : public QObject
28{
29 Q_OBJECT
30public:
31 static DateFormat currentFormat()
32 {
33 if ( !self )
34 self = new TimeStringFormatKeeper;
35 return self->format;
36 }
37private slots:
38 void formatChanged( DateFormat f )
39 {
40 format = f;
41 }
42private:
43 static TimeStringFormatKeeper *self;
44 DateFormat format;
45
46 TimeStringFormatKeeper()
47 : QObject( qApp )
48 {
49 Config config("qpe");
50 config.setGroup( "Date" );
51 format = DateFormat(QChar(config.readEntry("Separator", "/")[0]),
52 (DateFormat::Order)config .readNumEntry("ShortOrder", DateFormat::DayMonthYear),
53 (DateFormat::Order)config.readNumEntry("LongOrder", DateFormat::DayMonthYear));
54
55 connect( qApp, SIGNAL( dateFormatChanged(DateFormat) ),
56 this, SLOT( formatChanged( DateFormat ) ) );
57 }
58};
59
60TimeStringFormatKeeper *TimeStringFormatKeeper::self = 0;
61
62QString DateFormat::toNumberString() const
63{
64 QString buf = "";
65 // for each part of the order
66 for (int i = 0; i < 3; i++) {
67 // switch on the relavent 3 bits.
68 switch((_shortOrder >> (i * 3)) & 0x0007) {
69 case 0x0001:
70 buf += QObject::tr( "D" );
71 break;
72 case 0x0002:
73 buf += QObject::tr( "M" );
74 break;
75 case 0x0004:
76 buf += QObject::tr( "Y" );
77 break;
78 }
79 if (i < 2)
80 buf += _shortSeparator;
81 }
82 return buf;
83}
84
85QString DateFormat::toWordString() const
86{
87 QString buf = "";
88 // for each part of the order
89 for (int i = 0; i < 3; i++) {
90 // switch on the relavent 3 bits.
91 switch((_longOrder >> (i * 3)) & 0x0007) {
92 case 0x0001:
93 buf += QObject::tr( "day" );
94 if (i < 2) {
95 if ((_shortOrder << ((i+1) * 3)) & 0x0007)
96 buf += ", ";
97 else
98 buf += " ";
99 }
100 break;
101 case 0x0002:
102 buf += QObject::tr( "month" );
103 if (i < 2)
104 buf += " ";
105 break;
106 case 0x0004:
107 buf += QObject::tr( "year" );
108 if (i < 2)
109 buf += ", ";
110 break;
111 }
112 }
113 return buf;
114}
115
116QString DateFormat::numberDate(const QDate &d, int v) const
117{
118 QString buf = "";
119
120 int pad = 0;
121 if (v & padNumber)
122 pad = 2;
123
124 // for each part of the order
125 for (int i = 0; i < 3; i++) {
126 // switch on the relavent 3 bits.
127 switch((_shortOrder >> (i * 3)) & 0x0007) {
128 case 0x0001:
129 buf += QString("%1").arg(d.day(), pad);
130 break;
131 case 0x0002:
132 buf += QString("%1").arg(d.month(), pad);
133 break;
134 case 0x0004:
135 {
136 int year = d.year();
137 if (!(v & longNumber))
138 year = year % 100;
139
140 if (year < 10)
141 buf += "0";
142
143 buf += QString::number(year);
144
145 }
146 break;
147 }
148 if (i < 2)
149 buf = _shortSeparator;
150 }
151 return buf;
152}
153
154QString DateFormat::wordDate(const QDate &d, int v) const
155{
156 QString buf = "";
157 // for each part of the order
158 if (v & showWeekDay) {
159 QString weekDay = d.dayName(d.dayOfWeek());
160 if (!(v & longWord)) {
161 weekDay = weekDay.left(3);
162 }
163 buf += weekDay;
164 if (_longOrder & 0x0007 == 0x0002)
165 buf += ' ';
166 else
167 buf += ", ";
168 }
169
170 int pad = 0;
171 if (v & padNumber)
172 pad = 2;
173
174 for (int i = 0; i < 3; i++) {
175 // switch on the relavent 3 bits.
176 switch((_longOrder >> (i * 3)) & 0x0007) {
177 case 0x0001:
178 buf += QString("%1").arg(d.day(), pad);
179 if (i < 2) {
180 if ((_shortOrder << ((i+1) * 3)) & 0x0007)
181 buf += ", ";
182 else
183 buf += " ";
184 }
185 break;
186 case 0x0002:
187 {
188 QString monthName = d.monthName(d.month());
189 if (!(v & longWord)) {
190 monthName = monthName.left(3);
191 }
192 buf += monthName;
193 }
194 if (i < 2)
195 buf += " ";
196 break;
197 case 0x0004:
198 {
199 int year = d.year();
200 if (!(v & longNumber))
201 year = year % 100;
202
203 if (year < 10)
204 buf += "0";
205
206 buf += QString::number(year);
207 }
208 if (i < 2)
209 buf += ", ";
210 break;
211 }
212 }
213 return buf;
214}
215
216#ifndef QT_NO_DATASTREAM
217void DateFormat::save(QDataStream &d) const
218{
219 d << _shortSeparator.unicode();
220 uint v= _shortOrder;
221 d << v;
222 v = _longOrder;
223 d << v;
224}
225
226void DateFormat::load(QDataStream &d)
227{
228 ushort value;
229 d >> value;
230 _shortSeparator = QChar(value);
231 uint v = 0;
232 d >> v;
233 _shortOrder = (Order)v;
234 v = 0;
235 d >> v;
236 _longOrder = (Order)v;
237}
238
239QDataStream &operator<<(QDataStream &s, const DateFormat&df)
240{
241 df.save(s);
242 return s;
243}
244QDataStream &operator>>(QDataStream &s, DateFormat&df)
245{
246 df.load(s);
247 return s;
248}
249#endif
250
251QString TimeString::shortDate( const QDate &d, DateFormat dtf )
252{
253 return dtf.wordDate(d);
254}
255
256QString TimeString::dateString( const QDate &d, DateFormat dtf )
257{
258 return dtf.wordDate(d, DateFormat::longNumber | DateFormat::longWord);
259}
260
261
262QString TimeString::longDateString( const QDate &d, DateFormat dtf )
263{
264 return dtf.wordDate(d, DateFormat::showWeekDay | DateFormat::longNumber
265 | DateFormat::longWord);
266}
267
268DateFormat TimeString::currentDateFormat()
269{
270 return TimeStringFormatKeeper::currentFormat();
271}
272
273
274QString TimeString::dateString( const QDateTime &dt, bool ampm, bool seconds, DateFormat dtf )
275{
276 const QDate& d = dt.date();
277 const QTime& t = dt.time();
278
279 // based on QDateTime::toString()
280 QString buf = timeString(t,ampm,seconds);
281 buf += " ";
282 buf += longDateString( d, dtf );
283
284 return buf;
285}
286
287QString TimeString::timeString( const QTime &t, bool ampm, bool seconds )
288{
289 if ( !ampm ) {
290 if ( seconds )
291 return t.toString();
292 QString r = QString::number(t.hour());
293 if ( t.hour() < 10 ) r.prepend( "0" );
294 r.append( ":" );
295 if ( t.minute() < 10 ) r.append( "0" );
296 r.append(QString::number(t.minute()));
297 return r;
298 }
299 // ### else the hard case that should disappear in Qt 3.0
300 QString argString = seconds ? "%4:%5:%6 %7" : "%4:%5 %7";
301 int hour = t.hour();
302 QString strMin = QString::number( t.minute() );
303 QString strSec = QString::number( t.second() );
304 if ( hour > 12 )
305 argString = argString.arg( hour - 12, 2 );
306 else {
307 if ( hour == 0 )
308 argString = argString.arg( 12 );
309 else
310 argString = argString.arg( hour, 2 );
311 }
312 if ( t.minute() < 10 )
313 strMin.prepend( "0" );
314 if ( t.second() < 10 )
315 strSec.prepend( "0" );
316 argString = argString.arg( strMin );
317 if ( seconds )
318 argString = argString.arg( strSec );
319 if ( hour >= 12 )
320 argString = argString.arg( QObject::tr("PM") );
321 else
322 argString = argString.arg( QObject::tr("AM") );
323 return argString;
324}
325
326QString TimeString::shortTime( bool ampm, bool seconds )
327{
328 static const char* const day[] = {
329 QT_TRANSLATE_NOOP( "QObject", "Mon" ),
330 QT_TRANSLATE_NOOP( "QObject", "Tue" ),
331 QT_TRANSLATE_NOOP( "QObject", "Wed" ),
332 QT_TRANSLATE_NOOP( "QObject", "Thu" ),
333 QT_TRANSLATE_NOOP( "QObject", "Fri" ),
334 QT_TRANSLATE_NOOP( "QObject", "Sat" ),
335 QT_TRANSLATE_NOOP( "QObject", "Sun" )
336 };
337 // just create a shorter time String
338 QDateTime dtTmp = QDateTime::currentDateTime();
339 QString strTime;
340 strTime = QObject::tr( day[dtTmp.date().dayOfWeek()-1] ) + " " +
341 timeString( dtTmp.time(), ampm, seconds );
342 return strTime;
343}
344
345QString TimeString::dateString( const QDateTime &t, bool ampm )
346{
347 return dateString(t,ampm,FALSE);
348}
349
350QString TimeString::timeString( const QTime &t, bool ampm)
351{
352 return timeString(t,ampm,FALSE);
353}
354
355QString TimeString::shortTime( bool ampm )
356{
357 return shortTime(ampm,FALSE);
358}
359
360#include "timestring.moc"