📄 qdatetime.cpp
字号:
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_WIN32) SYSTEMTIME st; memset(&st, 0, sizeof(SYSTEMTIME)); GetLocalTime(&st); d.jd = QDate::gregorianToJulian(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 = gregorianToJulian(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);#endif return d;}#ifndef QT_NO_DATESTRING/*! \fn QDate QDate::fromString(const QString &string, Qt::DateFormat format) Returns the QDate represented by the \a string, using the \a format given, or an invalid date if the string cannot be parsed. Note for Qt::TextDate: It is recommended that you use the English short month names (e.g. "Jan"). Although localized month names can also be used, they depend on the user's locale settings. \warning Qt::LocalDate cannot be used here.*/QDate QDate::fromString(const QString& s, Qt::DateFormat f){ if ((s.isEmpty()) || (f == Qt::LocalDate)) { QDate d; d.jd = 0; return d; } 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 QDate(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; 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) { QDate d; d.jd = 0; return d; } int day = s.mid(dayPos, 2).trimmed().toInt(); int year = s.right(4).toInt(); return QDate(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 (1752 to 8000) \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 (year \a y, month \a m, and day \a d) 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(1202, 6, 6); // false (1202 is pre-Gregorian) \endcode \warning A \a y value in the range 00 to 99 is interpreted as 1900 to 1999. \sa isNull(), setYMD()*/bool QDate::isValid(int y, int m, int d){ if (y >= 0 && y <= 99) y += 1900; else if (y < FIRST_YEAR || (y == FIRST_YEAR && (m < 9 || (m == 9 && d < 14)))) return false; return (d > 0 && m > 0 && m <= 12) && (d <= monthDays[m] || (d == 29 && m == 2 && isLeapYear(y)));}/*! \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){ return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;}/*! \internal Converts a Gregorian date to a Julian day. This algorithm is taken from Communications of the ACM, Vol 6, No 8. \sa julianToGregorian()*/uint QDate::gregorianToJulian(int y, int m, int d){ uint c, ya; if (y <= 99) y += 1900; if (m > 2) { m -= 3; } else { m += 9; y--; } c = y; // NOTE: Sym C++ 6.0 bug c /= 100; ya = y - 100*c; return 1721119 + d + (146097*c)/4 + (1461*ya)/4 + (153*m+2)/5;}/*! \internal Converts a Julian day to a Gregorian date. This algorithm is taken from Communications of the ACM, Vol 6, No 8. \sa gregorianToJulian()*/void QDate::julianToGregorian(uint jd, int &y, int &m, int &d){ uint x; uint j = jd - 1721119; y = (j*4 - 1)/146097; j = j*4 - 146097*y - 1; x = j/4; j = (x*4 + 3) / 1461; y = 100*y + j; x = (x*4) + 3 - 1461*j; x = (x + 4)/4; m = (5*x - 3)/153; x = 5*x - 3 - 153*m; d = (x + 5)/5; if (m < 10) { m += 3; } else { m -= 9; y++; }}/*! \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 (milli)seconds. QTime uses the 24-hour clock format; it has no concept of AM/PM. It operates in local time; it knows nothing about time zones or daylight savings time. 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 clock 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 (milli)seconds between two times can be found using the secsTo() or msecsTo() functions. 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. \sa 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; 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()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -