-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathTimestamp.cc
67 lines (53 loc) · 1.4 KB
/
Timestamp.cc
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
//author voidccc
#include <sys/time.h>
#include <stdio.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#undef __STDC_FORMAT_MACROS
#include "Timestamp.h"
#include <iostream>
Timestamp::Timestamp(double microSeconds)
:_microSecondsSinceEpoch(microSeconds)
{}
Timestamp::~Timestamp()
{}
bool Timestamp::valid()
{
return _microSecondsSinceEpoch > 0;
}
int64_t Timestamp::microSecondsSinceEpoch()
{
return _microSecondsSinceEpoch;
}
string Timestamp::toString() const
{
char buf[32] = {0};
int64_t seconds = _microSecondsSinceEpoch / kMicroSecondsPerSecond;
int64_t microseconds = _microSecondsSinceEpoch % kMicroSecondsPerSecond;
snprintf(buf, sizeof(buf)-1, "%" PRId64 ".%06" PRId64 "", seconds, microseconds);
return buf;
}
Timestamp Timestamp::now()
{
return Timestamp(Timestamp::nowMicroSeconds());
}
Timestamp Timestamp::nowAfter(double seconds)
{
return Timestamp(Timestamp::nowMicroSeconds() + kMicroSecondsPerSecond * seconds);
}
double Timestamp::nowMicroSeconds()
{
struct timeval tv;
gettimeofday(&tv, NULL);
int64_t seconds = tv.tv_sec;
return seconds * kMicroSecondsPerSecond + tv.tv_usec;
}
bool operator<(Timestamp l, Timestamp r)
{
return l.microSecondsSinceEpoch() < r.microSecondsSinceEpoch();
}
bool operator==(Timestamp l, Timestamp r)
{
cout << "operator ==" << endl;
return l.microSecondsSinceEpoch() == r.microSecondsSinceEpoch();
}