date modifiers for localizer
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 20 Jun 2010 08:48:09 +0000 (08:48 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 20 Jun 2010 08:48:09 +0000 (08:48 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@507 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/misc/misc.cpp
src/misc/misc.h

index 391d8f9..b175de1 100644 (file)
@@ -126,6 +126,11 @@ MLocalFormat::MLocalFormat()
 
 MLocalFormat::MLocalFormat(const MLocalFormat&f)
 {
+       operator=(f);
+}
+
+MLocalFormat& MLocalFormat::operator=(const MLocalFormat&f)
+{
        m_day=f.m_day;
        m_sday=f.m_sday;
        m_month=f.m_month;
@@ -135,6 +140,9 @@ MLocalFormat::MLocalFormat(const MLocalFormat&f)
        m_thousand=f.m_thousand;
        m_moneydecimals=f.m_moneydecimals;
        m_thousanddigits=f.m_thousanddigits;
+       m_am=f.m_am;
+       m_pm=f.m_pm;
+       return *this;
 }
 
 MLocalFormat::~MLocalFormat(){}
@@ -215,6 +223,14 @@ void MLocalFormat::setMoneyFormat(QString c,int  n)
        m_moneydecimals=n;
 }
 
+void MLocalFormat::setAP(QString am,QString pm)
+{
+       if(am=="--")m_am=CoreApplication::translate("MLocalFormat","am","AM/PM time component");
+       else m_am=am;
+       if(pm=="--")m_pm=CoreApplication::translate("MLocalFormat","pm","AM/PM time component");
+       else m_pm=pm;
+}
+
 void MLocalFormat::setNumberFormat(QChar dec,QChar thou,int dig)
 {
        if(dec.isNull())m_decimal=QCoreApplication::translate("MLocalFormat",".","decimal dot")[0];
@@ -229,13 +245,13 @@ void MLocalFormat::setNumberFormat(QChar dec,QChar thou,int dig)
 QString MLocalFormat::formatDate(const QDate&date,QString format)const
 {
        if(format=="")format=QCoreApplication::translate("MLocalFormat","%Y-%M-%D","date format");
-       return formatDateTime(date,format);
+       return formatDateTime(QDateTime(date),format);
 }
 QString MLocalFormat::formatDate(qint64 date,QString format)const
 {
        return formatDate(unix2localTime(date).date(),format);
 }
-QString MLocalFormat::formatTime(const QTime&time,QString format=QString())const
+QString MLocalFormat::formatTime(const QTime&time,QString format)const
 {
        if(format=="")format=QCoreApplication::translate("MLocalFormat","%h:%I","time format");
        return formatDateTime(QDateTime(QDate::currentDate(),time),format);
@@ -248,13 +264,103 @@ QString MLocalFormat::formatDateTime(const QDateTime&time,QString format)const
 {
        if(format=="")format=QCoreApplication::translate("MLocalFormat","%Y-%M-%D %H:%I","date and time format");
        //parse
-       ...
+       QString out;
+       bool inp=false;
+       for(int i=0;i<format.size();i++){
+               if(inp){
+                       switch(format[i].unicode()){
+                               //date format codes
+                               case 'Y':out+=QString::number(time.date().year());break;
+                               case 'y':{
+                                       QString y=QString::number(time.date().year()%100);
+                                       if(y.size()<2)y="0"+y;
+                                       out+=y;
+                                       break;
+                               }
+                               case 'm':out+=QString::number(time.date().month());break;
+                               case 'M':{
+                                       QString m=QString::number(time.date().month());
+                                       if(m.size()<2)m="0"+m;
+                                       out+=m;
+                                       break;
+                               }
+                               case 'n':out+=m_smonth[time.date().month()-1];break;
+                               case 'N':out+=m_month[time.date().month()-1];break;
+                               case 'd':out+=QString::number(time.date().day());break;
+                               case 'D':{
+                                       QString m=QString::number(time.date().day());
+                                       if(m.size()<2)m="0"+m;
+                                       out+=m;
+                                       break;
+                               }
+                               case 'w':out+=m_sday[time.date().dayOfWeek()-1];break;
+                               case 'W':out+=m_day[time.date().dayOfWeek()-1];break;
+                               //time formats
+                               case 'h':out+=QString::number(time.time().hour());break;
+                               case 'H':{
+                                       QString t=QString::number(time.time().hour());
+                                       if(t.size()<2)t="0"+t;
+                                       out+=t;
+                                       break;
+                               }
+                               case 'i':out+=QString::number(time.time().minute());break;
+                               case 'I':{
+                                       QString t=QString::number(time.time().minute());
+                                       if(t.size()<2)t="0"+t;
+                                       out+=t;
+                                       break;
+                               }
+                               case 's':out+=QString::number(time.time().second());break;
+                               case 'S':{
+                                       QString t=QString::number(time.time().second());
+                                       if(t.size()<2)t="0"+t;
+                                       out+=t;
+                                       break;
+                               }
+                               case 'a':{
+                                       int t=time.time().hour()%12;if(t==0)t=12;
+                                       out+=QString::number(t);
+                                       break;
+                               }
+                               case 'A':{
+                                       int th=time.time().hour()%12;if(th==0)th=12;
+                                       QString t=QString::number(th);
+                                       if(t.size()<2)t="0"+t;
+                                       out+=t;
+                                       break;
+                               }
+                               case 'p':{
+                                       int t=time.time().hour();
+                                       if(t>=1 && t<=12)out+="am";
+                                       else out+="pm";
+                                       break;
+                               }
+                               case 'P':{
+                                       int t=time.time().hour();
+                                       if(t>=1 && t<=12)out+="AM";
+                                       else out+="PM";
+                                       break;
+                               }
+                               //% sign
+                               case '%':out+="%";break;
+                               //mistakes
+                               default:out+="%"+format[i];break;
+                       }
+               }else{
+                       if(format[i]=='%')inp=true;
+                       else out+=format[i];
+               }
+       }
+       //catch mistaken % at end
+       if(inp)out+="%";
+       //return result
+       return out;
 }
 QString MLocalFormat::formatDateTime(qint64 time,QString format)const
 {
        return formatDateTime(unix2localTime(time),format);
 }
-QDateTime MLocalFormat::unix2localTime(qint64 time)
+QDateTime MLocalFormat::unix2localTime(qint64 time)const
 {
        //TODO: use proper time zones
        return QDateTime::fromTime_t(time);
index 40a1fa0..343076d 100644 (file)
@@ -89,6 +89,9 @@ class MLocalFormat
                /**deletes the formatter object*/
                virtual ~MLocalFormat();
                
+               /**copies a formatter object*/
+               virtual MLocalFormat& operator=(const MLocalFormat&);
+               
                /**overrides the full names of week days, an empty list resets to the local translation, otherwise the list must be exactly seven entries long and starts with Monday*/
                virtual void setWeekDays(const QStringList&l=QStringList());
                /**overrides the short week day names, an empty list resets to the local translation, otherwise the list must be exactly seven entries long and starts with Monday (Mon)*/
@@ -110,6 +113,9 @@ class MLocalFormat
                \param digitsDiv defines the amount of digits in a block, eg. the value 3 will format 1000 as 1,000*/
                virtual void setNumberFormat(QChar decimal=QChar(),QChar thousandDiv=QChar(),int digitsDiv=-1);
                
+               /**overrides the formatting of the AM/PM part of time display, resets to the localization if none given*/
+               virtual void setAP(QString am="--",QString pm="--");
+               
                /**formats a date according to the given format, if none is given, the translation default is used
                \param date a QDate used as input
                \param format a format string as specified by QDate::toString */
@@ -135,12 +141,12 @@ class MLocalFormat
                virtual QString formatDateTime(qint64 time,QString format=QString())const;
        protected:
                QStringList m_day,m_sday,m_month,m_smonth;
-               QString m_currency;
+               QString m_currency,m_am,m_pm;
                QChar m_decimal,m_thousand;
                int m_moneydecimals,m_thousanddigits;
                
                /** \internal helper to convert a unix timestamp to local time*/
-               virtual QDateTime unix2localTime(qint64);
+               virtual QDateTime unix2localTime(qint64)const;
 };
 
 #endif