1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
/**********************************************************************
** Copyright (C) 2000-2006 Trolltech AS. All rights reserved.
**
** This file is part of the Qtopia Environment.
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 2 of the License, or (at your
** option) any later version.
**
** A copy of the GNU GPL license version 2 is included in this package as
** LICENSE.GPL.
**
** This program is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU General Public License for more details.
**
** In addition, as a special exception Trolltech gives permission to link
** the code of this program with Qtopia applications copyrighted, developed
** and distributed by Trolltech under the terms of the Qtopia Personal Use
** License Agreement. You must comply with the GNU General Public License
** in all respects for all of the code used other than the applications
** licensed under the Qtopia Personal Use License Agreement. If you modify
** this file, you may extend this exception to your version of the file,
** but you are not obligated to do so. If you do not wish to do so, delete
** this exception statement from your version.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
#include "timestring.h"
#include <qobject.h>
#include <qdatetime.h>
#include <qapplication.h>
#include "config.h"
#include <time.h>
class TimeStringFormat : public QObject
{
Q_OBJECT
public:
static DateFormat currentFormat()
{
if ( !self )
self = new TimeStringFormat;
return self->format;
}
private slots:
void formatChanged( DateFormat f )
{
format = f;
}
private:
static TimeStringFormat *self;
DateFormat format;
TimeStringFormat()
: QObject( qApp )
{
Config config("qpe");
config.setGroup( "Date" );
format = ::DateFormat(QChar(config.readEntry("Separator", "/")[0]),
(::DateFormat::Order)config.readNumEntry("ShortOrder", ::DateFormat::DayMonthYear),
(::DateFormat::Order)config.readNumEntry("LongOrder", ::DateFormat::DayMonthYear));
connect( qApp, SIGNAL( dateFormatChanged(DateFormat) ),
this, SLOT( formatChanged(DateFormat) ) );
}
};
TimeStringFormat *TimeStringFormat::self = 0;
QString DateFormat::toNumberString() const
{
QString buf = "";
// for each part of the order
for (int i = 0; i < 3; i++) {
// switch on the relavent 3 bits.
switch((_shortOrder >> (i * 3)) & 0x0007) {
case 0x0001:
buf += TimeStringFormat::tr( "D", "first letter of the word 'Day'" );
break;
case 0x0002:
buf += TimeStringFormat::tr( "M" , "first letter of the word 'Month'" );
break;
case 0x0004:
buf += TimeStringFormat::tr( "Y" , "first letter of the word 'Year'" );
break;
}
if (i < 2)
buf += _shortSeparator;
}
return buf;
}
QString DateFormat::toWordString() const
{
QString buf = "";
// for each part of the order
for (int i = 0; i < 3; i++) {
// switch on the relavent 3 bits.
switch((_longOrder >> (i * 3)) & 0x0007) {
case 0x0001:
buf += TimeStringFormat::tr( "day", "in month" );
if (i < 2) {
if ((_shortOrder << ((i+1) * 3)) & 0x0007)
buf += ", ";
else
buf += " ";
}
break;
case 0x0002:
buf += TimeStringFormat::tr( "month" );
if (i < 2)
buf += " ";
break;
case 0x0004:
buf += TimeStringFormat::tr( "year" );
if (i < 2)
buf += ", ";
break;
}
}
return buf;
}
QString DateFormat::numberDate(const QDate &d, int v) const
{
QString buf = "";
int pad = 2;
// for each part of the order
for (int i = 0; i < 3; i++) {
// switch on the relavent 3 bits.
switch((_shortOrder >> (i * 3)) & 0x0007) {
case 0x0001:
if (pad==2) buf += QString().sprintf("%02d",d.day());
else buf += QString().sprintf("%d",d.day());
break;
case 0x0002:
if (i==0) { // no padding with only MM/DD/YY format
pad=0;
}
if (pad==2) buf += QString().sprintf("%02d",d.month());
else buf += QString().sprintf("%d",d.month());
break;
case 0x0004:
{
int year = d.year();
if (!(v & longNumber))
year = year % 100;
buf += QString().sprintf("%02d",year);
}
break;
}
if (i < 2)
buf += _shortSeparator;
}
return buf;
}
static const char* unTranslatedFullMonthNames[] = {
QT_TRANSLATE_NOOP( "QDate", "January" ),
QT_TRANSLATE_NOOP( "QDate", "February" ),
QT_TRANSLATE_NOOP( "QDate", "March" ),
QT_TRANSLATE_NOOP( "QDate", "April" ),
QT_TRANSLATE_NOOP( "QDate", "May" ),
QT_TRANSLATE_NOOP( "QDate", "June" ),
QT_TRANSLATE_NOOP( "QDate", "July" ),
QT_TRANSLATE_NOOP( "QDate", "August" ),
QT_TRANSLATE_NOOP( "QDate", "September" ),
QT_TRANSLATE_NOOP( "QDate", "October" ),
QT_TRANSLATE_NOOP( "QDate", "November" ),
QT_TRANSLATE_NOOP( "QDate", "December" )
};
static const char* unTranslatedFullDayNames[] = {
QT_TRANSLATE_NOOP( "QDate", "Monday" ),
QT_TRANSLATE_NOOP( "QDate", "Tuesday" ),
QT_TRANSLATE_NOOP( "QDate", "Wednesday" ),
QT_TRANSLATE_NOOP( "QDate", "Thursday" ),
QT_TRANSLATE_NOOP( "QDate", "Friday" ),
QT_TRANSLATE_NOOP( "QDate", "Saturday" ),
QT_TRANSLATE_NOOP( "QDate", "Sunday" )
};
#ifdef QTOPIA_DESKTOP
//translations in qt.qm
static const char* unTranslatedMediumDayNames[] = {
"Mon" , "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
};
static const char* unTranslatedMediumMonthNames[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
#endif
static QString dayname(const QDate& d, bool lng)
{
if (lng && qApp)
return qApp->translate("QDate", unTranslatedFullDayNames[d.dayOfWeek()-1]);
else {
#ifdef QTOPIA_DESKTOP
if (qApp)
return qApp->translate("QDate", unTranslatedMediumDayNames[ d.dayOfWeek()-1]);
#endif
return d.dayName(d.dayOfWeek());
}
}
QString DateFormat::wordDate(const QDate &d, int v) const
{
// for each part of the order
QString weekDay;
if (v & showWeekDay)
weekDay = ::dayname(d,(v & longWord));
QString date="";
QString sep="";
for (int i = 0; i < 3; i++) {
// switch on the relavent 3 bits.
int field = (_longOrder >> (i * 3)) & 0x0007;
if ( field && !date.isEmpty() )
date += sep;
switch (field) {
case 0x0001: // Day
{
QString daysuffix = TimeStringFormat::tr("@day", "day suffix - applies to some asian languages (e.g. Japanese and Trad. Chinese). If it doesn't apply to your language it has to be translated to an '@day' " );
if (i==1) {
date += QString().sprintf("%02d",d.day());
if (daysuffix != "@day")
date+=daysuffix;
sep = TimeStringFormat::tr(",","day-date separator") + " ";
} else {
date += QString().sprintf("%2d",d.day());
if (daysuffix == "@day") {
if (separator()=='.') // 2002/1/11
sep = ". ";
else
sep = " ";
} else {
date += daysuffix+" ";
sep = " ";
}
}
}
break;
case 0x0002: // Month
{
QString monthName;
if (v & longWord)
monthName = qApp->translate("QDate", unTranslatedFullMonthNames[d.month()-1] );
else {
#ifdef QTOPIA_DESKTOP
monthName = qApp->translate("QDate", unTranslatedMediumMonthNames[d.month()-1] );
#else
monthName = d.monthName( d.month() );
#endif
}
date += monthName;
}
sep = " ";//TimeStringFormat::tr(" ","month-date separator");
break;
case 0x0004: // Year
{
int year = d.year();
if (!(v & longNumber))
year = year % 100;
if (year < 10)
date += "0";
date += QString::number(year);
QString yearsuffix = TimeStringFormat::tr("@year", "year suffix - applies to some asian languages (e.g. Japanese and Trad. Chinese). If it doesn't apply to your language it has to be translated to an '@year' " );
if (yearsuffix != "@year")
date += yearsuffix;
}
sep = TimeStringFormat::tr(",","year-date seperator") + " ";
break;
}
}
QString r = "";
if ( weekDay.isEmpty() )
r = date;
else if ((_longOrder & 0x0007) == 0x0002)
r = TimeStringFormat::tr("%1 %2","1=Monday 2=January 12").arg(weekDay).arg(date);
else if ( _longOrder )
r = TimeStringFormat::tr("%1, %2","1=Monday 2=12 January").arg(weekDay).arg(date);
else
r = weekDay;
return r;
}
#ifndef QT_NO_DATASTREAM
void DateFormat::save(QDataStream &d) const
{
d << _shortSeparator.unicode();
uint v= _shortOrder;
d << v;
v = _longOrder;
d << v;
}
void DateFormat::load(QDataStream &d)
{
ushort value;
d >> value;
_shortSeparator = QChar(value);
uint v = 0;
d >> v;
_shortOrder = (Order)v;
v = 0;
d >> v;
_longOrder = (Order)v;
}
QDataStream &operator<<(QDataStream &s, const DateFormat&df)
{
df.save(s);
return s;
}
QDataStream &operator>>(QDataStream &s, DateFormat&df)
{
df.load(s);
return s;
}
#endif
QString TimeString::shortDate( const QDate &d, DateFormat dtf )
{
return dtf.wordDate(d);
}
QString TimeString::dateString( const QDate &d, DateFormat dtf )
{
return dtf.wordDate(d, DateFormat::longNumber);
}
QString TimeString::longDateString( const QDate &d, DateFormat dtf )
{
return dtf.wordDate(d, DateFormat::showWeekDay | DateFormat::longNumber
| DateFormat::longWord);
}
DateFormat TimeString::currentDateFormat()
{
return TimeStringFormat::currentFormat();
}
QString TimeString::dateString( const QDateTime &dt, bool ampm, bool seconds, DateFormat dtf )
{
const QDate& d = dt.date();
const QTime& t = dt.time();
// based on QDateTime::toString()
QString buf = timeString(t,ampm,seconds);
buf += " ";
buf += longDateString( d, dtf );
return buf;
}
QString TimeString::timeString( const QTime &t, bool ampm, bool seconds )
{
if ( !ampm ) {
if ( seconds )
return t.toString();
QString r = QString::number(t.hour());
if ( t.hour() < 10 ) r.prepend( "0" );
r.append( ":" );
if ( t.minute() < 10 ) r.append( "0" );
r.append(QString::number(t.minute()));
return r;
}
// ### else the hard case that should disappear in Qt 3.0
QString argString = seconds ? "%4:%5:%6 %7" : "%4:%5 %7";
int hour = t.hour();
QString strMin = QString::number( t.minute() );
QString strSec = QString::number( t.second() );
if ( hour > 12 )
argString = argString.arg( hour - 12, 2 );
else {
if ( hour == 0 )
argString = argString.arg( 12 );
else
argString = argString.arg( hour, 2 );
}
if ( t.minute() < 10 )
strMin.prepend( "0" );
if ( t.second() < 10 )
strSec.prepend( "0" );
argString = argString.arg( strMin );
if ( seconds )
argString = argString.arg( strSec );
if ( hour >= 12 )
argString = argString.arg( TimeStringFormat::tr("PM") );
else
argString = argString.arg( TimeStringFormat::tr("AM") );
return argString;
}
QString TimeString::shortTime( bool ampm, bool seconds )
{
// just create a shorter time String
QDateTime dtTmp = QDateTime::currentDateTime();
QString strTime = TimeStringFormat::tr( "%1 %2", "1=Monday 2=12:45" )
.arg(::dayname(dtTmp.date(),FALSE))
.arg(timeString( dtTmp.time(), ampm, seconds ));
return strTime;
}
QString TimeString::dateString( const QDateTime &t, bool ampm )
{
return dateString(t,ampm,FALSE);
}
QString TimeString::timeString( const QTime &t, bool ampm)
{
return timeString(t,ampm,FALSE);
}
QString TimeString::shortTime( bool ampm )
{
return shortTime(ampm,FALSE);
}
QString TimeString::numberDateString( const QDate &d, DateFormat dtf )
{
return dtf.numberDate(d);
}
QString TimeString::longNumberDateString( const QDate &d, DateFormat dtf )
{
return dtf.numberDate(d,DateFormat::longNumber);
}
/*!
Returns date/time \a dt as a string,
showing year, month, date, hours, minutes, and seconds.
\a len determines the length of the resulting string.
The format, including order depends on the user's settings.
First availability: Qtopia 1.6
*/
//QString TimeString::localYMDHMS( const QDateTime &dt, Length len )
//{
// const QDate& d = dt.date();
// const QTime& t = dt.time();
// return LocalTimeFormat::tr("%1 %2","date,time").arg(localYMD(d,len)).arg(localHMS(t));
//}
#include "timestring.moc"
|