📄 qdatetime.cpp
字号:
{ if ((s.isEmpty()) || (f == Qt::LocalDate)) return QDate(); switch (f) { case Qt::ISODate: { int year(s.mid(0, 4).toInt()); int month(s.mid(5, 2).toInt()); int day(s.mid(8, 2).toInt()); if (year && month && day) return strictDate(year, month, day); } break; default:#ifndef QT_NO_TEXTDATE case Qt::TextDate: { /* This will fail gracefully if the input string doesn't contain any space. */ int monthPos = s.indexOf(QLatin1Char(' ')) + 1; int dayPos = s.indexOf(QLatin1Char(' '), monthPos) + 1; int yearPos = s.indexOf(QLatin1Char(' '), dayPos) + 1; QString monthName(s.mid(monthPos, dayPos - monthPos - 1)); int month = -1; // try English names first for (int i = 0; i < 12; i++) { if (monthName == QLatin1String(qt_shortMonthNames[i])) { month = i + 1; break; } } // try the localized names if (month == -1) { for (int i = 0; i < 12; i++) { if (monthName == shortMonthName(i + 1)) { month = i + 1; break; } } } if (month >= 1 && month <= 12) { int day = s.mid(dayPos, 2).trimmed().toInt(); int year = s.mid(yearPos).toInt(); return strictDate(year, month, day); } }#else break;#endif } return QDate();}/*! \fn QDate::fromString(const QString &string, const QString &format) Returns the QDate represented by the \a string, using the \a format given, or an invalid date if the string cannot be parsed. These expressions may be used for the format: \table \header \i Expression \i Output \row \i d \i The day as a number without a leading zero (1 to 31) \row \i dd \i The day as a 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. 'Monday' to 'Sunday'). Uses QDate::longDayName(). \row \i M \i The month as a number without a leading zero (1 to 12) \row \i MM \i The month as a number with a leading zero (01 to 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 treated as text. Any sequence of characters that are enclosed in single quotes will also be treated as text and will not be used as an expression. For example: \code QDate date = QDate::fromString("1MM12car2003", "d'MM'MMcaryyyy"); // date is 1 December 2003 \endcode If the format is not satisfied, an invalid QDate is returned. The expressions that don't expect leading zeroes (d, M) will be greedy. This means that they will use two digits even if this will put them outside the accepted range of values and leaves too few digits for other sections. For example, the following format string could have meant January 30 but the M will grab two digits, resulting in an invalid date: \code QDate date = QDate::fromString("130", "Md"); // invalid \endcode For any field that is not represented in the format the following defaults are used: \table \header \i Field \i Default value \row \i Year \i 1900 \row \i Month \i 1 \row \i Day \i 1 \endtable The following examples demonstrate the default values: \code QDate::fromString("1.30", "M.d"); // January 30 1900 QDate::fromString("20000110", "yyyyMMdd"); // January 10, 2000 QDate::fromString("20000110", "yyyyMd"); // January 10, 2000 \endcode \sa QDateTime::fromString(), QTime::fromString(), QDate::toString(), QDateTime::toString(), QTime::toString()*/QDate QDate::fromString(const QString &string, const QString &format){ QDate date;#ifndef QT_BOOTSTRAPPED QDateTimeParser dt(QVariant::Date); if (dt.parseFormat(format)) dt.fromString(string, &date, 0);#else Q_UNUSED(string); Q_UNUSED(format);#endif return date;}#endif // QT_NO_DATESTRING/*! \overload Returns true if the specified date (\a year, \a month, and \a day) is valid; otherwise returns false. Example: \code QDate::isValid(2002, 5, 17); // true QDate::isValid(2002, 2, 30); // false (Feb 30 does not exist) QDate::isValid(2004, 2, 29); // true (2004 is a leap year) QDate::isValid(2000, 2, 29); // true (2000 is a leap year) QDate::isValid(2006, 2, 29); // false (2006 is not a leap year) QDate::isValid(2100, 2, 29); // false (2100 is not a leap year) QDate::isValid(1202, 6, 6); // true (even though 1202 is pre-Gregorian) \endcode \sa isNull(), setDate()*/bool QDate::isValid(int year, int month, int day){ if (year < FIRST_YEAR || (year == FIRST_YEAR && (month < FIRST_MONTH || month == FIRST_MONTH && day < FIRST_DAY)) || year == 0) // there is no year 0 in the Julian calendar return false; // passage from Julian to Gregorian calendar if (year == 1582 && month == 10 && day > 4 && day < 15) return 0; return (day > 0 && month > 0 && month <= 12) && (day <= monthDays[month] || (day == 29 && month == 2 && isLeapYear(year)));}/*! \fn bool QDate::isLeapYear(int year) Returns true if the specified \a year is a leap year; otherwise returns false.*/bool QDate::isLeapYear(int y){ if (y < 1582) { return qAbs(y) % 4 == 0; } else { return y % 4 == 0 && y % 100 != 0 || y % 400 == 0; }}/*! \internal This function has a confusing name and shouldn't be part of the API anyway, since we have toJulian() and fromJulian(). Remove it in Qt 5.*/uint QDate::gregorianToJulian(int y, int m, int d){ return julianDayFromDate(y, m, d);}/*! \internal This function has a confusing name and shouldn't be part of the API anyway, since we have toJulian() and fromJulian(). Remove it in Qt 5.*/void QDate::julianToGregorian(uint jd, int &y, int &m, int &d){ getDateFromJulianDay(jd, &y, &m, &d);}/*! \fn static QDate QDate::fromJulianDay(int jd) Converts the Julian day \a jd to a QDate. \sa toJulianDay()*//*! \fn int QDate::toJulianDay() const Converts the date to a Julian day. \sa fromJulianDay()*//***************************************************************************** QTime member functions *****************************************************************************//*! \class QTime \reentrant \brief The QTime class provides clock time functions. \ingroup time \mainclass A QTime object contains a clock time, i.e. the number of hours, minutes, seconds, and milliseconds since midnight. It can read the current time from the system clock and measure a span of elapsed time. It provides functions for comparing times and for manipulating a time by adding a number of milliseconds. QTime uses the 24-hour clock format; it has no concept of AM/PM. Unlike QDateTime, QTime knows nothing about time zones or daylight savings time (DST). A QTime object is typically created either by giving the number of hours, minutes, seconds, and milliseconds explicitly, or by using the static function currentTime(), which creates a QTime object that contains the system's local time. Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy. The hour(), minute(), second(), and msec() functions provide access to the number of hours, minutes, seconds, and milliseconds of the time. The same information is provided in textual format by the toString() function. QTime provides a full set of operators to compare two QTime objects. One time is considered smaller than another if it is earlier than the other. The time a given number of seconds or milliseconds later than a given time can be found using the addSecs() or addMSecs() functions. Correspondingly, the number of seconds or milliseconds between two times can be found using secsTo() or msecsTo(). QTime can be used to measure a span of elapsed time using the start(), restart(), and elapsed() functions. \sa QDate, QDateTime*//*! \fn QTime::QTime() Constructs a null time object. A null time is identical to a QTime(0, 0, 0, 0) (i.e., midnight), except that isNull() and isValid() return false. \sa isNull(), isValid()*//*! Constructs a time with hour \a h, minute \a m, seconds \a s and milliseconds \a ms. \a h must be in the range 0 to 23, \a m and \a s must be in the range 0 to 59, and \a ms must be in the range 0 to 999. \sa isValid()*/QTime::QTime(int h, int m, int s, int ms){ setHMS(h, m, s, ms);}/*! \fn bool QTime::isNull() const Returns true if the time is null (i.e., the QTime object was constructed using the default constructor); otherwise returns false. A null time is also an invalid time. \sa isValid()*//*! Returns true if the time is valid; otherwise returns false. For example, the time 23:30:55.746 is valid, but 24:12:30 is invalid. \sa isNull()*/bool QTime::isValid() const{ return mds > NullTime && mds < MSECS_PER_DAY;}/*! Returns the hour part (0 to 23) of the time. \sa minute(), second(), msec()*/int QTime::hour() const{ return ds() / MSECS_PER_HOUR;}/*! Returns the minute part (0 to 59) of the time. \sa hour(), second(), msec()*/int QTime::minute() const{ return (ds() % MSECS_PER_HOUR) / MSECS_PER_MIN;}/*! Returns the second part (0 to 59) of the time. \sa hour(), minute(), msec()*/int QTime::second() const{ return (ds() / 1000)%SECS_PER_MIN;}/*! Returns the millisecond part (0 to 999) of the time. \sa hour(), minute(), second()*/int QTime::msec() const{ return ds() % 1000;}#ifndef QT_NO_DATESTRING/*! \overload Returns the time as a string. Milliseconds are not included. The \a f parameter determines the format of the string. If \a f is Qt::TextDate, the string format is HH:MM:SS; e.g. 1 second before midnight would be "23:59:59". If \a f is Qt::ISODate, the string format corresponds to the ISO 8601 extended specification for representations of dates, which is also HH:MM:SS. If \a f is Qt::LocalDate or Qt::SystemLocaleDate, the string format depends on the locale settings of the system. Identical to calling QLocale::system().toString(time, QLocale::ShortFormat). \e{Qt::LocalDate has been deprecated, please use Qt::SystemLocaleDate instead.} If \a f 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(time, QLocale::ShortFormat); If \a f is Qt::LocalDate, the string format depends on the locale settings of the system. If the datetime is invalid, an empty string will be returned.*/QString QTime::toString(Qt::DateFormat f) const{ if (!isValid()) return QString(); switch (f) { case Qt::SystemLocaleDate: return QLocale::system().toString(*this, QLocale::ShortFormat); case Qt::LocaleDate: return QLocale().toString(*this, QLocale::ShortFormat); default: case Qt::ISODate: case Qt::TextDate: return QString::fromLatin1("%1:%2:%3") .arg(hour(), 2, 10, QLatin1Char('0')) .arg(minute(), 2, 10, QLatin1Char('0')) .arg(second(), 2, 10, QLatin1Char('0')); }}/*! Returns the time 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 h \i the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) \row \i hh \i the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) \row \i H \i the hour without a leading zero (0 to 23, even with AM/PM display) \row \i HH \i the hour with a leading zero (00 to 23, even with AM/PM display) \row \i m \i the minute without a leading zero (0 to 59) \row \i mm \i the minute with a leading zero (00 to 59) \row \i s \i the second without a leading zero (0 to 59) \row \i ss \i the second with a leading zero (00 to 59) \row \i z \i the milliseconds without leading zeroes (0 to 999) \row \i zzz \i the milliseconds with leading zeroes (000 to 999) \row \i AP or A \i use AM/PM display. \e AP will be replaced by either "AM" or "PM". \row \i ap or a \i use am/pm display. \e ap will be replaced by either "am" or "pm". \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 QTime is 14:13:09.042) \table
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -