Qtopia library API Documentation

timestring.cpp

00001 /**********************************************************************
00002 ** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of the Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 
00021 #include "timestring.h"
00022 #include <qobject.h>
00023 #include <qpe/qpeapplication.h> //for qApp
00024 #include "config.h"
00025 
00026 
00027 class TimeStringFormatKeeper : public QObject
00028 {
00029     Q_OBJECT
00030 public:
00031     static DateFormat currentFormat()
00032     {
00033     if ( !self )
00034         self  = new TimeStringFormatKeeper;
00035     return self->format;
00036     }
00037 private slots:
00038     void formatChanged( DateFormat f )
00039     {
00040     format = f;
00041     }
00042 private:
00043     static TimeStringFormatKeeper *self;
00044     DateFormat format;
00045 
00046     TimeStringFormatKeeper()
00047     : QObject( qApp )
00048     {
00049     Config config("qpe");
00050     config.setGroup( "Date" );
00051     format = DateFormat(QChar(config.readEntry("Separator", "/")[0]),
00052         (DateFormat::Order)config .readNumEntry("ShortOrder", DateFormat::DayMonthYear),
00053         (DateFormat::Order)config.readNumEntry("LongOrder", DateFormat::DayMonthYear));
00054 
00055     connect( qApp, SIGNAL( dateFormatChanged(DateFormat) ),
00056          this, SLOT( formatChanged( DateFormat ) ) );
00057     }
00058 };
00059 
00060 TimeStringFormatKeeper *TimeStringFormatKeeper::self = 0;
00061 
00062 QString DateFormat::toNumberString() const
00063 {
00064     QString buf = "";
00065     // for each part of the order
00066     for (int i = 0; i < 3; i++) {
00067     // switch on the relavent 3 bits.
00068     switch((_shortOrder >> (i * 3)) & 0x0007) {
00069         case 0x0001:
00070         buf += QObject::tr( "D" );
00071         break;
00072         case 0x0002:
00073         buf += QObject::tr( "M" );
00074         break;
00075         case 0x0004:
00076         buf += QObject::tr( "Y" );
00077         break;
00078     }
00079     if (i < 2)
00080         buf += _shortSeparator;
00081     }
00082     return buf;
00083 }
00084 
00085 QString DateFormat::toWordString() const
00086 {
00087     QString buf = "";
00088     // for each part of the order
00089     for (int i = 0; i < 3; i++) {
00090     // switch on the relavent 3 bits.
00091     switch((_longOrder >> (i * 3)) & 0x0007) {
00092         case 0x0001:
00093         buf += QObject::tr( "day" );
00094         if (i < 2) {
00095             if ((_shortOrder << ((i+1) * 3)) & 0x0007)
00096             buf += ", ";
00097             else
00098             buf += " ";
00099         }
00100         break;
00101         case 0x0002:
00102         buf += QObject::tr( "month" );
00103         if (i < 2)
00104             buf += " ";
00105         break;
00106         case 0x0004:
00107         buf += QObject::tr( "year" );
00108         if (i < 2)
00109             buf += ", ";
00110         break;
00111     }
00112     }
00113     return buf;
00114 }
00115 
00116 QString DateFormat::numberDate(const QDate &d, int v) const
00117 {
00118     QString buf = "";
00119 
00120     int pad = 2;
00121 
00122     // for each part of the order
00123     for (int i = 0; i < 3; i++) {
00124     // switch on the relavent 3 bits.
00125     switch((_shortOrder >> (i * 3)) & 0x0007) {
00126         case 0x0001:
00127           if (pad==2) buf += QString().sprintf("%02d",d.day());
00128           else buf += QString().sprintf("%d",d.day());
00129         break;
00130         case 0x0002:
00131           if (i==0) { // no padding with only MM/DD/YY format
00132         pad=0;
00133           }
00134           if (pad==2) buf += QString().sprintf("%02d",d.month());
00135           else buf += QString().sprintf("%d",d.month());
00136         break;
00137         case 0x0004:
00138         {
00139             int year = d.year();
00140             if (!(v & longNumber))
00141             year = year % 100;
00142             buf += QString().sprintf("%02d",year);
00143         }
00144         break;
00145     }
00146     if (i < 2)
00147         buf += _shortSeparator;
00148     }
00149     return buf;
00150 }
00151 
00152 QString DateFormat::wordDate(const QDate &d, int v) const
00153 {
00154     QString buf = "";
00155     // for each part of the order
00156     if (v & showWeekDay) {
00157     QString weekDay = d.dayName(d.dayOfWeek());
00158     if (!(v & longWord)) {
00159         weekDay = weekDay.left(3);
00160     }
00161     buf += weekDay;
00162     if ((_longOrder & 0x0007) == 0x0002)
00163         buf += ' ';
00164     else
00165         buf += ", ";
00166     }
00167 
00168     for (int i = 0; i < 3; i++) {
00169     // switch on the relavent 3 bits.
00170     switch((_longOrder >> (i * 3)) & 0x0007) {
00171         case 0x0001:
00172           if (i==1) {
00173         buf += QString().sprintf("%02d, ",d.day());
00174           } else {
00175         buf += QString().sprintf("%2d",d.day());
00176         if (separator()=='.') // 2002/1/11
00177           buf += ". ";
00178             else
00179             buf += " ";
00180         }
00181         break;
00182         case 0x0002:
00183         {
00184             QString monthName = d.monthName(d.month());
00185             if (!(v & longWord)) {
00186             monthName = monthName.left(3);
00187             }
00188             buf += monthName;
00189         }
00190         if (i < 2)
00191             buf += " ";
00192         break;
00193         case 0x0004:
00194         {
00195             int year = d.year();
00196             if (!(v & longNumber))
00197             year = year % 100;
00198 
00199             if (year < 10)
00200             buf += "0";
00201 
00202             buf += QString::number(year);
00203         }
00204         if (i < 2)
00205             buf += ", ";
00206         break;
00207     }
00208     }
00209     return buf;
00210 }
00211 
00212 #ifndef QT_NO_DATASTREAM
00213 void DateFormat::save(QDataStream &d) const
00214 {
00215     d << _shortSeparator.unicode();
00216     uint v= _shortOrder;
00217     d << v;
00218     v = _longOrder;
00219     d << v;
00220 }
00221 
00222 void DateFormat::load(QDataStream &d)
00223 {
00224     ushort value;
00225     d >> value;
00226     _shortSeparator = QChar(value);
00227     uint v = 0;
00228     d >> v;
00229     _shortOrder = (Order)v;
00230     v = 0;
00231     d >> v;
00232     _longOrder = (Order)v;
00233 }
00234 
00235 QDataStream &operator<<(QDataStream &s, const DateFormat&df)
00236 {
00237         df.save(s);
00238         return s;
00239 }
00240 QDataStream &operator>>(QDataStream &s, DateFormat&df)
00241 {
00242         df.load(s);
00243         return s;
00244 }
00245 #endif
00246 
00247 QString TimeString::shortDate( const QDate &d, DateFormat dtf )
00248 {
00249     return dtf.wordDate(d);
00250 }
00251 
00252 QString TimeString::dateString( const QDate &d, DateFormat dtf )
00253 {
00254     return QObject::tr( dtf.wordDate(d, DateFormat::longNumber | DateFormat::longWord) );
00255 }
00256 
00257 
00258 QString TimeString::longDateString( const QDate &d, DateFormat dtf )
00259 {
00260     return QObject::tr( dtf.wordDate(d, DateFormat::showWeekDay | DateFormat::longNumber
00261         | DateFormat::longWord) );
00262 }
00263 
00264 DateFormat TimeString::currentDateFormat()
00265 {
00266     return TimeStringFormatKeeper::currentFormat();
00267 }
00268 
00269 
00270 QString TimeString::dateString( const QDateTime &dt, bool ampm, bool seconds, DateFormat dtf )
00271 {
00272     const QDate& d = dt.date();
00273     const QTime& t = dt.time();
00274 
00275     // based on QDateTime::toString()
00276     QString buf = timeString(t,ampm,seconds);
00277     buf += " ";
00278     buf += longDateString( d, dtf );
00279 
00280     return buf;
00281 }
00282 
00283 QString TimeString::timeString( const QTime &t, bool ampm, bool seconds )
00284 {
00285     if ( !ampm ) {
00286     if ( seconds )
00287         return t.toString();
00288     QString r = QString::number(t.hour());
00289     if ( t.hour() < 10 ) r.prepend( "0" );
00290     r.append( ":" );
00291     if ( t.minute() < 10 ) r.append( "0" );
00292     r.append(QString::number(t.minute()));
00293     return r;
00294     }
00295     // ### else the hard case that should disappear in Qt 3.0
00296     QString argString = seconds ? "%4:%5:%6 %7" : "%4:%5 %7";
00297     int hour = t.hour();
00298     QString strMin = QString::number( t.minute() );
00299     QString strSec = QString::number( t.second() );
00300     if ( hour > 12 )
00301     argString = argString.arg( hour - 12, 2 );
00302     else {
00303         if ( hour == 0 )
00304         argString = argString.arg( 12 );
00305         else
00306         argString = argString.arg( hour, 2 );
00307     }
00308     if ( t.minute() < 10 )
00309     strMin.prepend( "0" );
00310     if ( t.second() < 10 )
00311     strSec.prepend( "0" );
00312     argString = argString.arg( strMin );
00313     if ( seconds )
00314     argString = argString.arg( strSec );
00315     if ( hour >= 12 )
00316     argString = argString.arg( QObject::tr("PM") );
00317     else
00318     argString = argString.arg( QObject::tr("AM") );
00319     return argString;
00320 }
00321 
00322 QString TimeString::shortTime( bool ampm, bool seconds )
00323 {
00324     static const char* const day[] = {
00325         QT_TRANSLATE_NOOP( "QObject", "Mon" ),
00326         QT_TRANSLATE_NOOP( "QObject", "Tue" ),
00327         QT_TRANSLATE_NOOP( "QObject", "Wed" ),
00328         QT_TRANSLATE_NOOP( "QObject", "Thu" ),
00329         QT_TRANSLATE_NOOP( "QObject", "Fri" ),
00330         QT_TRANSLATE_NOOP( "QObject", "Sat" ),
00331         QT_TRANSLATE_NOOP( "QObject", "Sun" )
00332     };
00333     // just create a shorter time String
00334     QDateTime dtTmp = QDateTime::currentDateTime();
00335     QString strTime;
00336     strTime = QObject::tr( day[dtTmp.date().dayOfWeek()-1] ) + " " +
00337     timeString( dtTmp.time(), ampm, seconds );
00338     return strTime;
00339 }
00340 
00341 QString TimeString::dateString( const QDateTime &t, bool ampm )
00342 {
00343     return dateString(t,ampm,FALSE);
00344 }
00345 
00346 QString TimeString::timeString( const QTime &t, bool ampm)
00347 {
00348     return timeString(t,ampm,FALSE);
00349 }
00350 
00351 QString TimeString::shortTime( bool ampm )
00352 {
00353     return shortTime(ampm,FALSE);
00354 }
00355 
00356 QString TimeString::numberDateString( const QDate &d, DateFormat dtf )
00357 {
00358   return dtf.numberDate(d);
00359 }
00360 QString TimeString::longNumberDateString( const QDate &d, DateFormat dtf )
00361 {
00362   return dtf.numberDate(d,DateFormat::longNumber);
00363 }
00364 
00365 #include "timestring.moc"
KDE Logo
This file is part of the documentation for OPIE Version 1.5.5.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:24:08 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001