📄 qdatetime.cpp
字号:
const QByteArray lctime(setlocale(LC_TIME, "")); if (strftime(buffer, sizeof(buffer), "%B", &tt)) { setlocale(LC_TIME, lctime.data()); return QString::fromLocal8Bit(buffer); } setlocale(LC_TIME, lctime.data());#else SYSTEMTIME st; memset(&st, 0, sizeof(SYSTEMTIME)); st.wYear = 2000; st.wMonth = month; st.wDay = 1; QT_WA({ const wchar_t mmmm_t[] = L"MMMM"; // workaround for Borland TCHAR buf[255]; if (GetDateFormat(GetThreadLocale(), 0, &st, mmmm_t, buf, 255)) return QString::fromUtf16((ushort*)buf); } , { char buf[255]; if (GetDateFormatA(GetThreadLocale(), 0, &st, "MMMM", (char*)&buf, 255)) return QString::fromLocal8Bit(buf); })#endif return QString();}/*! Returns the name of the \a weekday using the following convention: \list \i 1 = "Mon" \i 2 = "Tue" \i 3 = "Wed" \i 4 = "Thu" \i 5 = "Fri" \i 6 = "Sat" \i 7 = "Sun" \endlist The day names will be localized according to the system's locale settings. \sa toString(), shortMonthName(), longMonthName(), longDayName()*/QString QDate::shortDayName(int weekday){ if (weekday < 1 || weekday > 7) { qWarning("QDate::shortDayName: Parameter out of range"); weekday = 1; }#ifndef Q_WS_WIN char buffer[255]; tm tt; memset(&tt, 0, sizeof(tm)); tt.tm_wday = (weekday == 7) ? 0 : weekday; const QByteArray lctime(setlocale(LC_TIME, "")); if (strftime(buffer, sizeof(buffer), "%a", &tt)) { setlocale(LC_TIME, lctime.data()); return QString::fromLocal8Bit(buffer); } setlocale(LC_TIME, lctime.data());#else SYSTEMTIME st; memset(&st, 0, sizeof(SYSTEMTIME)); st.wYear = 2001; st.wMonth = 10; st.wDayOfWeek = (weekday == 7) ? 0 : weekday; st.wDay = 21 + st.wDayOfWeek; QT_WA({ const wchar_t ddd_t[] = L"ddd"; // workaround for Borland TCHAR buf[255]; if (GetDateFormat(GetThreadLocale(), 0, &st, ddd_t, buf, 255)) return QString::fromUtf16((ushort*)buf); } , { char buf[255]; if (GetDateFormatA(GetThreadLocale(), 0, &st, "ddd", (char*)&buf, 255)) return QString::fromLocal8Bit(buf); });#endif return QString();}/*! Returns the long name of the \a weekday using the following convention: \list \i 1 = "Monday" \i 2 = "Tuesday" \i 3 = "Wednesday" \i 4 = "Thursday" \i 5 = "Friday" \i 6 = "Saturday" \i 7 = "Sunday" \endlist The day names will be localized according to the system's locale settings. \sa toString(), shortDayName(), shortMonthName(), longMonthName()*/QString QDate::longDayName(int weekday){ if (weekday < 1 || weekday > 7) { qWarning("QDate::longDayName: Parameter out of range"); weekday = 1; }#ifndef Q_WS_WIN char buffer[255]; tm tt; memset(&tt, 0, sizeof(tm)); tt.tm_wday = (weekday == 7) ? 0 : weekday; const QByteArray lctime(setlocale(LC_TIME, "")); if (strftime(buffer, sizeof(buffer), "%A", &tt)) { setlocale(LC_TIME, lctime.data()); return QString::fromLocal8Bit(buffer); } setlocale(LC_TIME, lctime.data());#else SYSTEMTIME st; memset(&st, 0, sizeof(SYSTEMTIME)); st.wYear = 2001; st.wMonth = 10; st.wDayOfWeek = (weekday == 7) ? 0 : weekday; st.wDay = 21 + st.wDayOfWeek; QT_WA({ const wchar_t dddd_t[] = L"dddd"; // workaround for Borland TCHAR buf[255]; if (GetDateFormat(GetThreadLocale(), 0, &st, dddd_t, buf, 255)) return QString::fromUtf16((ushort*)buf); } , { char buf[255]; if (GetDateFormatA(GetThreadLocale(), 0, &st, "dddd", (char*)&buf, 255)) return QString::fromLocal8Bit(buf); });#endif return QString();}#endif //QT_NO_TEXTDATE#ifndef QT_NO_DATESTRING/*! \fn QString QDate::toString(Qt::DateFormat format) const \overload Returns the date as a string. The \a format parameter determines the format of the string. If the \a format is Qt::TextDate, the string is formatted in the default way. QDate::shortDayName() and QDate::shortMonthName() are used to generate the string, so the day and month names will be localized names. An example of this formatting is "Sat May 20 1995". If the \a format is Qt::ISODate, the string format corresponds to the ISO 8601 extended specification for representations of dates and times, taking the form YYYY-MM-DD, where YYYY is the year, MM is the month of the year (between 01 and 12), and DD is the day of the month between 01 and 31. If the \a format is Qt::LocalDate, the string format depends on the locale settings of the system. On Mac OS X, an assumption is made that the date is in the local time zone. If the datetime is invalid, an empty string will be returned. \sa shortDayName(), shortMonthName()*/QString QDate::toString(Qt::DateFormat f) const{ if (!isValid()) return QString(); int y, m, d; julianToGregorian(jd, y, m, d); switch (f) { case Qt::LocalDate: {#ifdef Q_WS_WIN SYSTEMTIME st; memset(&st, 0, sizeof(SYSTEMTIME)); st.wYear = year(); st.wMonth = month(); st.wDay = day(); QT_WA({ TCHAR buf[255]; if (GetDateFormat(GetThreadLocale(), 0, &st, 0, buf, 255)) return QString::fromUtf16((ushort*)buf); } , { char buf[255]; if (GetDateFormatA(GetThreadLocale(), 0, &st, 0, (char*)&buf, 255)) return QString::fromLocal8Bit(buf); });#elif defined(Q_WS_MAC) CFGregorianDate macGDate; macGDate.year = year(); macGDate.month = month(); macGDate.day = day(); macGDate.hour = 0; macGDate.minute = 0; macGDate.second = 0.0; QCFType<CFTimeZoneRef> myTZ = CFTimeZoneCopyDefault(); QCFType<CFDateRef> myDate = CFDateCreate(0, CFGregorianDateGetAbsoluteTime(macGDate, myTZ));#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3) if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_3) { QCFType<CFLocaleRef> mylocale = CFLocaleCopyCurrent(); QCFType<CFDateFormatterRef> myFormatter = CFDateFormatterCreate(kCFAllocatorDefault, mylocale, kCFDateFormatterLongStyle, kCFDateFormatterNoStyle); return QCFString(CFDateFormatterCreateStringWithDate(0, myFormatter, myDate)); } else#endif { Handle intlHandle = GetIntlResource(1); LongDateTime oldDate; UCConvertCFAbsoluteTimeToLongDateTime(CFGregorianDateGetAbsoluteTime(macGDate, 0), &oldDate); Str255 pString; LongDateString(&oldDate, longDate, pString, intlHandle); return qt_mac_from_pascal_string(pString); }#else tm tt; memset(&tt, 0, sizeof(tm)); char buf[255]; tt.tm_mday = day(); tt.tm_mon = month() - 1; tt.tm_year = year() - 1900; const char *avoidEgcsWarning = "%x"; const QByteArray lctime(setlocale(LC_TIME, "")); if (strftime(buf, sizeof(buf), avoidEgcsWarning, &tt)) { setlocale(LC_TIME, lctime.data()); return QString::fromLocal8Bit(buf); } setlocale(LC_TIME, lctime.data());#endif return QString(); } default:#ifndef QT_NO_TEXTDATE case Qt::TextDate: { return QString::fromLatin1("%0 %1 %2 %3") .arg(shortDayName(dayOfWeek())) .arg(shortMonthName(m)) .arg(d) .arg(y); }#endif case Qt::ISODate: { QString month(QString::number(m).rightJustified(2, QLatin1Char('0'))); QString day(QString::number(d).rightJustified(2, QLatin1Char('0'))); return QString::number(y) + QLatin1Char('-') + month + QLatin1Char('-') + day; } }}/*! Returns the date as a string. The \a format parameter determines the format of the result string. These expressions may be used: \table \header \i Expression \i Output \row \i d \i the day as number without a leading zero (1 to31) \row \i dd \i the day as number with a leading zero (01 to 31) \row \i ddd \i the abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses QDate::shortDayName(). \row \i dddd \i the long localized day name (e.g. 'Qt::Monday' to 'Qt::Sunday'). Uses QDate::longDayName(). \row \i M \i the month as number without a leading zero (1-12) \row \i MM \i the month as number with a leading zero (01-12) \row \i MMM \i the abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses QDate::shortMonthName(). \row \i MMMM \i the long localized month name (e.g. 'January' to 'December'). Uses QDate::longMonthName(). \row \i yy \i the year as two digit number (00 to 99) \row \i yyyy \i the year as four digit number (1752 to 8000) \endtable All other input characters will be ignored. Any sequence of characters that are enclosed in singlequotes will be treated as text and not be used as an expression. Example format strings (assuming that the QDate is the 20 July 1969): \table \header \o Format \o Result \row \o dd.MM.yyyy \o 20.07.1969 \row \o ddd MMMM d yy \o Sun July 20 69 \row \o 'The day is' dddd \o The day is Sunday \endtable If the datetime is invalid, an empty string will be returned. \sa QDateTime::toString() QTime::toString()*/QString QDate::toString(const QString& format) const{ return fmtDateTime(format, 0, this);}#endif //QT_NO_DATESTRING/*! Sets the date's year \a y, month \a m, and day \a d. \a y must be in the range 1752 to 8000, \a m must be in the range 1 to 12, and \a d must be in the range 1 to 31. \warning If \a y is in the range 0 to 99, it is interpreted as 1900 to 1999. Returns true if the date is valid; otherwise returns false.*/bool QDate::setYMD(int y, int m, int d){ if (year() == y && month() == m && day() == d) return isValid(); if (!isValid(y,m,d)) { jd = 0; return false; } jd = gregorianToJulian(y, m, d); return true;}/*! Returns a QDate object containing a date \a ndays later than the date of this object (or earlier if \a ndays is negative). \sa addMonths() addYears() daysTo()*/QDate QDate::addDays(int ndays) const{ QDate d; d.jd = jd + ndays; return d;}/*! Returns a QDate object containing a date \a nmonths later than the date of this object (or earlier if \a nmonths is negative). \sa addDays() addYears()*/QDate QDate::addMonths(int nmonths) const{ int y, m, d; julianToGregorian(jd, y, m, d); while (nmonths != 0) { if (nmonths < 0 && nmonths + 12 <= 0) { y--; nmonths+=12; } else if (nmonths < 0) { m+= nmonths; nmonths = 0; if (m <= 0) { --y; m+=12; } } else if (nmonths - 12 >= 0) { y++; nmonths -= 12; } else if (m == 12) { y++; m = 0; } else { m+= nmonths; nmonths = 0; if (m > 12) { ++y; m -= 12; } } } QDate tmp(y, m, 1); if (d > tmp.daysInMonth()) d = tmp.daysInMonth(); return QDate(y, m, d);}/*! Returns a QDate object containing a date \a nyears later than the date of this object (or earlier if \a nyears is negative). \sa addDays(), addMonths()*/QDate QDate::addYears(int nyears) const{ int y, m, d; julianToGregorian(jd, y, m, d); y += nyears; QDate tmp(y,m,1); if(d > tmp.daysInMonth()) d = tmp.daysInMonth(); QDate date(y, m, d); return date;}/*! Returns the number of days from this date to \a d (which is negative if \a d is earlier than this date). Example: \code QDate d1(1995, 5, 17); // May 17, 1995 QDate d2(1995, 5, 20); // May 20, 1995 d1.daysTo(d2); // returns 3 d2.daysTo(d1); // returns -3 \endcode \sa addDays()*/int QDate::daysTo(const QDate &d) const{ return d.jd - jd;}/*! \fn bool QDate::operator==(const QDate &d) const Returns true if this date is equal to \a d; otherwise returns false.*//*! \fn bool QDate::operator!=(const QDate &d) const Returns true if this date is different from \a d; otherwise returns false.*//*! \fn bool QDate::operator<(const QDate &d) const Returns true if this date is earlier than \a d; otherwise returns
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -