📄 qdatetime.cpp
字号:
*/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, 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::LocalDate: {#ifdef Q_WS_WIN SYSTEMTIME st; memset(&st, 0, sizeof(SYSTEMTIME)); st.wHour = hour(); st.wMinute = minute(); st.wSecond = second(); st.wMilliseconds = 0; QT_WA({ TCHAR buf[255]; if (GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, 0, buf, 255)) return QString::fromUtf16((ushort*)buf); } , { char buf[255]; if (GetTimeFormatA(LOCALE_USER_DEFAULT, 0, &st, 0, (char*)&buf, 255)) return QString::fromLocal8Bit(buf); });#elif defined (Q_WS_MAC) CFGregorianDate macGDate; // Assume this is local time and the current date QDate dt = QDate::currentDate(); macGDate.year = dt.year(); macGDate.month = dt.month(); macGDate.day = dt.day(); macGDate.hour = hour(); macGDate.minute = minute(); macGDate.second = second(); 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, kCFDateFormatterNoStyle, kCFDateFormatterMediumStyle); return QCFString(CFDateFormatterCreateStringWithDate(0, myFormatter, myDate)); } else# endif { // For Jaguar, must use the older non-recommended Stuff Handle intlHandle = GetIntlResource(0); LongDateTime oldDate; UCConvertCFAbsoluteTimeToLongDateTime(CFGregorianDateGetAbsoluteTime(macGDate, myTz), &oldDate); Str255 pString; LongTimeString(&oldDate, true, pString, intlHandle); return qt_mac_from_pascal_string(pString); }#else tm tt; memset(&tt, 0, sizeof(tm)); char buf[255]; tt.tm_sec = second(); tt.tm_min = minute(); tt.tm_hour = hour(); const QByteArray lctime(setlocale(LC_TIME, "")); if (strftime(buf, sizeof(buf), "%X", &tt)) { setlocale(LC_TIME, lctime.data()); return QString::fromLocal8Bit(buf); } setlocale(LC_TIME, lctime.data());#endif return QString(); } 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 whithout a leading zero (0 to 59) \row \i ss \i the second whith 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 \header \i Format \i Result \row \i hh:mm:ss.zzz \i 14:13:09.042 \row \i h:m:s ap \i 2:13:9 pm \row \i H:m:s a \i 14:13:9 pm \endtable If the datetime is invalid, an empty string will be returned. \sa QDate::toString() QDateTime::toString()*/QString QTime::toString(const QString& format) const{ return fmtDateTime(format, this, 0);}#endif //QT_NO_DATESTRING/*! Sets the time to 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. Returns true if the set time is valid; otherwise returns false. \sa isValid()*/bool QTime::setHMS(int h, int m, int s, int ms){ if (!isValid(h,m,s,ms)) { mds = NullTime; // make this invalid return false; } mds = (h*SECS_PER_HOUR + m*SECS_PER_MIN + s)*1000 + ms; return true;}/*! Returns a QTime object containing a time \a nsecs seconds later than the time of this object (or earlier if \a nsecs is negative). Note that the time will wrap if it passes midnight. Example: \code QTime n(14, 0, 0); // n == 14:00:00 QTime t; t = n.addSecs(70); // t == 14:01:10 t = n.addSecs(-70); // t == 13:58:50 t = n.addSecs(10 * 60 * 60 + 5); // t == 00:00:05 t = n.addSecs(-15 * 60 * 60); // t == 23:00:00 \endcode \sa addMSecs(), secsTo(), QDateTime::addSecs()*/QTime QTime::addSecs(int nsecs) const{ return addMSecs(nsecs * 1000);}/*! Returns the number of seconds from this time to \a t. If \a t is earlier than this time, the number of seconds returned is negative. Because QTime measures time within a day and there are 86400 seconds in a day, the result is always between -86400 and 86400. \sa addSecs(), QDateTime::secsTo()*/int QTime::secsTo(const QTime &t) const{ return (t.ds() - ds()) / 1000;}/*! Returns a QTime object containing a time \a ms milliseconds later than the time of this object (or earlier if \a ms is negative). Note that the time will wrap if it passes midnight. See addSecs() for an example. \sa addSecs(), msecsTo()*/QTime QTime::addMSecs(int ms) const{ QTime t; if (ms < 0) { // % not well-defined for -ve, but / is. int negdays = (MSECS_PER_DAY-ms) / MSECS_PER_DAY; t.mds = (ds() + ms + negdays*MSECS_PER_DAY) % MSECS_PER_DAY; } else { t.mds = (ds() + ms) % MSECS_PER_DAY; } return t;}/*! Returns the number of milliseconds from this time to \a t. If \a t is earlier than this time, the number of milliseconds returned is negative. Because QTime measures time within a day and there are 86400 seconds in a day, the result is always between -86400000 and 86400000 msec. \sa secsTo(), addMSecs()*/int QTime::msecsTo(const QTime &t) const{ return t.ds() - ds();}/*! \fn bool QTime::operator==(const QTime &t) const Returns true if this time is equal to \a t; otherwise returns false.*//*! \fn bool QTime::operator!=(const QTime &t) const Returns true if this time is different from \a t; otherwise returns false.*//*! \fn bool QTime::operator<(const QTime &t) const Returns true if this time is earlier than \a t; otherwise returns false.*//*! \fn bool QTime::operator<=(const QTime &t) const Returns true if this time is earlier than or equal to \a t; otherwise returns false.*//*! \fn bool QTime::operator>(const QTime &t) const Returns true if this time is later than \a t; otherwise returns false.*//*! \fn bool QTime::operator>=(const QTime &t) const Returns true if this time is later than or equal to \a t; otherwise returns false.*//*! \overload Returns the current time as reported by the system clock. Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.*/QTime QTime::currentTime(){ QTime ct;#if defined(Q_OS_WIN32) SYSTEMTIME st; memset(&st, 0, sizeof(SYSTEMTIME)); GetLocalTime(&st); ct.mds = MSECS_PER_HOUR * st.wHour + MSECS_PER_MIN * st.wMinute + 1000 * st.wSecond + st.wMilliseconds;#elif defined(Q_OS_UNIX) // posix compliant system struct timeval tv; gettimeofday(&tv, 0); time_t ltime = tv.tv_sec; 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 ct.mds = MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + 1000 * t->tm_sec + tv.tv_usec / 1000;#else time_t ltime; // no millisecond resolution ::time(<ime); tm *t; localtime(<ime); ct.mds = MSECS_PER_HOUR * t->tm_hour + MSECS_PER_MIN * t->tm_min + 1000 * t->tm_sec;#endif return ct;}#ifndef QT_NO_DATESTRING/*! \fn QTime QTime::fromString(const QString &string, Qt::DateFormat format) Returns the time represented in the \a string as a QTime using the \a format given, or an invalid time if this is not possible. \warning Note that Qt::LocalDate cannot be used here.*/QTime QTime::fromString(const QString& s, Qt::DateFormat f){ if (s.isEmpty() || f == Qt::LocalDate) { qWarning("QTime::fromString: Parameter out of range"); QTime t; t.mds = NullTime; return t; } int hour(s.mid(0, 2).toInt()); int minute(s.mid(3, 2).toInt()); int second(s.mid(6, 2).toInt()); QString msec_s(QLatin1String("0.") + s.mid(9, 4)); float msec(msec_s.toFloat()); return QTime(hour, minute, second, qRound(msec * 1000.0));}/*! \fn QTime::fromString(const QString &string, const QString &format) Returns the QTime represented by the \a string, using the \a format given, or an invalid time if the string cannot be parsed. These expressions may be used for the format: \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 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 whithout a leading zero (0 to 59) \row \i ss \i the second whith 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 \i interpret as an AM/PM time. \e AP must be either "AM" or "PM". \row \i ap \i Interpret as an AM/PM time. \e ap must be either "am" or "pm". \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 not be used as an expression. \code QTime time = QTime::fromString("1mm12car00", "m'mm'hcarss"); // time is 12:01.00 \endcode If the format is not satisfied an invalid QTime is returned. Expressions that do not expect leading zeroes to be given (h, m, s and z) are greedy. This means that they will use two digits even if this puts them outside the range of accepted values and leaves too few digits for other sections. For example, the following string could have meant 00:07:10, but the m will grab two digits, resulting in an invalid time: \code QTime time = QTime::fromString("00:710", "hh:ms"); // invalid \endcode Any field that is not represented in the format will be set to zero. For example: \code QTime time = QTime::fromString("1.30", "m.s"); // time is 00:01:30.000 \endcode \sa QDateTime::fromString() QDate::fromString() QDate::toString() QDateTime::toString() QTime::toString()*/QTime QTime::fromString(const QString &string, const QString &format){ QTime time;#ifndef QT_BOOTSTRAPPED QDateTimeParser dt(QVariant::Time); if (dt.parseFormat(format)) dt.fromString(string, 0, &time);#else Q_UNUSED(string); Q_UNUSED(format);#endif return time;}#endif // QT_NO_DATESTRING/*! \overload Returns true if the specified time is valid; otherwise returns false. The time is valid if \a h is in the range 0 to 23, \a m and \a s are in the range 0 to 59, and \a ms is in the range 0 to 999. Example: \code
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -