📄 qdatetime.cpp
字号:
return QString::fromUcs2( (ushort*)buf ); } , { char buf[255]; if ( GetDateFormatA( LOCALE_USER_DEFAULT, 0, &st, "MMM", (char*)&buf, 255 ) ) return QString::fromLocal8Bit( buf ); } );#endif return QString::null;}/*! Returns the long name of the \a month. 1 = "January", 2 = "February", ... 12 = "December" The month names will be localized according to the system's locale settings. \sa toString(), shortMonthName(), shortDayName(), longDayName()*/QString QDate::longMonthName( int month ){#if defined(QT_CHECK_RANGE) if ( month < 1 || month > 12 ) { qWarning( "QDate::longMonthName: Parameter out ouf range" ); month = 1; }#endif#ifndef Q_WS_WIN char buffer[255]; tm tt; memset( &tt, 0, sizeof( tm ) ); tt.tm_mon = month - 1; if ( strftime( buffer, sizeof( buffer ), "%B", &tt ) ) return QString::fromLocal8Bit( buffer );#else SYSTEMTIME st; memset( &st, 0, sizeof(SYSTEMTIME) ); st.wYear = 2000; st.wMonth = month; st.wDay = 1 ; const wchar_t mmmm_t[] = L"MMMM"; // workaround for Borland QT_WA( { TCHAR buf[255]; if ( GetDateFormat( LOCALE_USER_DEFAULT, 0, &st, mmmm_t, buf, 255 ) ) return QString::fromUcs2( (ushort*)buf ); } , { char buf[255]; if ( GetDateFormatA( LOCALE_USER_DEFAULT, 0, &st, "MMMM", (char*)&buf, 255 ) ) return QString::fromLocal8Bit( buf ); } )#endif return QString::null;}/*! \fn QString QDate::dayName( int weekday ) \obsolete Use shortDayName() instead.*//*! Returns the name of the \a weekday. 1 = "Mon", 2 = "Tue", ... 7 = "Sun" The day names will be localized according to the system's locale settings. \sa toString(), shortMonthName(), longMonthName(), longDayName()*/QString QDate::shortDayName( int weekday ){#if defined(QT_CHECK_RANGE) if ( weekday < 1 || weekday > 7 ) { qWarning( "QDate::shortDayName: Parameter out of range" ); weekday = 1; }#endif#ifndef Q_WS_WIN char buffer[255]; tm tt; memset( &tt, 0, sizeof( tm ) ); tt.tm_wday = ( weekday == 7 ) ? 0 : weekday; if ( strftime( buffer, sizeof( buffer ), "%a", &tt ) ) return QString::fromLocal8Bit( buffer );#else SYSTEMTIME st; memset( &st, 0, sizeof(SYSTEMTIME) ); st.wYear = 2001; st.wMonth = 10; st.wDayOfWeek = ( weekday == 7 ) ? 0 : weekday; st.wDay = 21 + st.wDayOfWeek; const wchar_t ddd_t[] = L"ddd"; // workaround for Borland QT_WA( { TCHAR buf[255]; if ( GetDateFormat( LOCALE_USER_DEFAULT, 0, &st, ddd_t, buf, 255 ) ) return QString::fromUcs2( (ushort*)buf ); } , { char buf[255]; if ( GetDateFormatA( LOCALE_USER_DEFAULT, 0, &st, "ddd", (char*)&buf, 255 ) ) return QString::fromLocal8Bit( buf ); } );#endif return QString::null;}/*! Returns the long name of the \a weekday. 1 = "Monday", 2 = "Tuesday", ... 7 = "Sunday" The day names will be localized according to the system's locale settings. \sa toString(), shortDayName(), shortMonthName(), longMonthName()*/QString QDate::longDayName( int weekday ){#if defined(QT_CHECK_RANGE) if ( weekday < 1 || weekday > 7 ) { qWarning( "QDate::longDayName: Parameter out of range" ); weekday = 1; }#endif#ifndef Q_WS_WIN char buffer[255]; tm tt; memset( &tt, 0, sizeof( tm ) ); tt.tm_wday = ( weekday == 7 ) ? 0 : weekday; if ( strftime( buffer, sizeof( buffer ), "%A", &tt ) ) return QString::fromLocal8Bit( buffer );#else SYSTEMTIME st; memset( &st, 0, sizeof(SYSTEMTIME) ); st.wYear = 2001; st.wMonth = 10; st.wDayOfWeek = ( weekday == 7 ) ? 0 : weekday; st.wDay = 21 + st.wDayOfWeek; const wchar_t dddd_t[] = L"dddd"; // workaround for Borland QT_WA( { TCHAR buf[255]; if ( GetDateFormat( LOCALE_USER_DEFAULT, 0, &st, dddd_t, buf, 255 ) ) return QString::fromUcs2( (ushort*)buf ); } , { char buf[255]; if ( GetDateFormatA( LOCALE_USER_DEFAULT, 0, &st, "dddd", (char*)&buf, 255 ) ) return QString::fromLocal8Bit( buf ); } );#endif return QString::null;}#endif //QT_NO_TEXTDATE#ifndef QT_NO_DATESTRING#if !defined(QT_NO_SPRINTF)/*! \overload Returns the date as a string. The \a f parameter determines the format of the string. If \a f is \c Qt::TextDate, the string format is "Sat May 20 1995" (using the shortDayName() and shortMonthName() functions to generate the string, so the day and month names are locale specific). If \a f is \c Qt::ISODate, the string format corresponds to the ISO 8601 specification for representations of dates, which is YYYY-MM-DD where YYYY is the year, MM is the month of the year (between 01 and 12), and DD is the day of the month between 01 and 31. If \a f is \c Qt::LocalDate, the string format depends on the locale settings of the system. If the date is an invalid date, then QString::null will be returned. \sa shortDayName(), shortMonthName()*/QString QDate::toString( Qt::DateFormat f ) const{ if ( !isValid() ) return QString::null; int y, m, d; julianToGregorian( jd, y, m, d ); switch ( f ) { case Qt::LocalDate: {#ifndef Q_WS_WIN tm tt; memset( &tt, 0, sizeof( tm ) ); char buf[255]; tt.tm_mday = day(); tt.tm_mon = month() - 1; tt.tm_year = year() - 1900; static const char * avoidEgcsWarning = "%x"; if ( strftime( buf, sizeof(buf), avoidEgcsWarning, &tt ) ) return QString::fromLocal8Bit( buf );#else SYSTEMTIME st; memset( &st, 0, sizeof(SYSTEMTIME) ); st.wYear = year(); st.wMonth = month(); st.wDay = day(); QT_WA( { TCHAR buf[255]; if ( GetDateFormat( LOCALE_USER_DEFAULT, 0, &st, 0, buf, 255 ) ) return QString::fromUcs2( (ushort*)buf ); } , { char buf[255]; if ( GetDateFormatA( LOCALE_USER_DEFAULT, 0, &st, 0, (char*)&buf, 255 ) ) return QString::fromLocal8Bit( buf ); } );#endif return QString::null; } default:#ifndef QT_NO_TEXTDATE case Qt::TextDate: { QString buf = shortDayName( dayOfWeek() ); buf += ' '; buf += shortMonthName( m ); QString t; t.sprintf( " %d %d", d, y ); buf += t; return buf; }#endif case Qt::ISODate: { QString month( QString::number( m ).rightJustify( 2, '0' ) ); QString day( QString::number( d ).rightJustify( 2, '0' ) ); return QString::number( y ) + "-" + month + "-" + day; } }}#endif //QT_NO_SPRINTF/*! Returns the date 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 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 All other input characters will be ignored. Example format strings (assuming that the QDate is the 20<sup><small>th</small></sup> July 1969): \table \header \i Format \i Result \row \i dd.MM.yyyy \i11 20.07.1969 \row \i ddd MMMM d yy \i11 Sun July 20 69 \endtable If the date is an invalid date, then QString::null will be returned. \sa QDateTime::toString() QTime::toString()*/QString QDate::toString( const QString& format ) const{ return fmtDateTime( format, 0, this );}#endif //QT_NO_DATESTRING/*! Sets the date's year \a y, month \a m and day \a d. \a y must be in the range 1752..8000, \a m must be in the range 1..12, and \a d must be in the range 1..31. \warning If \a y is in the range 0..99, it is interpreted as 1900..1999. Returns TRUE if the date is valid; otherwise returns FALSE.*/bool QDate::setYMD( int y, int m, int d ){ if ( year() == y && month() == m && day() == d ) return isValid(); if ( !isValid(y,m,d) ) {#if defined(QT_CHECK_RANGE) qWarning( "QDate::setYMD: Invalid date %04d-%02d-%02d", y, m, d );#endif return FALSE; } jd = gregorianToJulian( y, m, d ); return TRUE;}/*! Returns a QDate object containing a date \a ndays later than the date of this object (or earlier if \a ndays is negative). \sa addMonths() addYears() daysTo()*/QDate QDate::addDays( int ndays ) const{ QDate d; d.jd = jd + ndays; return d;}/*! Returns a QDate object containing a date \a nmonths later than the date of this object (or earlier if \a nmonths is negative). \sa addDays() addYears()*/QDate QDate::addMonths( int nmonths ) const{ int y, m, d; julianToGregorian( jd, y, m, d ); while ( nmonths != 0 ) { if ( nmonths < 0 && nmonths + 12 <= 0 ) { y--; nmonths+=12; } else if ( nmonths < 0 ) { m+= nmonths; nmonths = 0; if ( m <= 0 ) { --y; m+=12; } } else if ( nmonths - 12 >= 0 ) { y++; nmonths-=12; } else if ( m == 12 ) { y++; m = 0; } else { m+= nmonths; nmonths = 0; if ( m > 12 ) { ++y; m -= 12; } } } QDate tmp(y,m,1); if( d > tmp.daysInMonth() ) d = tmp.daysInMonth(); QDate date(y, m, d); return date;}/*! Returns a QDate object containing a date \a nyears later than the date of this object (or earlier if \a nyears is negative). \sa addDays(), addMonths()*/QDate QDate::addYears( int nyears ) const{ int y, m, d; julianToGregorian( jd, y, m, d ); y += nyears; QDate tmp(y,m,1); if( d > tmp.daysInMonth() ) d = tmp.daysInMonth(); QDate date(y, m, d); return date;}/*! Returns the number of days from this date to \a d (which is negative if \a d is earlier than this date). Example: \code QDate d1( 1995, 5, 17 ); // May 17th 1995 QDate d2( 1995, 5, 20 ); // May 20th 1995 d1.daysTo( d2 ); // returns 3 d2.daysTo( d1 ); // returns -3 \endcode \sa addDays()*/int QDate::daysTo( const QDate &d ) const{ return d.jd - jd;}/*! \fn bool QDate::operator==( const QDate &d ) const Returns TRUE if this date is equal to \a d; otherwise returns FALSE.*//*! \fn bool QDate::operator!=( const QDate &d ) const Returns TRUE if this date is different from \a d; otherwise returns FALSE.*//*! \fn bool QDate::operator<( const QDate &d ) const Returns TRUE if this date is earlier than \a d, otherwise returns 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(){ return currentDate( Qt::LocalTime );}/*! Returns the current date, as reported by the system clock, for the TimeSpec \a ts. The default TimeSpec is LocalTime. \sa QTime::currentTime(), QDateTime::currentDateTime(), Qt::TimeSpec*/QDate QDate::currentDate( Qt::TimeSpec ts ){ QDate d;#if defined(Q_OS_WIN32) SYSTEMTIME t; memset( &t, 0, sizeof(SYSTEMTIME) ); if ( ts == Qt::LocalTime ) GetLocalTime( &t ); else GetSystemTime( &t ); d.jd = gregorianToJulian( t.wYear, t.wMonth, t.wDay );#else // posix compliant system time_t ltime; time( <ime ); tm *t;# if defined(QT_THREAD_SUPPORT) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -