📄 qdatetime.cpp
字号:
/*! 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){ int days = QDate(1970, 1, 1).daysTo(utcDate); int secs = QTime().secsTo(utcTime); if (days < 0 || (days == 0 && secs < 0)) return uint(-1); qlonglong retval = (qlonglong(days) * SECS_PER_DAY) + secs; if (retval >= Q_INT64_C(0xFFFFFFFF)) return uint(-1); return uint(retval);}/*! 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); return ::toTime_t(utcDate, utcTime);}/*! \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 or Qt::SystemLocaleDate, the string format depends on the locale settings of the system. Identical to calling QLocale::system().toString(dateTime, QLocale::ShortFormat). \e{Qt::LocalDate has been deprecated, please use Qt::SystemLocaleDate instead.} If the \a format is Qt::LocaleDate, the string format depends on the default application locale. This is the locale set with QLocale::setDefault(), or the system locale if no default locale has been set. Identical to calling QLocale().toString(dateTime, QLocale::ShortFormat); If the datetime is invalid, an empty string will be returned. \warning The Qt::ISODate format is only valid for years in the range 0 to 9999. This restriction may apply to Qt::LocalDate as well, depending on the locale settings. \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); if (buf.isEmpty()) return QString(); // failed to convert 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::LocaleDate || f == Qt::SystemLocaleDate) { buf = d->date.toString(f); if (buf.isEmpty()) return QString(); // failed to convert buf += QLatin1Char(' '); buf += d->time.toString(f); } 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::longMonthName(). \row \i yy \i the year as two digit number (00-99) \row \i yyyy \i the year as four digit number \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 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 without a leading zero (0 to 59) \row \i ss \i the second with 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 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. Any sequence of characters that are enclosed in singlequotes will be treated as text and not be used as an expression. Example format strings (assumed that the QDateTime is 21 May 2001 14:13:09): \table \header \i Format \i Result \row \i dd.MM.yyyy \i 21.05.2001 \row \i ddd MMMM d yy \i Tue May 21 01 \row \i hh:mm:ss.zzz \i 14:13:09.042 \row \i h:m:s ap \i 2:13:9 pm \endtable If the datetime is invalid, an empty string will be returned. \sa QDate::toString() QTime::toString()*/QString QDateTime::toString(const QString& format) const{ return fmtDateTime(format, &d->time, &d->date);}#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->date.addDays(ndays), d->time, timeSpec());}/*! 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->date.addMonths(nmonths), d->time, timeSpec());}/*! 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->date.addYears(nyears), d->time, timeSpec());}QDateTime QDateTimePrivate::addMSecs(const QDateTime &dt, qint64 msecs){ QDate utcDate; QTime utcTime; dt.d->getUTC(utcDate, utcTime); uint dd = utcDate.jd; int tt = utcTime.ds(); int sign = 1; if (msecs < 0) { msecs = -msecs; sign = -1; } if (msecs >= int(MSECS_PER_DAY)) { dd += sign * (msecs / MSECS_PER_DAY); msecs %= MSECS_PER_DAY; } tt += sign * msecs; 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; } utcDate.jd = dd; utcTime.mds = tt; return QDateTime(utcDate, utcTime, Qt::UTC).toTimeSpec(dt.timeSpec());}/*! Returns a QDateTime object containing a datetime \a nsecs seconds later than the datetime of this object (or earlier if \a nsecs is negative).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -