📄 qdatetime.cpp
字号:
// use the reentrant versions of localtime() and gmtime() where available tm res; if ( ts == Qt::LocalTime ) t = localtime_r( <ime, &res ); else t = gmtime_r( <ime, &res );# else if ( ts == Qt::LocalTime ) t = localtime( <ime ); else t = gmtime( <ime );# endif // QT_THREAD_SUPPORT && _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/*! Returns the QDate represented by the string \a s, using the format \a f, or an invalid date if the string cannot be parsed. Note for \c 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 \c Qt::LocalDate cannot be used here.*/QDate QDate::fromString( const QString& s, Qt::DateFormat f ){ if ( ( s.isEmpty() ) || ( f == Qt::LocalDate ) ) {#if defined(QT_CHECK_RANGE) qWarning( "QDate::fromString: Parameter out of range" );#endif 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.find( ' ' ) + 1; int dayPos = s.find( ' ', 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 == 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 defined(QT_CHECK_RANGE) if ( month < 1 || month > 12 ) { qWarning( "QDate::fromString: Parameter out of range" ); QDate d; d.jd = 0; return d; }#endif int day = s.mid( dayPos, 2 ).stripWhiteSpace().toInt(); int year = s.right( 4 ).toInt(); return QDate( year, month, day ); }#else break;#endif } return QDate();}#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 May 17th 2002 is valid QDate::isValid( 2002, 2, 30 ); // FALSE Feb 30th 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..99 is interpreted as 1900..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 && leapYear(y)));}/*! Returns TRUE if the specified year \a y is a leap year; otherwise returns FALSE.*/bool QDate::leapYear( 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++; }}/***************************************************************************** QTime member functions *****************************************************************************//*! \class QTime qdatetime.h \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 the time 0 hours, minutes, seconds and milliseconds, i.e. 00:00:00.000 (midnight). This is a valid time. \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..23, \a m and \a s must be in the range 0..59, and \a ms must be in the range 0..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 equal to 00:00:00.000; otherwise returns FALSE. A null time is valid. \sa isValid()*//*! Returns TRUE if the time is valid; otherwise returns FALSE. The time 23:30:55.746 is valid, whereas 24:12:30 is invalid. \sa isNull()*/bool QTime::isValid() const{ return ds < MSECS_PER_DAY;}/*! Returns the hour part (0..23) of the time.*/int QTime::hour() const{ return ds / MSECS_PER_HOUR;}/*! Returns the minute part (0..59) of the time.*/int QTime::minute() const{ return (ds % MSECS_PER_HOUR)/MSECS_PER_MIN;}/*! Returns the second part (0..59) of the time.*/int QTime::second() const{ return (ds / 1000)%SECS_PER_MIN;}/*! Returns the millisecond part (0..999) of the time.*/int QTime::msec() const{ return ds % 1000;}#ifndef QT_NO_DATESTRING#ifndef QT_NO_SPRINTF/*! \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 \c Qt::TextDate, the string format is HH:MM:SS; e.g. 1 second before midnight would be "23:59:59". If \a f is \c 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 time is an invalid time, then QString::null will be returned.*/QString QTime::toString( Qt::DateFormat f ) const{ if ( !isValid() ) return QString::null; switch ( f ) { case Qt::LocalDate: {#ifndef Q_WS_WIN tm tt; memset( &tt, 0, sizeof( tm ) ); char buf[255]; tt.tm_sec = second(); tt.tm_min = minute(); tt.tm_hour = hour(); if ( strftime( buf, sizeof(buf), "%X", &tt ) ) return QString::fromLocal8Bit( buf );#else 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::fromUcs2( (ushort*)buf ); } , { char buf[255]; if ( GetTimeFormatA( LOCALE_USER_DEFAULT, 0, &st, 0, (char*)&buf, 255 ) ) return QString::fromLocal8Bit( buf ); } );#endif return QString::null; } default: case Qt::ISODate: case Qt::TextDate: QString buf; buf.sprintf( "%.2d:%.2d:%.2d", hour(), minute(), second() ); return buf; }}#endif/*! 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..23 or 1..12 if AM/PM display) \row \i hh \i the hour with a leading zero (00..23 or 01..12 if AM/PM display) \row \i m \i the minute without a leading zero (0..59) \row \i mm \i the minute with a leading zero (00..59) \row \i s \i the second whithout a leading zero (0..59) \row \i ss \i the second whith a leading zero (00..59) \row \i z \i the milliseconds without leading zeroes (0..999) \row \i zzz \i the milliseconds with leading zeroes (000..999) \row \i AP \i use AM/PM display. \e AP will be replaced by either "AM" or "PM". \row \i ap \i use am/pm display. \e ap will be replaced by either "am" or "pm". \endtable All other input characters will be ignored. Example format strings (assuming that the QTime is 14:13:09.042) \table \header \i Format \i Result \row \i hh:mm:ss.zzz \i11 14:13:09.042 \row \i h:m:s ap \i11 2:13:9 pm \endtable If the time is an invalid time, then QString::null 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..23, \a m and \a s must be in the range 0..59, and \a ms must be in the range 0..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) ) {#if defined(QT_CHECK_RANGE) qWarning( "QTime::setHMS Invalid time %02d:%02d:%02d.%03d", h, m, s, ms );#endif ds = MSECS_PER_DAY; // make this invalid return FALSE; } ds = (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 (which is negative if \a t is earlier than this time). 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 ((int)t.ds - (int)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).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -