QT6 源(84):阅读与注释时间类型 QTime,源代码以及属性测试

(1)本 QTime 类型,是支持 <<、 >> 运算符直接往数据流里输出的

在这里插入图片描述

(2)

在这里插入图片描述

++

在这里插入图片描述

(3)本段开始 QTime 与字符串之间的转换

在这里插入图片描述

(4)格式字符

在这里插入图片描述

++

在这里插入图片描述

++

在这里插入图片描述

(5)源码来自于 qdatetime . h

/*
The QTime class provides clock time functions.

QTime 对象包含一个时钟时间,它可以表示为午夜以来的小时、分钟、秒和亳秒数。
它提供了比较时间和通过添加毫秒数来操作时间的函数。
QTime 对象应该通过值而不是通过引用传递给 const;它们只是包装int。

QTime使用24小时制时钟格式;它没有AM/PM的概念。
与QDateTime不同,QTime不知道时区或夏令时(DST)。
通常可以通过显式指定小时、分钟、秒和毫秒数来创建 QTime 对象,
或者使用静态函数currentTime()来创建一个表示系统本地时间的 QTime 对象。

hour()、second()和msec()函数提供对时间小时数、分钟数、秒数minute()和毫秒数的访问。
toString()函数以文本格式提供相同的信息。
addSecs()和addMSecs()函数提供了比给定时间晚给定秒数或毫秒数的时间。
相应地可以使用secsTo()或msecsTo()函数找到两个时间之间的秒数或毫秒数。

QTime提供了一套完整的运算符来比较两个QTime对象;
较早的时间被认为小于较晚的时间;如果A.msecsTo(B)是正数,则A<B。
QTime对象也可以从文本表示形式使用fromString()创建,并使用toString()转换为字符串表示形式。
All conversion to and from string formats is done using the C locale.
对于本地化转换,请参见QLocale。

*/

class Q_CORE_EXPORT QTime
{
private:
    enum TimeFlag { NullTime = -1 };

    constexpr inline int ds() const { return mds == -1 ? 0 : mds; }

    int mds;         //本类的数据成员.应该存储的是从夜里 0点开始的毫秒数

    friend constexpr //友元函数,这是全局函数
    bool operator==(QTime lhs, QTime rhs) { return lhs.mds == rhs.mds; }

    friend constexpr
    bool operator!=(QTime lhs, QTime rhs) { return lhs.mds != rhs.mds; }

    friend constexpr
    bool operator< (QTime lhs, QTime rhs) { return lhs.mds <  rhs.mds; }

    friend constexpr
    bool operator<=(QTime lhs, QTime rhs) { return lhs.mds <= rhs.mds; }

    friend constexpr
    bool operator> (QTime lhs, QTime rhs) { return lhs.mds >  rhs.mds; }

    friend constexpr
    bool operator>=(QTime lhs, QTime rhs) { return lhs.mds >= rhs.mds; }

    friend class QDateTime;

    friend class QDateTimePrivate;

#ifndef QT_NO_DATASTREAM //以下俩函数是存在的,随后的整理将取消这样的条件编译,为了整齐
    friend Q_CORE_EXPORT QDataStream & operator<<(QDataStream &, QTime  );
    friend Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QTime &);
#endif

    explicit constexpr QTime(int ms) : mds(ms) {}

public:
    //Constructs a null time object.
    //For a null time, isNull() returns true and isValid() returns false.
    //If you need a zero time, use QTime(0, 0).
    //For the start of a day, see QDate::startOfDay().
    constexpr   QTime () : mds(NullTime) {}

                QTime (int h, int m, int s = 0, int ms = 0); //计时起点均从 0开始
    //Constructs a time with hour h, minute m, seconds s and milliseconds ms.
    //h must be in the range 0 to 23, m and s must be in the range 0 to 59,
    //and ms must be in the range 0 to 999.

        bool    setHMS(int h, int m, int s    , int ms = 0);
    //Sets the time to hour h, minute m, seconds s and milliseconds ms.
    //Returns true if the set time is valid; otherwise returns false.

    static  QTime  currentTime();
    //返回系统时钟报告的当前时间。
    //请注意,准确性取决于底层操作系统的准确性;并非所有系统都提供1毫秒的准确性。
    //此外,currentTime()仅在一天内增加;每当午夜过去时,它会减少24小时;
    //此外,如果发生夏令时转换,其变化可能不对应于经过的时间。

    //Returns true if the time is null  //总之 null time 也是 invalid time
    //(i.e., the QTime object was constructed using the default constructor);
    //otherwise returns false. A null time is also an invalid time.
    constexpr bool isNull () const { return mds == NullTime; }

              bool isValid() const; //enum QTime::TimeFlag { NullTime = -1 };
    //Returns true if the time is valid; otherwise returns false.
    //For example, the time 23:30:55.746 is valid, but 24:12:30 is invalid.

    static    bool isValid(int h, int m, int s, int ms = 0); //这是个静态成员函数
    //Returns true if the specified time is valid; otherwise returns false.
    //The time is valid if h is in the range 0 to 23,
    //m and s are in the range 0 to 59, and ms is in the range 0 to 999.

    int hour  () const; //Returns the hour         part (0 to  23) of the time.
    int minute() const; //Returns the minute       part (0 to  59) of the time.
    int second() const; //Returns the second       part (0 to  59) of the time.
    int msec  () const; //Returns the millisecond  part (0 to 999) of the time.
                        //Returns -1 if the time is invalid. 这四个函数都出错返 -1

    //返回从当前时间到 t的  秒数。如果t早于当前时间,则返回的秒数是负数。
    //因为 QTime 在一天内测量时间,而一天有 86400秒,所以结果总是在 -86400和 86400 之间。
    //secsTo()不考虑任何毫秒。    Returns 0 if either time is invalid.
    int  secsTo(QTime t) const;

    int msecsTo(QTime t) const;
    //返回从当前时间到 t的毫秒数。如果t早于当前时间,则返回的毫秒数为负数。
    //因为 OTime 在一天内测量时间,而一天有 86400 秒,
    //所以结果总是在 -86400000 到 86400000 毫秒之间。如果时间无效,则返回 0。

    //Returns the number of msecs since the start of the day, i.e. since 00:00:00.
            constexpr inline  int       msecsSinceStartOfDay() const
    {   return mds == NullTime ? 0 : mds; } //返回值是以毫秒为单位

    static  constexpr inline  QTime fromMSecsSinceStartOfDay(int msecs)
    {   return QTime(msecs); }  //意思是构造一个 QTime对象,其时间值距离起点是 形参msecs 毫秒
    //Returns a new QTime instance with the time set to the number of msecs since the
    //start of the day, i.e. since 00:00:00.
    //If msecs falls outside the valid range an invalid QTime will be returned.

    //返回--个 QTime 对象,该对象包含比此对象的时间晚 secs秒的时间(如果 secs为负数,则为更早的时间)
    //请注意,如果时间超过午夜,时间将重新开始。如果此时间无效,则返回 null 时间。
    [[nodiscard]] QTime  addSecs(int secs) const; //这俩函数不会改变原对象的时间值
    [[nodiscard]] QTime addMSecs(int   ms) const;
    //返回一个 QTime 对象,其中包含比此对象的时间晚 ms 毫秒的时间(如果 ms为负数,则为更早的时间)。
    //请注意,如果时间超过午夜,时间将重新计算。请参addSecs()示例。如果此时间无效,则返回 null时间。

    //输出是一样的         "00:38:40"  "00:38:40"   "00:38:40"   "00:38:40.658"
    //enum Qt::DateFormat { TextDate, ISODate, RFC2822Date = 8, ISODateWithMs };
    QString toString(  Qt::DateFormat     f = Qt::TextDate) const;

    QString toString(      QStringView    format) const;

    //Returns the time as a string.
    //The format parameter determines the format of the result string.
    QString toString(const QString      & format) const  //按照自定义格式进行转换为文本
    { return toString(qToStringViewIgnoringNull(format)); }
    //任何包含在单引号中的非空字符序列都会被原样包含在输出字符串中,(去掉引号),即使它包含格式化字符。
    //在输出中,连续的两个单引号("''")会被一个单引号替换。      //格式字符 hH m s zzz AP
    //格式字符串中的所有其他字符都会被原样包含在输出字符串。
    //没有分隔符的格式(例如“ddMM”)是支持的,但必须谨慎使用,因为生成的字符串并不总是可读的
    //(例如,如果“dM”产生“212”,它可能意味着12月2日或2月21日)
    //If the time is invalid, an empty string will be returned.
    //举例:qDebug() << time.toString("时 hh,分 mm,秒 ss,毫秒 zzz");
    //举例:qDebug() << time.toString("时  h,分  m,秒  s,毫秒   z AP");

    //举例:QTime::fromString("+++10---6***36???24 PM" , "+++h---m***s???z AP"  );
    //举例:QTime::fromString("+++10---6***36???024 PM", "+++h---m***s???zzz PM");
    //得到结果分别为  QTime("22:06:36.240")、  QTime("10:06:36.024")
    static QTime fromString(const QString      & string, //按自定义格式读取时间值
                            const QString      & format)
    { return fromString(string, qToStringViewIgnoringNull(format)); }
    //所有其他输入字符将被视为文本。任何包含在单引号中的非空字符序列也将被视为文本(去掉引号),
    //而不是被解释为表达式。                                 //未给出的时间域将按 0值处理。
    //Any field that is not represented in the format will be set to zero.

    static QTime fromString(const QString      & string, //按公用格式读取数据
                              Qt::DateFormat     format = Qt::TextDate)
    { return fromString(qToStringViewIgnoringNull(string), format); }
    //enum Qt::DateFormat { TextDate, ISODate, RFC2822Date = 8, ISODateWithMs };
    //Returns the time represented in the string as a QTime using the format given,
    //or an invalid time if this is not possible.        //使用公用格式

    static QTime fromString(const QString      & string,
                                  QStringView    format);

    static QTime fromString(      QStringView    string,
                              Qt::DateFormat     format = Qt::TextDate);

    static QTime fromString(      QStringView    string,
                                  QStringView    format)
    { return fromString(string.toString(), format); }

}; //完结 class QTime
Q_DECLARE_TYPEINFO(QTime, Q_RELOCATABLE_TYPE);

(6)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangzhangkeji

谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值