summaryrefslogtreecommitdiffabout
authorulf69 <ulf69>2004-10-07 01:12:08 (UTC)
committer ulf69 <ulf69>2004-10-07 01:12:08 (UTC)
commitf1eb5b74c962909851607c4b4cb05ee18a347d37 (patch) (side-by-side diff)
treefa98d9fc97e5d75810936917dc6c6039e7c79b45
parentf434ef382b60c1a420dd0e037d119d082e478491 (diff)
downloadkdepimpi-f1eb5b74c962909851607c4b4cb05ee18a347d37.zip
kdepimpi-f1eb5b74c962909851607c4b4cb05ee18a347d37.tar.gz
kdepimpi-f1eb5b74c962909851607c4b4cb05ee18a347d37.tar.bz2
added easy to use method to read Date with certain format
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--microkde/kdecore/klocale.cpp17
-rw-r--r--microkde/kdecore/klocale.h2
2 files changed, 19 insertions, 0 deletions
diff --git a/microkde/kdecore/klocale.cpp b/microkde/kdecore/klocale.cpp
index 17031c7..8b4513e 100644
--- a/microkde/kdecore/klocale.cpp
+++ b/microkde/kdecore/klocale.cpp
@@ -427,384 +427,401 @@ QDate KLocale::readDate(const QString &intstr, const QString &fmt, bool* ok) con
break;
case 'n':
case 'm':
month = readInt(str, strpos);
if (month < 1 || month > 12)
goto error;
break;
case 'Y':
case 'y':
year = readInt(str, strpos);
if (year < 0)
goto error;
// Qt treats a year in the range 0-100 as 1900-1999.
// It is nicer for the user if we treat 0-68 as 2000-2068
if (year < 69)
year += 2000;
else if (c == 'y')
year += 1900;
break;
}
}
//kdDebug(173) << "KLocale::readDate day=" << day << " month=" << month << " year=" << year << endl;
if ( year != -1 && month != -1 && day != -1 )
{
if (ok) *ok = true;
return QDate(year, month, day);
}
error:
if (ok) *ok = false;
return QDate(); // invalid date
}
QTime KLocale::readTime(const QString &intstr, bool *ok) const
{
QTime _time;
_time = readTime(intstr, true, ok);
if (_time.isValid()) return _time;
return readTime(intstr, false, ok);
}
QTime KLocale::readTime(const QString &intstr, bool seconds, bool *ok) const
{
QString str = intstr.simplifyWhiteSpace().lower();
QString Format = timeFormat().simplifyWhiteSpace();
if (!seconds)
Format.replace(QRegExp(QString::fromLatin1(".%S")), QString::null);
int hour = -1, minute = -1, second = seconds ? -1 : 0; // don't require seconds
bool g_12h = false;
bool pm = false;
uint strpos = 0;
uint Formatpos = 0;
while (Format.length() > Formatpos || str.length() > strpos)
{
if ( !(Format.length() > Formatpos && str.length() > strpos) ) goto error;
QChar c = Format.at(Formatpos++);
if (c != '%')
{
if (c.isSpace())
strpos++;
else if (c != str.at(strpos++))
goto error;
continue;
}
// remove space at the begining
if (str.length() > strpos && str.at(strpos).isSpace())
strpos++;
c = Format.at(Formatpos++);
switch (c)
{
case 'p':
{
QString s;
s = i18n("pm").lower();
int len = s.length();
if (str.mid(strpos, len) == s)
{
pm = true;
strpos += len;
}
else
{
s = i18n("am").lower();
len = s.length();
if (str.mid(strpos, len) == s) {
pm = false;
strpos += len;
}
else
goto error;
}
}
break;
case 'k':
case 'H':
g_12h = false;
hour = readInt(str, strpos);
if (hour < 0 || hour > 23)
goto error;
break;
case 'l':
case 'I':
g_12h = true;
hour = readInt(str, strpos);
if (hour < 1 || hour > 12)
goto error;
break;
case 'M':
minute = readInt(str, strpos);
if (minute < 0 || minute > 59)
goto error;
break;
case 'S':
second = readInt(str, strpos);
if (second < 0 || second > 59)
goto error;
break;
}
}
if (g_12h)
{
hour %= 12;
if (pm) hour += 12;
}
if (ok) *ok = true;
return QTime(hour, minute, second);
error:
if (ok) *ok = false;
return QTime(-1, -1, -1); // return invalid date if it didn't work
// This will be removed in the near future, since it gives a warning on stderr.
// The presence of the bool* (since KDE-3.0) removes the need for an invalid QTime.
}
QDateTime KLocale::readDateTime(const QString &intstr,
IntDateFormat intIntDateFormat,
bool* ok) const
{
bool ok1, ok2;
// AT the moment we can not read any other format then ISODate
if ( intIntDateFormat != ISODate )
{
qDebug("KLocale::readDateTime, only ISODate is supported.");
return QDateTime();
}
int pos = intstr.find("T");
QString date = intstr.left(pos);
QString time = intstr.mid(pos+1);
QString dformat = dateFormat(intIntDateFormat);
QString tformat = timeFormat(intIntDateFormat);
QDate m_date = readDate(date, dformat, &ok1);
QTime m_time = readTime(time, tformat, &ok2);
if (ok)
{
if ((ok1 == false) || (ok2 == false))
*ok = false;
else
*ok = true;
}
QDateTime m_dt;
m_dt.setDate(m_date);
m_dt.setTime(m_time);
qDebug("KLocale::readDateTime() transformed %s into %s (%s), %s (%s) : err1=%i, err2=%i", intstr.latin1(), date.latin1(), dformat.latin1(), time.latin1(), tformat.latin1(), ok1, ok2);
return m_dt;
}
+QDate KLocale::readDate(const QString &intstr,
+ IntDateFormat intIntDateFormat,
+ bool* ok) const
+{
+ bool ok1;
+
+ QString dformat = dateFormat(intIntDateFormat);
+
+ QDate m_date = readDate(intstr, dformat, &ok1);
+
+ if (ok)
+ *ok = ok1;
+
+ //qDebug("KLocale::readDate() transformed %s into %s (%s), %s (%s) : err1=%i, err2=%i", intstr.latin1(), date.latin1(), dformat.latin1(), time.latin1(), tformat.latin1(), ok1, ok2);
+ return m_date;
+}
+
bool KLocale::use12Clock() const
{
return !mHourF24Format ;;
}
bool KLocale::weekStartsMonday() const
{
return mWeekStartsMonday;
}
int KLocale::weekStartDay() const
{
if ( mWeekStartsMonday )
return 1;
return 7;
}
QString KLocale::weekDayName(int i,bool shortName) const
{
if ( shortName )
switch ( i )
{
case 1: return i18n("Monday", "Mon");
case 2: return i18n("Tuesday", "Tue");
case 3: return i18n("Wednesday", "Wed");
case 4: return i18n("Thursday", "Thu");
case 5: return i18n("Friday", "Fri");
case 6: return i18n("Saturday", "Sat");
case 7: return i18n("Sunday", "Sun");
}
else
switch ( i )
{
case 1: return i18n("Monday");
case 2: return i18n("Tuesday");
case 3: return i18n("Wednesday");
case 4: return i18n("Thursday");
case 5: return i18n("Friday");
case 6: return i18n("Saturday");
case 7: return i18n("Sunday");
}
return QString::null;
}
QString KLocale::monthName(int i,bool shortName) const
{
if ( shortName )
switch ( i )
{
case 1: return i18n("January", "Jan");
case 2: return i18n("February", "Feb");
case 3: return i18n("March", "Mar");
case 4: return i18n("April", "Apr");
case 5: return i18n("May short", "May");
case 6: return i18n("June", "Jun");
case 7: return i18n("July", "Jul");
case 8: return i18n("August", "Aug");
case 9: return i18n("September", "Sep");
case 10: return i18n("October", "Oct");
case 11: return i18n("November", "Nov");
case 12: return i18n("December", "Dec");
}
else
switch (i)
{
case 1: return i18n("January");
case 2: return i18n("February");
case 3: return i18n("March");
case 4: return i18n("April");
case 5: return i18n("May long", "May");
case 6: return i18n("June");
case 7: return i18n("July");
case 8: return i18n("August");
case 9: return i18n("September");
case 10: return i18n("October");
case 11: return i18n("November");
case 12: return i18n("December");
}
return QString::null;
}
QString KLocale::country() const
{
return QString::null;
}
QString KLocale::dateFormat(IntDateFormat intIntDateFormat) const
{
const IntDateFormat dformat = (intIntDateFormat == Undefined)?mIntDateFormat:intIntDateFormat;
if ( dformat == ISODate )
return "%Y-%m-%d";
if ( QApplication::desktop()->width() < 480 ) {
if ( dformat == Default )
return "%a %d %b %Y";
else if ( dformat == Format1 )
return "%a %b %d %Y";
} else {
if ( dformat == Default )
return "%A %d %B %Y";
else if ( dformat == Format1 )
return "%A %B %d %Y";
}
return mDateFormat ;
}
QString KLocale::dateFormatShort(IntDateFormat intIntDateFormat) const
{
const IntDateFormat dformat = (intIntDateFormat == Undefined)?mIntDateFormat:intIntDateFormat;
if ( dformat == Default )
return "%d.%m.%Y";
else if ( dformat == Format1 )
return "%m.%d.%Y";
else if ( dformat == ISODate ) // = Qt::ISODate
return "%Y-%m-%d";
return mDateFormatShort ;
}
QString KLocale::timeFormat(IntDateFormat intIntTimeFormat) const
{
const IntDateFormat tformat = (intIntTimeFormat == Undefined)?mIntTimeFormat:intIntTimeFormat;
if ( tformat == Default )
if ( mHourF24Format)
return "%H:%M:%S";
else
return "%I:%M:%S%p";
else if ( tformat == Format1 )
if ( mHourF24Format)
return "%H:%M:%S";
else
return "%I:%M:%S%p";
else if ( tformat == ISODate ) // = Qt::ISODate
if ( mHourF24Format)
return "%H:%M:%S";
else
return "%I:%M:%S%p";
}
void KLocale::insertCatalogue ( const QString & )
{
}
KCalendarSystem *KLocale::calendar()
{
if ( !mCalendarSystem ) {
mCalendarSystem = new KCalendarSystemGregorian;
}
return mCalendarSystem;
}
int KLocale::timezoneOffset( QString timeZone )
{
int ret = 1001;
int index = mTimeZoneList.findIndex( timeZone );
if ( index < 24 )
ret = ( index-11 ) * 60 ;
return ret;
}
QStringList KLocale::timeZoneList() const
{
return mTimeZoneList;
}
void KLocale::setTimezone( const QString &timeZone )
{
mTimeZoneOffset = timezoneOffset( timeZone );
}
void KLocale::setDaylightSaving( bool b, int start , int end )
{
daylightEnabled = b;
daylightStart = start;
daylightEnd = end;
mSouthDaylight = (end < start);
// qDebug("klocale daylight %d %d %d ", b, start , end );
}
int KLocale::localTimeOffset( const QDateTime &dt )
{
diff --git a/microkde/kdecore/klocale.h b/microkde/kdecore/klocale.h
index 949301a..5783530 100644
--- a/microkde/kdecore/klocale.h
+++ b/microkde/kdecore/klocale.h
@@ -1,116 +1,118 @@
#ifndef MINIKDE_KLOCALE_H
#define MINIKDE_KLOCALE_H
#include <qstring.h>
#include <qstringlist.h>
#include <qdatetime.h>
#include <qdict.h>
#ifndef I18N_NOOP
#define I18N_NOOP(x) (x)
#endif
class KCalendarSystem;
void setLocaleDict( QDict<QString> * dict );
QString i18n(const char *text);
QString i18n(const char *hint, const char *text);
QString i18n(const char *text1, const char *textn, int num);
// Qt3's uic generates i18n( "msg", "comment" ) calls which conflict
// with our i18n method. we use uic -tr tr2i18n to redirect
// to the right i18n() function
inline QString tr2i18n(const char* message, const char* =0) {
return i18n( message);
}
class KLocale
{
public:
KLocale();
QString formatNumber(double num, int precision = -1) const;
QString formatNumber(const QString &numStr) const;
double readNumber(const QString &numStr, bool * ok = 0) const;
QString decimalSymbol() const;
QString thousandsSeparator() const;
QString positiveSign() const;
QString negativeSign() const;
QString translate( const char *index ) const;
QString translate( const char *index, const char *fallback) const;
enum IntDateFormat { Undefined=-1, Default=0, Format1=1, ISODate=2, Userdefined=3 };
QString formatDate(const QDate &pDate, bool shortFormat = false, IntDateFormat intIntDateFormat = Undefined) const;
QString formatTime(const QTime &pTime, bool includeSecs = false, IntDateFormat intIntDateFormat = Undefined) const;
QString formatDateTime(const QDateTime &pDateTime, IntDateFormat intIntDateFormat = Undefined) const;
QString formatDateTime(const QDateTime &pDateTime,
bool shortFormat,
bool includeSecs = false, IntDateFormat intIntDateFormat = Undefined) const;
QDate readDate(const QString &str, bool* ok = 0) const;
QDate readDate( const QString &intstr, const QString &fmt, bool* ok = 0) const;
QTime readTime(const QString &str, bool* ok = 0) const;
+ QDate readDate(const QString &intstr, IntDateFormat intIntDateFormat, bool* ok) const;
+
QDateTime readDateTime(const QString &intstr, IntDateFormat intIntDateFormat, bool* ok) const;
bool use12Clock() const;
bool weekStartsMonday() const;
int weekStartDay() const;
QString weekDayName(int,bool=false) const;
QString monthName(int,bool=false) const;
QString country() const;
QString dateFormat(IntDateFormat intIntDateFormat = Undefined) const;
QString dateFormatShort(IntDateFormat intIntDateFormat = Undefined) const;
QString timeFormat(IntDateFormat intIntDateFormat = Undefined) const;
void insertCatalogue ( const QString & );
KCalendarSystem *calendar();
void setHore24Format ( bool );
void setWeekStartMonday( bool );
void setIntDateFormat( IntDateFormat );
void setIntTimeFormat( IntDateFormat );
IntDateFormat getIntDateFormat( );
IntDateFormat getIntTimeFormat( );
void setLanguage( int );
void setDateFormat( QString );
void setDateFormatShort( QString );
QString m_decimalSymbol;
QString m_thousandsSeparator;
QString m_currencySymbol;
QString m_monetaryDecimalSymbol;
QString m_monetaryThousandsSeparator;
QString m_positiveSign;
QString m_negativeSign;
int timezoneOffset( QString );
QStringList timeZoneList() const;
void setDaylightSaving( bool, int , int );
int localTimeOffset(const QDateTime &);
void setTimezone( const QString &timeZone );
private:
QTime readTime(const QString &str, bool seconds, bool *ok) const;
QDate readDate(const QString &str, bool shortFormat, bool *ok) const;
KCalendarSystem *mCalendarSystem;
bool mWeekStartsMonday;
bool mHourF24Format;
IntDateFormat mIntDateFormat;
IntDateFormat mIntTimeFormat;
int mLanguage;
QString mDateFormat;
QString mDateFormatShort;
QStringList mTimeZoneList;
bool daylightEnabled;
int mDaylightTZoffset;
int mNondaylightTZoffset;
bool mSouthDaylight;
int daylightStart, daylightEnd, mTimeZoneOffset;
};
#endif