-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtimestamp.h
More file actions
116 lines (87 loc) · 2.51 KB
/
Copy pathtimestamp.h
File metadata and controls
116 lines (87 loc) · 2.51 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
#ifndef __YDX_TIME_STAMP_H__
#define __YDX_TIME_STAMP_H__
#include <stdint.h>
#include <string>
#include <inttypes.h>
#include <boost/operators.hpp>
namespace ydx
{
class Timestamp: public boost::less_than_comparable<Timestamp>
{
public:
Timestamp()
:micro_seconds_since_epoch_(0)
{
}
explicit Timestamp(int64_t micro_second_arg)
:micro_seconds_since_epoch_(micro_second_arg)
{
}
void swap(Timestamp &other)
{
std::swap(micro_seconds_since_epoch_, other.micro_seconds_since_epoch_);
}
std::string string_format(bool show_micro = true) const ;
std::string to_string() const ;
bool valid() const { return micro_seconds_since_epoch_ > 0;}
void set_micro_second(Timestamp &other){micro_seconds_since_epoch_ = other.get_micro_second();}
int64_t get_micro_second() const { return micro_seconds_since_epoch_;}
time_t seconds_since_epoch() const
{
return static_cast<time_t>(micro_seconds_since_epoch_ / micro_seconds_per_sec);
}
static Timestamp now();
static Timestamp invalid()
{
return Timestamp();
}
static Timestamp from_unix_time(time_t t)
{
return from_unix_time(t, 0);
}
static Timestamp from_unix_time(time_t t, int microseconds)
{
return Timestamp(static_cast<int64_t>(t) * micro_seconds_per_sec + microseconds);
}
static const int micro_seconds_per_sec = 1000 * 1000;
private:
int64_t micro_seconds_since_epoch_;
};
inline Timestamp addTime(Timestamp timestamp, double seconds)
{
int64_t delta = static_cast<int64_t>(seconds * Timestamp::micro_seconds_per_sec);
return Timestamp(timestamp.get_micro_second() + delta);
}
inline bool operator<(Timestamp lhs, Timestamp rhs)
{
return lhs.get_micro_second() < rhs.get_micro_second();
}
inline bool operator==(Timestamp lhs, Timestamp rhs)
{
return lhs.get_micro_second() == rhs.get_micro_second();
}
inline double timeDifference(Timestamp high, Timestamp low)
{
int64_t diff = high.get_micro_second() - low.get_micro_second();
return static_cast<double>(diff) / Timestamp::micro_seconds_per_sec;
}
inline int64_t timeDifferenceMicro(Timestamp high, Timestamp low)
{
int64_t diff = high.get_micro_second() - low.get_micro_second();
return diff / 1000;
}
class CTime
{
public:
static CTime GetCurrentTime();
static std::string GetTime();
std::string Format(const char* pszFormat = "%Y-%m-%d %H:%M:%S") const;
std::string Format(char* buf, const char* pszFormat = "%Y-%m-%d %H:%M:%S") const;
struct tm* GetLocalTm(struct tm* ptm) const;
CTime( time_t time);
CTime(struct tm& atm);
private:
time_t time_sec_;
};
}
#endif