📄 qdatetime.cpp
字号:
\i 1 = "Jan" \i 2 = "Feb" \i 3 = "Mar" \i 4 = "Apr" \i 5 = "May" \i 6 = "Jun" \i 7 = "Jul" \i 8 = "Aug" \i 9 = "Sep" \i 10 = "Oct" \i 11 = "Nov" \i 12 = "Dec" \endlist The month names will be localized according to the system's locale settings. \sa toString(), longMonthName(), shortDayName(), longDayName()*/QString QDate::shortMonthName(int month){ if (month < 1 || month > 12) { qWarning("QDate::shortMonthName: Parameter out ouf range"); month = 1; } return QLocale::system().monthName(month, QLocale::ShortFormat);}/*! Returns the long name of the \a month using the following convention: \list \i 1 = "January" \i 2 = "February" \i 3 = "March" \i 4 = "April" \i 5 = "May" \i 6 = "June" \i 7 = "July" \i 8 = "August" \i 9 = "September" \i 10 = "October" \i 11 = "November" \i 12 = "December" \endlist The month names will be localized according to the system's locale settings. \sa toString(), shortMonthName(), shortDayName(), longDayName()*/QString QDate::longMonthName(int month){ if (month < 1 || month > 12) { qWarning("QDate::longMonthName: Parameter out ouf range"); month = 1; } return QLocale::system().monthName(month, QLocale::LongFormat);}/*! 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; } return QLocale::system().dayName(weekday, QLocale::ShortFormat);}/*! 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; } return QLocale::system().dayName(weekday, QLocale::LongFormat);}#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 or Qt::SystemLocaleDate, the string format depends on the locale settings of the system. Identical to calling QLocale::system().toString(date, QLocale::ShortFormat). \e{Qt::LocalDate has been deprecated, please use Qt::SystemLocaleDate instead.} If the \a format is Qt::LocaleDate, the string format depends on the default application locale. This is the locale set with QLocale::setDefault(), or the system locale if no default locale has been set. Identical to calling QLocale().toString(date, QLocale::ShortFormat); If the datetime is invalid, an empty string will be returned. \warning The Qt::ISODate format is only valid for years in the range 0 to 9999. This restriction may apply to Qt::LocalDate as well, depending on the locale settings. \sa shortDayName(), shortMonthName()*/QString QDate::toString(Qt::DateFormat f) const{ if (!isValid()) return QString(); int y, m, d; getDateFromJulianDay(jd, &y, &m, &d); switch (f) { case Qt::SystemLocaleDate: return QLocale::system().toString(*this, QLocale::ShortFormat); case Qt::LocaleDate: return QLocale().toString(*this, QLocale::ShortFormat); 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: { if (year() < 0 || year() > 9999) return QString(); 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 \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. Two consecutive singlequotes ("''") are replaced by a singlequote in the output. 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. \warning This function is only valid for years in the range 0 to 9999. \sa QDateTime::toString() QTime::toString()*/QString QDate::toString(const QString& format) const{ if (uint(year()) > 9999) return QString(); return fmtDateTime(format, 0, this);}#endif //QT_NO_DATESTRING/*! \obsolete Sets the date's year \a y, month \a m, and day \a d. If \a y is in the range 0 to 99, it is interpreted as 1900 to 1999. Use setDate() instead.*/bool QDate::setYMD(int y, int m, int d){ if (uint(y) <= 99) y += 1900; return setDate(y, m, d);}/*! \since 4.2 Sets the date's \a year, \a month, and \a day. Returns true if the date is valid; otherwise returns false. If the specified date is invalid, the QDate object is set to be invalid. Any date before 2 January 4713 B.C. is considered invalid. \sa isValid()*/bool QDate::setDate(int year, int month, int day){ if (!isValid(year, month, day)) { jd = 0; } else { jd = julianDayFromDate(year, month, day); } return jd != 0;}/*! 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; getDateFromJulianDay(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; } } } return fixedDate(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; getDateFromJulianDay(jd, &y, &m, &d); y += nyears; return fixedDate(y, m, d);}/*! 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 false.*//*! \fn bool QDate::operator<=(const QDate &d) const Returns true if this date is earlier than or equal to \a d; otherwise returns false.*//*! \fn bool QDate::operator>(const QDate &d) const Returns true if this date is later than \a d; otherwise returns false.*//*! \fn bool QDate::operator>=(const QDate &d) const Returns true if this date is later than or equal to \a d; otherwise returns false.*//*! \overload Returns the current date, as reported by the system clock. \sa QTime::currentTime(), QDateTime::currentDateTime()*/QDate QDate::currentDate(){ QDate d;#if defined(Q_OS_WIN) SYSTEMTIME st; memset(&st, 0, sizeof(SYSTEMTIME)); GetLocalTime(&st); d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay);#else // posix compliant system time_t ltime; time(<ime); tm *t;#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) // use the reentrant version of localtime() where available tm res; t = localtime_r(<ime, &res);#else t = localtime(<ime);#endif // !QT_NO_THREAD && _POSIX_THREAD_SAFE_FUNCTIONS d.jd = julianDayFromDate(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);#endif return d;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -