• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++中如何獲得系統(tǒng)毫秒數(shù)

                    在網(wǎng)上搜索這個(gè)主題的時(shí)候,發(fā)現(xiàn)很多人也在問(wèn)相同的問(wèn)題,后來(lái)自己摸索了一下,給出如下一種可能的解決方案,如果有問(wèn)題,請(qǐng)指出,謝謝: )
              1#ifndef TIME_H
              2#define TIME_H
              3
              4typedef long long int64;
              5
              6#include <sys/timeb.h>
              7
              8NAMESPACE_BEGIN(codekitten)
              9
             10class Time 
             11{
             12    friend ostream& operator <<(ostream& os, const Time& time);
             13public:
             14    Time()
             15    {
             16        timeb t;
             17        ftime(&t);
             18        mMilliseconds = 1000 * t.time + t.millitm;
             19    }

             20
             21    Time(int64 milliseconds) : mMilliseconds(milliseconds){}
             22
             23    Time(const Time& time)
             24    {
             25        if (&time != this)
             26        {
             27            mMilliseconds = time.mMilliseconds;
             28        }

             29    }

             30
             31    ~Time() {}
             32
             33public:
             34    Time& operator =(const Time& time)
             35    {
             36        if(&time != this)
             37        {
             38            mMilliseconds = time.mMilliseconds;
             39        }

             40        return *this;
             41    }

             42
             43    bool operator ==(const Time& time) const
             44    {
             45        return mMilliseconds == time.mMilliseconds;
             46    }

             47    bool operator !=(const Time& time) const
             48    {
             49        return mMilliseconds != time.mMilliseconds;
             50    }

             51
             52    bool operator >(const Time& time) const
             53    {
             54        return mMilliseconds > time.mMilliseconds;
             55    }

             56
             57    bool operator <(const Time& time) const
             58    {
             59        return mMilliseconds < time.mMilliseconds;
             60    }

             61
             62    bool operator >=(const Time& time) const
             63    {
             64        return mMilliseconds >= time.mMilliseconds;
             65    }

             66
             67    bool operator <=(const Time& time) const
             68    {
             69        return mMilliseconds <= time.mMilliseconds;
             70    }

             71
             72    bool before(const Time& time) const
             73    {
             74        return mMilliseconds < time.mMilliseconds;
             75    }

             76
             77    bool after(const Time& time) const
             78    {
             79        return mMilliseconds > time.mMilliseconds;
             80    }

             81
             82    int64 getTime() const
             83    {
             84        return mMilliseconds;
             85    }

             86
             87    void setTime(int64 milliseconds)
             88    {
             89        mMilliseconds = milliseconds;
             90    }

             91
             92public:
             93    static int64 getSystemTime()
             94    {
             95        timeb t;
             96        ftime(&t);
             97        return 1000 * t.time + t.millitm;
             98    }

             99
            100private:
            101    int64 mMilliseconds;
            102}
            ;
            103
            104NAMESPACE_END
            105
            106#endif
                    該類的功能是返回自1970年1月1日以來(lái)的毫秒數(shù),和JAVA中的獲取毫秒數(shù)功能相同。我只在Windows下測(cè)試通過(guò),不知道在Linux下行不行。

            posted on 2008-07-04 21:26 codekitten 閱讀(9427) 評(píng)論(3)  編輯 收藏 引用 所屬分類: C++

            評(píng)論

            # re: C++中如何獲得系統(tǒng)毫秒數(shù) 2008-07-04 23:49 ??

            有一篇文章,或許有幫助
            http://www-128.ibm.com/developerworks/cn/linux/sdk/rt/part1/index.html

            btw:ftime已經(jīng)obsolete, 在linux下的man page, mac os x 也是obsolete.  回復(fù)  更多評(píng)論   

            # re: C++中如何獲得系統(tǒng)毫秒數(shù) 2008-07-06 00:09 foxtail

            很好  回復(fù)  更多評(píng)論   

            # re: C++中如何獲得系統(tǒng)毫秒數(shù) 2008-07-06 20:39 cppexplore

            linux上都是用gettimeofday
            win上還是ftime,一般為了跨平臺(tái),win上會(huì)實(shí)現(xiàn)gettimeofday,如下:
            int inline gettimeofday (struct timeval *tp, void *tz)
            {
            struct _timeb timebuffer;

            _ftime (&timebuffer);
            tp->tv_sec = timebuffer.time;
            tp->tv_usec = timebuffer.millitm * 1000;
            return 0;
            }  回復(fù)  更多評(píng)論   

            導(dǎo)航

            <2008年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆分類(2)

            隨筆檔案(2)

            C++

            積分與排名

            最新評(píng)論

            久久亚洲国产精品成人AV秋霞| 久久精品草草草| 精品久久人人妻人人做精品| 久久综合久久综合九色| 久久精品成人影院| 中文字幕精品久久久久人妻| 性高朝久久久久久久久久| 久久久无码精品亚洲日韩京东传媒| 久久久久国产精品嫩草影院| 久久精品午夜一区二区福利| 伊人久久免费视频| 久久人妻少妇嫩草AV蜜桃| AAA级久久久精品无码片| 久久一区二区免费播放| 久久久久亚洲精品天堂| 国产精品日韩深夜福利久久| 久久免费看黄a级毛片| 伊人久久综在合线亚洲2019 | 久久久受www免费人成| 亚洲欧美日韩中文久久 | 色婷婷综合久久久久中文一区二区 | 亚洲午夜久久久久妓女影院| 99麻豆久久久国产精品免费| 亚洲精品视频久久久| 久久se精品一区精品二区| 久久综合亚洲色一区二区三区| 色偷偷888欧美精品久久久| 久久国产欧美日韩精品| 久久久久国产| 久久精品国产一区二区三区| 无码久久精品国产亚洲Av影片 | 人妻少妇精品久久| 99久久精品国内| 久久精品国产久精国产思思| 久久婷婷人人澡人人爽人人爱| 久久精品二区| 久久精品亚洲精品国产欧美| 99久久免费只有精品国产| 国产精品一久久香蕉国产线看观看 | 色欲久久久天天天综合网精品 | 色老头网站久久网|