C++中如何获得系统毫秒数

        在网上搜索这个主题的时候,发现很多人也在问相同的问题,后来自己摸索了一下,给出如下一种可能的解决方案,如果有问题,请指出,谢谢: )
  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日以来的毫秒数,和JAVA中的获取毫秒数功能相同。我只在Windows下测试通过,不知道在Linux下行不行。

posted on 2008-07-04 21:26 codekitten 阅读(9294) 评论(3)  编辑 收藏 引用 所属分类: C++

评论

# re: C++中如何获得系统毫秒数 2008-07-04 23:49 ??

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

btw:ftime已经obsolete, 在linux下的man page, mac os x 也是obsolete.  回复  更多评论   

# re: C++中如何获得系统毫秒数 2008-07-06 00:09 foxtail

很好  回复  更多评论   

# re: C++中如何获得系统毫秒数 2008-07-06 20:39 cppexplore

linux上都是用gettimeofday
win上还是ftime,一般为了跨平台,win上会实现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;
}  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

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

统计

常用链接

留言簿(1)

随笔分类(2)

随笔档案(2)

C++

积分与排名

最新评论