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