summaryrefslogtreecommitdiff
path: root/cpp/src/IceUtil/Time.cpp
blob: d808796a6d7614b2c69c283df92b375e31194b3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

#include <IceUtil/Time.h>

#ifdef _WIN32
#   include <sys/timeb.h>
#else
#   include <sys/time.h>
#endif

using namespace IceUtil;

Time::Time() :
    _usec(0)
{
}

IceUtil::Time::Time(const timeval& tv) :
    _usec((tv.tv_sec * (LongLong)1000000) + tv.tv_usec)
{
}

Time
IceUtil::Time::now()
{
#ifdef WIN32
    struct _timeb timebuffer;
    _ftime(&timebuffer);

    return Time(timebuffer.time * (LongLong)1000000) +
               (timebuffer.millitm * (LongLong)1000);
#else
    struct timeval tv;
    gettimeofday(&tv, 0);

    return Time(tv);
#endif
}

Time
IceUtil::Time::seconds(long t)
{
    return Time(t * (LongLong)1000000);
}

Time
IceUtil::Time::milliSeconds(long t)
{
    return Time(t * (LongLong)1000);
}

#ifdef _WIN32
Time
IceUtil::Time::microSeconds(__int64 t)
{
    return Time(t);
}
#else
Time
IceUtil::Time::microSeconds(long long t)
{
    return Time(t);
}
#endif

Time
IceUtil::Time::operator-() const
{
    return Time(-_usec);
}

Time
IceUtil::Time::operator-(const Time& other) const
{
    return Time(_usec - other._usec);
}

Time
IceUtil::Time::operator+(const Time& other) const
{
    return Time(_usec + other._usec);
}

Time&
IceUtil::Time::operator+=(const Time& other)
{
    _usec += other._usec;
    return *this;
}

Time&
IceUtil::Time::operator-=(const Time& other)
{
    _usec -= other._usec;
    return *this;
}

bool
IceUtil::Time::operator<(const Time& other) const
{
    return _usec < other._usec;
}

bool
IceUtil::Time::operator<=(const Time& other) const
{
    return _usec <= other._usec;
}

bool
IceUtil::Time::operator>(const Time& other) const
{
    return _usec > other._usec;
}

bool
IceUtil::Time::operator>=(const Time& other) const
{
    return _usec >= other._usec;
}

bool
IceUtil::Time::operator==(const Time& other) const
{
    return _usec == other._usec;
}

bool
IceUtil::Time::operator!=(const Time& other) const
{
    return _usec != other._usec;
}

IceUtil::Time::operator timeval() const
{
    timeval tv;
    tv.tv_sec = _usec / 1000000;
    tv.tv_usec = _usec % 1000000;
    return tv;
}

//
// Private constructor.
//
Time::Time(LongLong usec) :
    _usec(usec)
{
}