summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qdatetime.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/tools/qdatetime.cpp')
-rw-r--r--src/corelib/tools/qdatetime.cpp206
1 files changed, 9 insertions, 197 deletions
diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp
index 0660da0894..481a4b3123 100644
--- a/src/corelib/tools/qdatetime.cpp
+++ b/src/corelib/tools/qdatetime.cpp
@@ -168,8 +168,8 @@ int qt_monthNumberFromShortName(const QString &shortName)
return -1;
}
#endif
+
#ifndef QT_NO_DATESTRING
-static QString fmtDateTime(const QString& f, const QTime* dt = 0, const QDate* dd = 0);
static void rfcDateImpl(const QString &s, QDate *dd = 0, QTime *dt = 0, int *utfcOffset = 0);
#endif
static QDateTimePrivate::Spec utcToLocal(QDate &date, QTime &time);
@@ -964,14 +964,14 @@ QString QDate::toString(Qt::DateFormat f) const
range 0 to 9999. This restriction may apply to locale-aware
formats as well, depending on the locale settings.
- \sa QDateTime::toString(), QTime::toString()
+ \sa QDateTime::toString(), QTime::toString(), QLocale::toString()
*/
QString QDate::toString(const QString& format) const
{
if (year() > 9999)
return QString();
- return fmtDateTime(format, 0, this);
+ return QLocale::system().toString(*this, format);
}
#endif //QT_NO_DATESTRING
@@ -1720,11 +1720,11 @@ QString QTime::toString(Qt::DateFormat format) const
If the time is invalid, an empty string will be returned.
If \a format is empty, the default format "hh:mm:ss" is used.
- \sa QDate::toString(), QDateTime::toString()
+ \sa QDate::toString(), QDateTime::toString(), QLocale::toString()
*/
QString QTime::toString(const QString& format) const
{
- return fmtDateTime(format, this, 0);
+ return QLocale::system().toString(*this, format);
}
#endif //QT_NO_DATESTRING
/*!
@@ -2977,11 +2977,11 @@ QString QDateTime::toString(Qt::DateFormat f) const
If the datetime is invalid, an empty string will be returned.
- \sa QDate::toString(), QTime::toString()
+ \sa QDate::toString(), QTime::toString(), QLocale::toString()
*/
QString QDateTime::toString(const QString& format) const
{
- return fmtDateTime(format, &d->time, &d->date);
+ return QLocale::system().toString(*this, format);
}
#endif //QT_NO_DATESTRING
@@ -4162,200 +4162,11 @@ QDataStream &operator>>(QDataStream &in, QDateTime &dateTime)
#endif // QT_NO_DATASTREAM
-
-// checks if there is an unquoted 'AP' or 'ap' in the string
-static bool hasUnquotedAP(const QString &f)
-{
- const QLatin1Char quote('\'');
- bool inquote = false;
- const int max = f.size();
- for (int i=0; i<max; ++i) {
- if (f.at(i) == quote) {
- inquote = !inquote;
- } else if (!inquote && f.at(i).toUpper() == QLatin1Char('A')) {
- return true;
- }
- }
- return false;
-}
-
-#ifndef QT_NO_DATESTRING
/*****************************************************************************
Some static function used by QDate, QTime and QDateTime
*****************************************************************************/
-// Replaces tokens by their value. See QDateTime::toString() for a list of valid tokens
-static QString getFmtString(const QString& f, const QTime* dt = 0, const QDate* dd = 0, bool am_pm = false)
-{
- if (f.isEmpty())
- return QString();
-
- QString buf = f;
- int removed = 0;
-
- if (dt) {
- if (f.startsWith(QLatin1String("hh")) || f.startsWith(QLatin1String("HH"))) {
- const bool hour12 = f.at(0) == QLatin1Char('h') && am_pm;
- if (hour12 && dt->hour() > 12)
- buf = QString::number(dt->hour() - 12).rightJustified(2, QLatin1Char('0'), true);
- else if (hour12 && dt->hour() == 0)
- buf = QLatin1String("12");
- else
- buf = QString::number(dt->hour()).rightJustified(2, QLatin1Char('0'), true);
- removed = 2;
- } else if (f.at(0) == QLatin1Char('h') || f.at(0) == QLatin1Char('H')) {
- const bool hour12 = f.at(0) == QLatin1Char('h') && am_pm;
- if (hour12 && dt->hour() > 12)
- buf = QString::number(dt->hour() - 12);
- else if (hour12 && dt->hour() == 0)
- buf = QLatin1String("12");
- else
- buf = QString::number(dt->hour());
- removed = 1;
- } else if (f.startsWith(QLatin1String("mm"))) {
- buf = QString::number(dt->minute()).rightJustified(2, QLatin1Char('0'), true);
- removed = 2;
- } else if (f.at(0) == (QLatin1Char('m'))) {
- buf = QString::number(dt->minute());
- removed = 1;
- } else if (f.startsWith(QLatin1String("ss"))) {
- buf = QString::number(dt->second()).rightJustified(2, QLatin1Char('0'), true);
- removed = 2;
- } else if (f.at(0) == QLatin1Char('s')) {
- buf = QString::number(dt->second());
- } else if (f.startsWith(QLatin1String("zzz"))) {
- buf = QString::number(dt->msec()).rightJustified(3, QLatin1Char('0'), true);
- removed = 3;
- } else if (f.at(0) == QLatin1Char('z')) {
- buf = QString::number(dt->msec());
- removed = 1;
- } else if (f.at(0).toUpper() == QLatin1Char('A')) {
- const bool upper = f.at(0) == QLatin1Char('A');
- buf = dt->hour() < 12 ? QLatin1String("am") : QLatin1String("pm");
- if (upper)
- buf = buf.toUpper();
- if (f.size() > 1 && f.at(1).toUpper() == QLatin1Char('P') &&
- f.at(0).isUpper() == f.at(1).isUpper()) {
- removed = 2;
- } else {
- removed = 1;
- }
- }
- }
-
- if (dd) {
- if (f.startsWith(QLatin1String("dddd"))) {
- buf = dd->longDayName(dd->dayOfWeek());
- removed = 4;
- } else if (f.startsWith(QLatin1String("ddd"))) {
- buf = dd->shortDayName(dd->dayOfWeek());
- removed = 3;
- } else if (f.startsWith(QLatin1String("dd"))) {
- buf = QString::number(dd->day()).rightJustified(2, QLatin1Char('0'), true);
- removed = 2;
- } else if (f.at(0) == QLatin1Char('d')) {
- buf = QString::number(dd->day());
- removed = 1;
- } else if (f.startsWith(QLatin1String("MMMM"))) {
- buf = dd->longMonthName(dd->month());
- removed = 4;
- } else if (f.startsWith(QLatin1String("MMM"))) {
- buf = dd->shortMonthName(dd->month());
- removed = 3;
- } else if (f.startsWith(QLatin1String("MM"))) {
- buf = QString::number(dd->month()).rightJustified(2, QLatin1Char('0'), true);
- removed = 2;
- } else if (f.at(0) == QLatin1Char('M')) {
- buf = QString::number(dd->month());
- removed = 1;
- } else if (f.startsWith(QLatin1String("yyyy"))) {
- const int year = dd->year();
- buf = QString::number(qAbs(year)).rightJustified(4, QLatin1Char('0'));
- if(year > 0)
- removed = 4;
- else
- {
- buf.prepend(QLatin1Char('-'));
- removed = 5;
- }
-
- } else if (f.startsWith(QLatin1String("yy"))) {
- buf = QString::number(dd->year()).right(2).rightJustified(2, QLatin1Char('0'));
- removed = 2;
- }
- }
- if (removed == 0 || removed >= f.size()) {
- return buf;
- }
-
- return buf + getFmtString(f.mid(removed), dt, dd, am_pm);
-}
-
-// Parses the format string and uses getFmtString to get the values for the tokens. Ret
-static QString fmtDateTime(const QString& f, const QTime* dt, const QDate* dd)
-{
- QString buf;
-
- if (f.isEmpty())
- return buf;
- if (dt && !dt->isValid())
- return buf;
- if (dd && !dd->isValid())
- return buf;
-
- const bool ap = hasUnquotedAP(f);
-
- QString frm;
- uint status = '0';
-
- for (int i = 0, n = f.length(); i < n; ++i) {
- const QChar c = f.at(i);
- const uint cc = c.unicode();
- if (cc == '\'') {
- if (status == cc) {
- if (i > 0 && f.at(i - 1).unicode() == cc)
- buf += c;
- status = '0';
- } else {
- if (!frm.isEmpty()) {
- buf += getFmtString(frm, dt, dd, ap);
- frm.clear();
- }
- status = cc;
- }
- } else if (status == '\'') {
- buf += c;
- } else if (c == status) {
- if (ap && (cc == 'P' || cc == 'p'))
- status = '0';
- frm += c;
- } else {
- buf += getFmtString(frm, dt, dd, ap);
- frm.clear();
- if (cc == 'h' || cc == 'm' || cc == 'H' || cc == 's' || cc == 'z') {
- status = cc;
- frm += c;
- } else if (cc == 'd' || cc == 'M' || cc == 'y') {
- status = cc;
- frm += c;
- } else if (ap && cc == 'A') {
- status = 'P';
- frm += c;
- } else if (ap && cc == 'a') {
- status = 'p';
- frm += c;
- } else {
- buf += c;
- status = '0';
- }
- }
- }
-
- buf += getFmtString(frm, dt, dd, ap);
-
- return buf;
-}
-
+#ifndef QT_NO_DATESTRING
static void rfcDateImpl(const QString &s, QDate *dd, QTime *dt, int *utcOffset)
{
int day = -1;
@@ -4419,6 +4230,7 @@ static void rfcDateImpl(const QString &s, QDate *dd, QTime *dt, int *utcOffset)
}
#endif // QT_NO_DATESTRING
+
#ifdef Q_OS_WIN
static const int LowerYear = 1980;
#else