📄 qdatetime.cpp
字号:
QTime::isValid(21, 10, 30); // returns true QTime::isValid(22, 5, 62); // returns false \endcode*/bool QTime::isValid(int h, int m, int s, int ms){ return (uint)h < 24 && (uint)m < 60 && (uint)s < 60 && (uint)ms < 1000;}/*! Sets this time to the current time. This is practical for timing: \code QTime t; t.start(); some_lengthy_task(); qDebug("Time elapsed: %d ms", t.elapsed()); \endcode \sa restart(), elapsed(), currentTime()*/void QTime::start(){ *this = currentTime();}/*! Sets this time to the current time and returns the number of milliseconds that have elapsed since the last time start() or restart() was called. This function is guaranteed to be atomic and is thus very handy for repeated measurements. Call start() to start the first measurement, and restart() for each later measurement. Note that the counter wraps to zero 24 hours after the last call to start() or restart(). \warning If the system's clock setting has been changed since the last time start() or restart() was called, the result is undefined. This can happen when daylight savings time is turned on or off. \sa start(), elapsed(), currentTime()*/int QTime::restart(){ QTime t = currentTime(); int n = msecsTo(t); if (n < 0) // passed midnight n += 86400*1000; *this = t; return n;}/*! Returns the number of milliseconds that have elapsed since the last time start() or restart() was called. Note that the counter wraps to zero 24 hours after the last call to start() or restart. Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy. \warning If the system's clock setting has been changed since the last time start() or restart() was called, the result is undefined. This can happen when daylight savings time is turned on or off. \sa start(), restart()*/int QTime::elapsed() const{ int n = msecsTo(currentTime()); if (n < 0) // passed midnight n += 86400 * 1000; return n;}/***************************************************************************** QDateTime member functions *****************************************************************************//*! \class QDateTime \reentrant \brief The QDateTime class provides date and time functions. \ingroup time \mainclass A QDateTime object contains a calendar date and a clock time (a "datetime"). It is a combination of the QDate and QTime classes. It can read the current datetime from the system clock. It provides functions for comparing datetimes and for manipulating a datetime by adding a number of seconds, days, months, or years. A QDateTime object is typically created either by giving a date and time explicitly in the constructor, or by using the static function currentDateTime() that returns a QDateTime object set to the system clock's time. The date and time can be changed with setDate() and setTime(). A datetime can also be set using the setTime_t() function that takes a POSIX-standard "number of seconds since 00:00:00 on January 1, 1970" value. The fromString() function returns a QDateTime, given a string and a date format used to interpret the date within the string. The date() and time() functions provide access to the date and time parts of the datetime. The same information is provided in textual format by the toString() function. QDateTime provides a full set of operators to compare two QDateTime objects where smaller means earlier and larger means later. You can increment (or decrement) a datetime by a given number of seconds using addSecs(), or days using addDays(). Similarly you can use addMonths() and addYears(). The daysTo() function returns the number of days between two datetimes, and secsTo() returns the number of seconds between two datetimes. QDateTime can store datetimes as \l{Qt::LocalTime}{local time} or as \l{Qt::UTC}{UTC}. QDateTime::currentDateTime() returns a QDateTime expressed as local time; use toUTC() to convert it to UTC. You can also use timeSpec() to find out if a QDateTime object stores a UTC time or a local time. Operations such as addSecs() and secsTo() are aware of daylight saving time (DST). \sa QDate QTime QDateTimeEdit*//*! Constructs a null datetime (i.e. null date and null time). A null datetime is invalid, since the date is invalid. \sa isValid()*/QDateTime::QDateTime(){ d = new QDateTimePrivate;}/*! Constructs a datetime with the given \a date, and a valid time (00:00:00.000).*/QDateTime::QDateTime(const QDate &date){ d = new QDateTimePrivate; d->date = date; d->time = QTime(0, 0, 0);}/*! Constructs a datetime with the given \a date and \a time, using the time specification defined by \a spec. If \a date is valid and \a time is not, the time will be set to midnight.*/QDateTime::QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec){ d = new QDateTimePrivate; d->date = date; d->time = date.isValid() && !time.isValid() ? QTime(0, 0, 0) : time; d->spec = (spec == Qt::UTC) ? QDateTimePrivate::UTC : QDateTimePrivate::LocalUnknown;}/*! Constructs a copy of the \a other datetime.*/QDateTime::QDateTime(const QDateTime &other){ d = other.d; d->ref.ref();}/*! Destroys the datetime.*/QDateTime::~QDateTime(){ if (!d->ref.deref()) delete d;}/*! Makes a copy of the \a other datetime and returns a reference to the copy.*/QDateTime &QDateTime::operator=(const QDateTime &other){ qAtomicAssign(d, other.d); return *this;}/*! Returns true if both the date and the time are null; otherwise returns false. A null datetime is invalid. \sa QDate::isNull(), QTime::isNull(), isValid()*/bool QDateTime::isNull() const{ return d->date.isNull() && d->time.isNull();}/*! Returns true if both the date and the time are valid; otherwise returns false. \sa QDate::isValid(), QTime::isValid()*/bool QDateTime::isValid() const{ return d->date.isValid() && d->time.isValid();}/*! Returns the date part of the datetime. \sa setDate(), time(), timeSpec()*/QDate QDateTime::date() const{ return d->date;}/*! Returns the time part of the datetime. \sa setTime(), date(), timeSpec()*/QTime QDateTime::time() const{ return d->time;}/*! Returns the time specification of the datetime. \sa setTimeSpec(), date(), time(), Qt::TimeSpec*/Qt::TimeSpec QDateTime::timeSpec() const{ return d->spec == QDateTimePrivate::UTC ? Qt::UTC : Qt::LocalTime;}/*! Sets the date part of this datetime to \a date. If no time is set, it is set to midnight. \sa date(), setTime(), setTimeSpec()*/void QDateTime::setDate(const QDate &date){ detach(); d->date = date; if (date.isValid() && !d->time.isValid()) d->time = QTime(0, 0, 0);}/*! Sets the time part of this datetime to \a time. \sa time(), setDate(), setTimeSpec()*/void QDateTime::setTime(const QTime &time){ detach(); d->time = time;}/*! Sets the time specification used in this datetime to \a spec. \sa timeSpec(), setDate(), setTime(), Qt::TimeSpec*/void QDateTime::setTimeSpec(Qt::TimeSpec spec){ detach(); d->spec = (spec == Qt::UTC) ? QDateTimePrivate::UTC : QDateTimePrivate::LocalUnknown;}static uint toTime_t(const QDate &utcDate, const QTime &utcTime){ return (QDate(1970, 1, 1).daysTo(utcDate) * SECS_PER_DAY) + QTime().secsTo(utcTime);}/*! Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC). On systems that do not support timezones, this function will behave as if local time were Qt::UTC. \sa setTime_t()*/uint QDateTime::toTime_t() const{ QDate utcDate; QTime utcTime; d->getUTC(utcDate, utcTime); int secsSince1Jan1970UTC = ::toTime_t(utcDate, utcTime); if (secsSince1Jan1970UTC < 0) return (uint)-1; return (uint)secsSince1Jan1970UTC;}/*! \fn void QDateTime::setTime_t(uint seconds) Sets the date and time given the number of \a seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC). On systems that do not support timezones this function will behave as if local time were Qt::UTC. \sa toTime_t()*/void QDateTime::setTime_t(uint secsSince1Jan1970UTC){ detach(); QDateTimePrivate::Spec oldSpec = d->spec; d->date = QDate(1970, 1, 1).addDays(secsSince1Jan1970UTC / SECS_PER_DAY); d->time = QTime().addSecs(secsSince1Jan1970UTC % SECS_PER_DAY); d->spec = QDateTimePrivate::UTC; if (oldSpec != QDateTimePrivate::UTC) d->spec = d->getLocal(d->date, d->time);}#ifndef QT_NO_DATESTRING/*! \fn QString QDateTime::toString(Qt::DateFormat format) const \overload Returns the datetime as a string in the \a format given. If the \a format is Qt::TextDate, the string is formatted in the default way. QDate::shortDayName(), QDate::shortMonthName(), and QTime::toString() are used to generate the string, so the day and month names will be localized names. An example of this formatting is "Wed May 20 03:40:13 1998". If the \a format is Qt::ISODate, the string format corresponds to the ISO 8601 extended specification for representations of dates and times, taking the form YYYY-MM-DDTHH:MM:SS. If the \a format 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. \sa QDate::toString() QTime::toString() Qt::DateFormat*/QString QDateTime::toString(Qt::DateFormat f) const{ QString buf; if (!isValid()) return buf; if (f == Qt::ISODate) { buf = d->date.toString(Qt::ISODate); buf += QLatin1Char('T'); buf += d->time.toString(Qt::ISODate); }#ifndef QT_NO_TEXTDATE else if (f == Qt::TextDate) {#ifndef Q_WS_WIN buf = d->date.shortDayName(d->date.dayOfWeek()); buf += QLatin1Char(' '); buf += d->date.shortMonthName(d->date.month()); buf += QLatin1Char(' '); buf += QString::number(d->date.day());#else QString winstr; QT_WA({ TCHAR out[255]; GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ILDATE, out, 255); winstr = QString::fromUtf16((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->date.shortDayName(d->date.dayOfWeek()); buf += QLatin1Char(' '); buf += QString::number(d->date.day()); buf += QLatin1String(". "); buf += d->date.shortMonthName(d->date.month()); break; default: buf = d->date.shortDayName(d->date.dayOfWeek()); buf += QLatin1Char(' '); buf += d->date.shortMonthName(d->date.month()); buf += QLatin1Char(' '); buf += QString::number(d->date.day()); }#endif buf += QLatin1Char(' '); buf += d->time.toString(); buf += QLatin1Char(' '); buf += QString::number(d->date.year()); }#endif else if (f == Qt::LocalDate) { buf = d->date.toString(Qt::LocalDate); buf += QLatin1Char(' '); buf += d->time.toString(Qt::LocalDate); } return buf;}/*! 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 to 31) \row \i dd \i the day as 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. 'Qt::Monday' to 'Qt::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' to 'Dec'). Uses QDate::shortMonthName(). \row \i MMMM \i the long localized month name (e.g. 'January' to 'December'). Uses QDate::long
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -