KDECore
kcalendarsystem.cpp
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 #include <kglobal.h>
00026 
00027 #include "kcalendarsystem.h"
00028 #include "klocale.h"
00029 
00030 class KCalendarSystemPrivate
00031 {
00032 public:
00033   const KLocale * locale;
00034 };
00035 
00036 KCalendarSystem::KCalendarSystem(const KLocale * locale)
00037   : d(new KCalendarSystemPrivate)
00038 {
00039   d->locale = locale;
00040 }
00041 
00042 KCalendarSystem::~KCalendarSystem()
00043 {
00044   delete d;
00045 }
00046 
00047 const KLocale * KCalendarSystem::locale() const
00048 {
00049   if ( d->locale )
00050     return d->locale;
00051 
00052   return KGlobal::locale();
00053 }
00054 
00055 QString KCalendarSystem::dayString(const QDate & pDate, bool bShort) const
00056 {
00057   QString sResult;
00058 
00059   sResult.setNum(day(pDate));
00060   if (!bShort && sResult.length() == 1 )
00061     sResult.prepend('0');
00062 
00063   return sResult;
00064 }
00065 
00066 QString KCalendarSystem::monthString(const QDate & pDate, bool bShort) const
00067 {
00068   QString sResult;
00069 
00070   sResult.setNum(month(pDate));
00071   if (!bShort && sResult.length() == 1 )
00072     sResult.prepend('0');
00073 
00074   return sResult;
00075 }
00076 
00077 QString KCalendarSystem::yearString(const QDate & pDate, bool bShort) const
00078 {
00079   QString sResult;
00080 
00081   sResult.setNum(year(pDate));
00082   if (bShort && sResult.length() == 4 )
00083     sResult = sResult.right(2);
00084 
00085   return sResult;
00086 }
00087 
00088 static int stringToInteger(const QString & sNum, int & iLength)
00089 {
00090   unsigned int iPos = 0;
00091 
00092   int result = 0;
00093   for (; sNum.length() > iPos && sNum.at(iPos).isDigit(); iPos++)
00094     {
00095       result *= 10;
00096       result += sNum.at(iPos).digitValue();
00097     }
00098 
00099   iLength = iPos;
00100   return result;
00101 }
00102 
00103 
00104 int KCalendarSystem::dayStringToInteger(const QString & sNum, int & iLength) const
00105 {
00106   return stringToInteger(sNum, iLength);
00107 }
00108 
00109 int KCalendarSystem::monthStringToInteger(const QString & sNum, int & iLength) const
00110 {
00111   return stringToInteger(sNum, iLength);
00112 }
00113 
00114 int KCalendarSystem::yearStringToInteger(const QString & sNum, int & iLength) const
00115 {
00116   return stringToInteger(sNum, iLength);
00117 }
00118 
00119 QString KCalendarSystem::weekDayName (int weekDay, bool shortName) const
00120 {
00121   if ( shortName )
00122     switch ( weekDay )
00123       {
00124       case 1:  return locale()->translate("Monday", "Mon");
00125       case 2:  return locale()->translate("Tuesday", "Tue");
00126       case 3:  return locale()->translate("Wednesday", "Wed");
00127       case 4:  return locale()->translate("Thursday", "Thu");
00128       case 5:  return locale()->translate("Friday", "Fri");
00129       case 6:  return locale()->translate("Saturday", "Sat");
00130       case 7:  return locale()->translate("Sunday", "Sun");
00131       }
00132   else
00133     switch ( weekDay )
00134       {
00135       case 1:  return locale()->translate("Monday");
00136       case 2:  return locale()->translate("Tuesday");
00137       case 3:  return locale()->translate("Wednesday");
00138       case 4:  return locale()->translate("Thursday");
00139       case 5:  return locale()->translate("Friday");
00140       case 6:  return locale()->translate("Saturday");
00141       case 7:  return locale()->translate("Sunday");
00142       }
00143 
00144   return QString::null;
00145 }
00146