首页 > 其他分享 >QDateTime

QDateTime

时间:2023-06-22 19:55:53浏览次数:35  
标签:const Qt qint64 日期 other QDateTime

QDateTime 

#include <QDateTime>

 

Public Functions

  QDateTime()
  QDateTime(const QDate &date)
  QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec = Qt::LocalTime)
  QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec, int offsetSeconds)
  QDateTime(const QDate &date, const QTime &time, const QTimeZone &timeZone)
  QDateTime(const QDateTime &other)
  QDateTime(QDateTime &&other)
  ~QDateTime()
QDateTime addDays(qint64 ndays) const
QDateTime addMSecs(qint64 msecs) const
QDateTime addMonths(int nmonths) const
QDateTime addSecs(qint64 s) const
QDateTime addYears(int nyears) const
QDate date() const
qint64 daysTo(const QDateTime &other) const
bool isDaylightTime() const
bool isNull() const
bool isValid() const
qint64 msecsTo(const QDateTime &other) const
int offsetFromUtc() const
qint64 secsTo(const QDateTime &other) const
void setDate(const QDate &date)
void setMSecsSinceEpoch(qint64 msecs)
void setOffsetFromUtc(int offsetSeconds)
void setSecsSinceEpoch(qint64 secs)
void setTime(const QTime &time)
void setTimeSpec(Qt::TimeSpec spec)
void setTimeZone(const QTimeZone &toZone)
void swap(QDateTime &other)
QTime time() const
Qt::TimeSpec timeSpec() const
QTimeZone timeZone() const
QString timeZoneAbbreviation() const
CFDateRef toCFDate() const
QDateTime toLocalTime() const
qint64 toMSecsSinceEpoch() const
NSDate * toNSDate() const
QDateTime toOffsetFromUtc(int offsetSeconds) const
qint64 toSecsSinceEpoch() const
QString toString(const QString &format) const
QString toString(Qt::DateFormat format = Qt::TextDate) const
QString toString(QStringView format) const
QDateTime toTimeSpec(Qt::TimeSpec spec) const
QDateTime toTimeZone(const QTimeZone &timeZone) const
QDateTime toUTC() const
bool operator!=(const QDateTime &other) const
bool operator<(const QDateTime &other) const
bool operator<=(const QDateTime &other) const
QDateTime & operator=(QDateTime &&other)
QDateTime & operator=(const QDateTime &other)
bool operator==(const QDateTime &other) const
bool operator>(const QDateTime &other) const
bool operator>=(const QDateTime &other) const

Static Public Members

QDateTime currentDateTime()
QDateTime currentDateTimeUtc()
qint64 currentMSecsSinceEpoch()
qint64 currentSecsSinceEpoch()
QDateTime fromCFDate(CFDateRef date)
QDateTime fromMSecsSinceEpoch(qint64 msecs)
QDateTime fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetSeconds = 0)
QDateTime fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone)
QDateTime fromNSDate(const NSDate *date)
QDateTime fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec = Qt::LocalTime, int offsetSeconds = 0)
QDateTime fromSecsSinceEpoch(qint64 secs, const QTimeZone &timeZone)
QDateTime fromString(const QString &string, Qt::DateFormat format = Qt::TextDate)
QDateTime fromString(const QString &string, const QString &format)

QDataStream & operator<<(QDataStream &out, const QDateTime &dateTime)
QDataStream & operator>>(QDataStream &in, QDateTime &dateTime)

 

 

 

详细说明

QDateTime类提供日期和时间函数。

 

QDateTime对象包含日历日期和时钟时间(“日期时间”)。它是QDate和QTime类的组合。它可以从系统时钟中读取当前的日期时间。它提供了比较日期时间以及通过添加秒、天、月或年数来操作日期时间的功能。

 

QDateTime对象通常是通过在构造函数中显式给定日期和时间来创建的,或者通过使用静态函数currentDateTime()来创建,该函数返回设置为系统时钟时间的QDateTime。日期和时间可以使用setDate()和setTime()更改。还可以使用setTime_t()函数设置日期时间,该函数采用POSIX标准的“自1970年1月1日00:00:00以来的秒数”值。fromString()函数返回一个QDateTime,给定一个字符串和用于解释字符串中日期的日期格式。

 

date()和time()函数提供了对datetime的日期和时间部分的访问。toString()函数以文本格式提供了相同的信息。

 

QDateTime提供了一整套运算符来比较两个QDateTime对象,其中较小表示较早,较大表示较晚。

 

