📄 qdatetime.cpp
字号:
return; } }#else if ( ts == Qt::LocalTime ) brokenDown = localtime( &tmp ); if ( !brokenDown ) { brokenDown = gmtime( &tmp ); if ( !brokenDown ) { d.jd = QDate::gregorianToJulian( 1970, 1, 1 ); t.ds = 0; return; } }#endif d.jd = QDate::gregorianToJulian( brokenDown->tm_year + 1900, brokenDown->tm_mon + 1, brokenDown->tm_mday ); t.ds = MSECS_PER_HOUR * brokenDown->tm_hour + MSECS_PER_MIN * brokenDown->tm_min + 1000 * brokenDown->tm_sec;}#ifndef QT_NO_DATESTRING#ifndef QT_NO_SPRINTF/*! \overload Returns the datetime as a string. The \a f parameter determines the format of the string. If \a f is \c Qt::TextDate, the string format is "Wed May 20 03:40:13 1998" (using QDate::shortDayName(), QDate::shortMonthName(), and QTime::toString() to generate the string, so the day and month names will have localized names). If \a f is \c Qt::ISODate, the string format corresponds to the ISO 8601 extended specification for representations of dates and times, which is YYYY-MM-DDTHH:MM:SS. If \a f is \c Qt::LocalDate, the string format depends on the locale settings of the system. If the format \a f is invalid or the datetime is invalid, toString() returns a null string. \sa QDate::toString() QTime::toString()*/QString QDateTime::toString( Qt::DateFormat f ) const{ if ( !isValid() ) return QString::null; if ( f == Qt::ISODate ) { return d.toString( Qt::ISODate ) + "T" + t.toString( Qt::ISODate ); }#ifndef QT_NO_TEXTDATE else if ( f == Qt::TextDate ) {#ifndef Q_WS_WIN QString buf = d.shortDayName( d.dayOfWeek() ); buf += ' '; buf += d.shortMonthName( d.month() ); buf += ' '; buf += QString().setNum( d.day() ); buf += ' ';#else QString buf; QString winstr; QT_WA( { TCHAR out[255]; GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_ILDATE, out, 255 ); winstr = QString::fromUcs2( (ushort*)out ); } , { char out[255]; GetLocaleInfoA( LOCALE_USER_DEFAULT, LOCALE_ILDATE, (char*)&out, 255 ); winstr = QString::fromLocal8Bit( out ); } ); switch ( winstr.toInt() ) { case 1: buf = d.shortDayName( d.dayOfWeek() ) + " " + QString().setNum( d.day() ) + ". " + d.shortMonthName( d.month() ) + " "; break; default: buf = d.shortDayName( d.dayOfWeek() ) + " " + d.shortMonthName( d.month() ) + " " + QString().setNum( d.day() ) + " "; break; }#endif buf += t.toString(); buf += ' '; buf += QString().setNum( d.year() ); return buf; }#endif else if ( f == Qt::LocalDate ) { return d.toString( Qt::LocalDate ) + " " + t.toString( Qt::LocalDate ); } return QString::null;}#endif/*! Returns the datetime as a string. The \a format parameter determines the format of the result string. These expressions may be used for the date: \table \header \i Expression \i Output \row \i d \i the day as number without a leading zero (1-31) \row \i dd \i the day as number with a leading zero (01-31) \row \i ddd \i the abbreviated localized day name (e.g. 'Mon'..'Sun'). Uses QDate::shortDayName(). \row \i dddd \i the long localized day name (e.g. 'Monday'..'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'..'Dec'). Uses QDate::shortMonthName(). \row \i MMMM \i the long localized month name (e.g. 'January'..'December'). Uses QDate::longMonthName(). \row \i yy \i the year as two digit number (00-99) \row \i yyyy \i the year as four digit number (1752-8000) \endtable These expressions may be used for the time: \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 (assumed that the QDateTime is 21<small><sup>st</sup></small> May 2001 14:13:09) \table \header \i Format \i Result \row \i dd.MM.yyyy \i11 21.05.2001 \row \i ddd MMMM d yy \i11 Tue May 21 01 \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 datetime is an invalid datetime, then QString::null will be returned. \sa QDate::toString() QTime::toString()*/QString QDateTime::toString( const QString& format ) const{ return fmtDateTime( format, &t, &d );}#endif //QT_NO_DATESTRING/*! Returns a QDateTime object containing a datetime \a ndays days later than the datetime of this object (or earlier if \a ndays is negative). \sa daysTo(), addMonths(), addYears(), addSecs()*/QDateTime QDateTime::addDays( int ndays ) const{ return QDateTime( d.addDays(ndays), t );}/*! Returns a QDateTime object containing a datetime \a nmonths months later than the datetime of this object (or earlier if \a nmonths is negative). \sa daysTo(), addDays(), addYears(), addSecs()*/QDateTime QDateTime::addMonths( int nmonths ) const{ return QDateTime( d.addMonths(nmonths), t );}/*! Returns a QDateTime object containing a datetime \a nyears years later than the datetime of this object (or earlier if \a nyears is negative). \sa daysTo(), addDays(), addMonths(), addSecs()*/QDateTime QDateTime::addYears( int nyears ) const{ return QDateTime( d.addYears(nyears), t );}/*! Returns a QDateTime object containing a datetime \a nsecs seconds later than the datetime of this object (or earlier if \a nsecs is negative). \sa secsTo(), addDays(), addMonths(), addYears()*/QDateTime QDateTime::addSecs( int nsecs ) const{ uint dd = d.jd; int tt = t.ds; int sign = 1; if ( nsecs < 0 ) { nsecs = -nsecs; sign = -1; } if ( nsecs >= (int)SECS_PER_DAY ) { dd += sign*(nsecs/SECS_PER_DAY); nsecs %= SECS_PER_DAY; } tt += sign*nsecs*1000; if ( tt < 0 ) { tt = MSECS_PER_DAY - tt - 1; dd -= tt / MSECS_PER_DAY; tt = tt % MSECS_PER_DAY; tt = MSECS_PER_DAY - tt - 1; } else if ( tt >= (int)MSECS_PER_DAY ) { dd += ( tt / MSECS_PER_DAY ); tt = tt % MSECS_PER_DAY; } QDateTime ret; ret.t.ds = tt; ret.d.jd = dd; return ret;}/*! Returns the number of days from this datetime to \a dt (which is negative if \a dt is earlier than this datetime). \sa addDays(), secsTo()*/int QDateTime::daysTo( const QDateTime &dt ) const{ return d.daysTo( dt.d );}/*! Returns the number of seconds from this datetime to \a dt (which is negative if \a dt is earlier than this datetime). Example: \code QDateTime dt = QDateTime::currentDateTime(); QDateTime xmas( QDate(dt.date().year(),12,24), QTime(17,00) ); qDebug( "There are %d seconds to Christmas", dt.secsTo(xmas) ); \endcode \sa addSecs(), daysTo(), QTime::secsTo()*/int QDateTime::secsTo( const QDateTime &dt ) const{ return t.secsTo(dt.t) + d.daysTo(dt.d)*SECS_PER_DAY;}/*! Returns TRUE if this datetime is equal to \a dt; otherwise returns FALSE. \sa operator!=()*/bool QDateTime::operator==( const QDateTime &dt ) const{ return t == dt.t && d == dt.d;}/*! Returns TRUE if this datetime is different from \a dt; otherwise returns FALSE. \sa operator==()*/bool QDateTime::operator!=( const QDateTime &dt ) const{ return t != dt.t || d != dt.d;}/*! Returns TRUE if this datetime is earlier than \a dt; otherwise returns FALSE.*/bool QDateTime::operator<( const QDateTime &dt ) const{ if ( d < dt.d ) return TRUE; return d == dt.d ? t < dt.t : FALSE;}/*! Returns TRUE if this datetime is earlier than or equal to \a dt; otherwise returns FALSE.*/bool QDateTime::operator<=( const QDateTime &dt ) const{ if ( d < dt.d ) return TRUE; return d == dt.d ? t <= dt.t : FALSE;}/*! Returns TRUE if this datetime is later than \a dt; otherwise returns FALSE.*/bool QDateTime::operator>( const QDateTime &dt ) const{ if ( d > dt.d ) return TRUE; return d == dt.d ? t > dt.t : FALSE;}/*! Returns TRUE if this datetime is later than or equal to \a dt; otherwise returns FALSE.*/bool QDateTime::operator>=( const QDateTime &dt ) const{ if ( d > dt.d ) return TRUE; return d == dt.d ? t >= dt.t : FALSE;}/*! \overload Returns the current datetime, as reported by the system clock. \sa QDate::currentDate(), QTime::currentTime()*/QDateTime QDateTime::currentDateTime(){ return currentDateTime( Qt::LocalTime );}/*! Returns the current datetime, as reported by the system clock, for the TimeSpec \a ts. The default TimeSpec is LocalTime. \sa QDate::currentDate(), QTime::currentTime(), Qt::TimeSpec*/QDateTime QDateTime::currentDateTime( Qt::TimeSpec ts ){ QDateTime dt; QTime t; dt.setDate( QDate::currentDate(ts) ); if ( QTime::currentTime(&t, ts) ) // midnight or right after? dt.setDate( QDate::currentDate(ts) ); // fetch date again dt.setTime( t ); return dt;}#ifndef QT_NO_DATESTRING/*! Returns the QDateTime represented by the string \a s, using the format \a f, or an invalid datetime if this is not possible. 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 Note that \c Qt::LocalDate cannot be used here.*/QDateTime QDateTime::fromString( const QString& s, Qt::DateFormat f ){ if ( ( s.isEmpty() ) || ( f == Qt::LocalDate ) ) {#if defined(QT_CHECK_RANGE) qWarning( "QDateTime::fromString: Parameter out of range" );#endif QDateTime dt; dt.d.jd = 0; return dt; } if ( f == Qt::ISODate ) { return QDateTime( QDate::fromString( s.mid(0,10), Qt::ISODate ), QTime::fromString( s.mid(11), Qt::ISODate ) ); }#if !defined(QT_NO_REGEXP) && !defined(QT_NO_TEXTDATE) else if ( f == Qt::TextDate ) { QString monthName( s.mid( 4, 3 ) ); int month = -1; // Assume that English monthnames are the default for ( int i = 0; i < 12; ++i ) { if ( monthName == qt_shortMonthNames[i] ) { month = i + 1; break; } } // If English names can't be found, search the localized ones if ( month == -1 ) { for ( int i = 1; i <= 12; ++i ) { if ( monthName == QDate::shortMonthName( i ) ) { month = i; break; } } }#if defined(QT_CHECK_RANGE) if ( month < 1 || month > 12 ) { qWarning( "QDateTime::fromString: Parameter out of range" ); QDateTime dt; dt.d.jd = 0; return dt; }#endif int day = s.mid( 8, 2 ).simplifyWhiteSpace().toInt(); int year = s.right( 4 ).toInt(); QDate date( year, month, day ); QTime time; int hour, minute, second; int pivot = s.find( QRegExp(QString::fromLatin1("[0-9][0-9]:[0-9][0-9]:[0-9][0-9]")) ); if ( pivot != -1 ) { hour = s.mid( pivot, 2 ).toInt(); minute = s.mid( pivot+3, 2 ).toInt(); second = s.mid( pivot+6, 2 ).toInt(); time.setHMS( hour, minute, second ); } return QDateTime( date, time ); }#endif //QT_NO_REGEXP return QDateTime();}#endif //QT_NO_DATESTRING/***************************************************************************** Date/time stream functions *****************************************************************************/#ifndef QT_NO_DATASTREAM/*! \relates QDate Writes the date, \a d, to the data stream, \a s. \sa \link datastreamformat.html Format of the QDataStream operators \endlink*/QDataStream &operator<<( QDataStream &s, const QDate &d ){ return s << (Q_UINT32)(d.jd);}/*! \relates QDate Reads a date from the stream \a s into \a d. \sa \link datastreamformat.html Format of the QDataStream operators \endlink*/QDataStream &operator>>( QDataStream &s, QDate &d ){ Q_UINT32 jd; s >> jd; d.jd = jd; return s;}/*! \relates QTime Writes time \a t to the stream \a s. \sa \link datastreamformat.html Format of the QDataStream operators \endlink*/QDataStream &operator<<( QDataStream &s, const QTime &t ){ return s << (Q_UINT32)(t.ds);}/*! \relates QTime Reads a time from the stream \a s i
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -