-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuration.cpp
More file actions
131 lines (110 loc) · 2.74 KB
/
Copy pathDuration.cpp
File metadata and controls
131 lines (110 loc) · 2.74 KB
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
#include"BaseLib/Duration.h"
namespace BaseLib {
Duration::Duration(int64 nanosecs)
: std::chrono::nanoseconds{nanosecs}
{ }
Duration::Duration(std::chrono::nanoseconds nanosecs)
: std::chrono::nanoseconds(nanosecs)
{ }
Duration::~Duration()
{ }
int64 Duration::InMillis() const
{
return std::chrono::duration_cast<std::chrono::milliseconds>(*this).count();
}
int64 Duration::InMicrosecs() const
{
return std::chrono::duration_cast<std::chrono::microseconds>(*this).count();
}
int64 Duration::InNanosecs() const
{
return this->count();
}
int64 Duration::InSecs() const
{
return std::chrono::duration_cast<std::chrono::seconds>(*this).count();
}
long Duration::InMinutes() const
{
return std::chrono::duration_cast<std::chrono::minutes>(*this).count();
}
long Duration::InHours() const
{
return std::chrono::duration_cast<std::chrono::hours>(*this).count();
}
bool Duration::IsInfinite() const
{
return InMillis() >= std::numeric_limits<long>::max();
}
// -----------------------------------------
// Factory functions
// -----------------------------------------
Duration Duration::Infinite()
{
return Duration(std::numeric_limits<long long>::max());
}
Duration Duration::Zero()
{
return Duration(0);
}
Duration Duration::FromMicroseconds(const int64& micros)
{
if(micros >= LONG_MAX)
{
return Duration(std::chrono::nanoseconds::max());
}
else
{
auto dur = std::chrono::microseconds{micros};
return Duration(std::chrono::duration_cast<std::chrono::nanoseconds>(dur));
}
}
Duration Duration::FromMilliseconds(const int64& millisecs)
{
if(millisecs >= LONG_MAX)
{
return Duration(std::chrono::nanoseconds::max());
}
else
{
auto dur = std::chrono::milliseconds{millisecs};
return Duration(std::chrono::duration_cast<std::chrono::nanoseconds>(dur));
}
}
Duration Duration::FromSeconds(const int64& secs)
{
if(secs >= LONG_MAX)
{
return Duration(std::chrono::nanoseconds::max());
}
else
{
auto dur = std::chrono::seconds{secs};
return Duration(std::chrono::duration_cast<std::chrono::nanoseconds>(dur));
}
}
Duration Duration::FromMinutes(const long& mins)
{
if(mins >= LONG_MAX)
{
return Duration(std::chrono::nanoseconds::max());
}
else
{
auto dur = std::chrono::minutes{mins};
return Duration(std::chrono::duration_cast<std::chrono::nanoseconds>(dur));
}
}
Duration Duration::FromHours(const long& hrs)
{
if(hrs >= LONG_MAX)
{
return Duration(std::chrono::nanoseconds::max());
}
else
{
auto dur = std::chrono::hours{hrs};
return Duration(std::chrono::duration_cast<std::chrono::nanoseconds>(dur));
}
}
}