您可以使用addMSecs()将日期时间增加(或减少)给定的毫秒数,使用addSecs()增加秒数,或使用addDays()减少天数。类似地,您可以使用addMonths()和addYears()。daysTo()函数返回两个日期时间之间的天数,secsTo(。

 

QDateTime可以将日期时间存储为本地时间或UTC。QDateTime::currentDateTime()返回以本地时间表示的QDateTime;使用toUTC()将其转换为UTC。您还可以使用timeSpec()来确定QDateTime对象存储的是UTC时间还是本地时间。addSecs()和secsTo()等操作知道夏令时(DST)。

 

注意:QDateTime不包括闰秒。

 

备注

没有年份0

没有年份0。当年的日期被视为无效。1年是“基督之前的1年”或“当前时代之前的1”。公元前1月1日的前一天是公元前12月31日。

 

有效日期范围

能够存储在QDateTime中的有效值的范围取决于内部存储实现。QDateTime当前存储在qint64中,作为编码日期和时间的串行毫秒值。这将日期范围限制在+/-2.92亿年左右,而QDate的日期范围为+/-20亿年。创建具有极值的QDateTime时必须小心,以免使存储溢出。支持的值的确切范围因Qt::TimeSpec和时区而异。

 

系统时区的使用

QDateTime使用系统的时区信息来确定本地时间与UTC的偏移量。如果系统配置不正确或不是最新的,QDateTime也会给出错误的结果。

 

夏令时(夏令时)

QDateTime在处理夏令时时会考虑系统的时区信息。在现代Unix系统上,这意味着它尽可能应用正确的历史DST数据。在Windows上,如果系统不支持历史DST数据,则不会维护DST的历史准确性。

 

考虑夏令时的有效日期范围为1970-01-01年至今,在2037-12-31年之前,正确处理夏令时的规则已经到位,但这些规则可能会改变。对于超出该范围的日期,QDateTime使用1970年或2037年的规则进行了最佳猜测,但我们无法保证准确性。这意味着QDateTime不会考虑1970年之前区域设置时区的变化,即使系统的时区数据库支持这些信息。

 

QDateTime考虑了从标准时间到夏令时的转换。例如,如果转换在凌晨2点,时钟前进到凌晨3点,那么从02:00:00到02:59:59.999有一个“缺失”小时,QDateTime认为该小时无效。任何日期的数学运算都会将这一缺失的小时考虑在内,并返回有效的结果。

 

与UTC的偏移

还支持从UTC偏移的Qt::TimeSpec。这允许您定义一个相对于UTC的QDateTime,该时间距UTC的固定偏移量为给定秒数。例如,+3600秒的偏移量比UTC提前一小时,通常用ISO标准表示法写成“UTC+01:00”。夏令时从不适用于此TimeSpec。

 

偏移秒没有明确的大小限制,但使用toString()和fromString()方法时有一个隐含的限制,它们使用[+|-]hh:mm的格式,有效地将范围限制为+/-99小时59分钟和整分钟。请注意,目前没有任何时区超出+/-14小时的范围。

 

时区支持

Qt::TimeZone的Qt:∶TimeSpec也支持与QTimeZone类结合使用。

 

 

 

date:

ExpressionOutput
d the day as number without a leading zero (1 to 31)
dd the day as number with a leading zero (01 to 31)
ddd the abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses the system locale to localize the name, i.e. QLocale::system().
dddd the long localized day name (e.g. 'Monday' to 'Sunday'). Uses the system locale to localize the name, i.e. QLocale::system().
M the month as number without a leading zero (1-12)
MM the month as number with a leading zero (01-12)
MMM the abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses the system locale to localize the name, i.e. QLocale::system().
MMMM the long localized month name (e.g. 'January' to 'December'). Uses the system locale to localize the name, i.e. QLocale::system().
yy the year as two digit number (00-99)
yyyy the year as four digit number

These expressions may be used for the time:

ExpressionOutput
h the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hh the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
H the hour without a leading zero (0 to 23, even with AM/PM display)
HH the hour with a leading zero (00 to 23, even with AM/PM display)
m the minute without a leading zero (0 to 59)
mm the minute with a leading zero (00 to 59)
s the whole second without a leading zero (0 to 59)
ss the whole second with a leading zero where applicable (00 to 59)
z the fractional part of the second, to go after a decimal point, without trailing zeroes (0 to 999). Thus "s.z" reports the seconds to full available (millisecond) precision without trailing zeroes.
zzz the fractional part of the second, to millisecond precision, including trailing zeroes where applicable (000 to 999).
AP or A use AM/PM display. A/AP will be replaced by either "AM" or "PM".
ap or a use am/pm display. a/ap will be replaced by either "am" or "pm".
t the timezone (for example "CEST")

All other input characters will be ignored. Any sequence of characters that are enclosed in single quotes will be treated as text and not be used as an expression. Two consecutive single quotes ("''") are replaced by a singlequote in the output. Formats without separators (e.g. "HHmm") are currently not supported.

Example format strings (assumed that the QDateTime is 21 May 2001 14:13:09.120):

FormatResult
dd.MM.yyyy 21.05.2001
ddd MMMM d yy Tue May 21 01
hh:mm:ss.zzz 14:13:09.120
hh:mm:ss.z 14:13:09.12
h:m:s ap 2:13:9 pm

If the datetime is invalid, an empty string will be returned.

 

 

##############################

标签:const,Qt,qint64,日期,other,QDateTime
From: https://www.cnblogs.com/herd/p/17498217.html

相关文章

  • QDateTime计算时间差
    QDateTime计算时间差fromPyQt5.QtCoreimportQDateTime#CreatetwoQDateTimeobjectsdt1=QDateTime.currentDateTime()dt2=QDateTime(2022,1,1,0,0)#Calculatethedifferenceinsecondsdiff_seconds=dt1.secsTo(dt2)print(f'Thedifferenceinse......
  • QDateTimeEdit的使用
    QDateTimeEdit及QDateTime的使用QStringstrYear=ui->dateTimeEdit->sectionText(QDateTimeEdit::YearSection);QStringstrMonth=ui->dateTimeEdit->secti......
  • QDateTime
    QDateTimetime=QDateTime::currentDateTime();//获取当前时间QStringstr=time.toString("yyyy-MM-dd");//设置显示格式inttimeT=time.toTime_t();//将当前时......
  • 【PyQt6】Python中QDateTime的常用方法总结
    前置QtDesigner里拖了一个QDateTimeEdit,设置时间格式为“yyyy/MM/ddHH:mm:ss”常用函数dt1=object1.dateTime()#object为QDateTimeEdit对象dt2=object2.dateTi......