-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdate.hpp
More file actions
156 lines (126 loc) · 5.61 KB
/
Copy pathdate.hpp
File metadata and controls
156 lines (126 loc) · 5.61 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
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
//
// Created by caesar kekxv on 2024/6/14.
//
#ifndef CLANGTOOLS_DATE_HPP
#define CLANGTOOLS_DATE_HPP
#include <ctime>
#include <chrono>
#include <istream>
#include <memory>
#include <string>
namespace clangTools {
class date {
// 重命名system_clock名称空间
using system_clk = std::chrono::system_clock;
// 重命名time_point类型
using _time_point = std::chrono::time_point<system_clk>;
private:
_time_point m_time_point;
public:
static std::shared_ptr<date> now();
/**
* 创建时间对象
* @param year 年
* @param monthIndex 月:0-11
* @param day 天
* @param hours 小时
* @param minutes 分钟
* @param seconds 秒
* @param milliseconds 毫秒
* @return 时间
*/
static std::shared_ptr<date> parse(int year, int monthIndex, int day = 1, int hours = 0, int minutes = 0, int seconds = 0, int milliseconds = 0);
/**
* 解析时间
* @param src 时间
* @param format_string 时间格式
* @return 是否解析成功
*/
static std::shared_ptr<date> parse(const std::string &src, const char *format_string = "yyyy-MM-dd HH:mm:ss");
/**
* 解析时间戳,只支持秒
* @param time_ 时间戳
* @return 是否解析成功
*/
static std::shared_ptr<date> parse(const std::time_t &time_);
/**
* 解析时间戳,只支持毫秒
* @param time_ 时间戳
* @return 是否解析成功
*/
static std::shared_ptr<date> parseMicroseconds(const std::time_t &time_);
~date();
explicit date();
/**
* 重置时间点
*/
void reset();
/**
* 时间
* @param format_string 解析格式
* @return 格式化的时间
*/
std::string format(const char *format_string = "yyyy-MM-dd HH:mm:ss") const;
/**
* 转字符串
* @param format_string 格式
* @return 字符串
*/
std::string to_string(const char *format_string = "yyyy-MM-dd HH:mm:ss") const;
/**
* 克隆一个对象
* @param format_string 格式,支持 "yyyy+1-MM-1-dd HH:mm:ss" 如果是 "yyyy-01-01 HH:mm:ss",需要改为 "yyyy--01--01 HH:mm:ss",否则解析错误
* @return 新的对象
*/
std::shared_ptr<date> clone(const char *format_string = "yyyy-MM-dd HH:mm:ss") const;
public:
date &operator=(const date &date_) = default;
date &operator=(const std::shared_ptr<date> &date_) {
m_time_point = date_->m_time_point;
return (*this);
}
friend std::ostream &operator<<(std::ostream &output, const date &date_) {
output << date_.to_string();
return output;
}
friend std::ostream &operator<<(std::ostream &output, const std::shared_ptr<date> &date_) {
output << date_->to_string();
return output;
}
friend std::istream &operator>>(std::istream &input, date &date_) {
std::string str;
input >> str;
date_ = date::parse(str);
return input;
}
friend bool operator>(const date &d1, const date &d2) { return d1.m_time_point > d2.m_time_point; }
friend bool operator>(const std::shared_ptr<date> &d1, const std::shared_ptr<date> &d2) { return *d1 > *d2; }
friend bool operator>(const date &d1, const std::shared_ptr<date> &d2) { return d1 > *d2; }
friend bool operator>(const std::shared_ptr<date> &d1, const date &d2) { return *d1 > d2; }
friend bool operator>=(const date &d1, const date &d2) { return d1.m_time_point >= d2.m_time_point; }
friend bool operator>=(const std::shared_ptr<date> &d1, const std::shared_ptr<date> &d2) { return *d1 >= *d2; }
friend bool operator>=(const date &d1, const std::shared_ptr<date> &d2) { return d1 >= *d2; }
friend bool operator>=(const std::shared_ptr<date> &d1, const date &d2) { return *d1 >= d2; }
friend bool operator<(const date &d1, const date &d2) { return d1.m_time_point < d2.m_time_point; }
friend bool operator<(const std::shared_ptr<date> &d1, const std::shared_ptr<date> &d2) { return *d1 < *d2; }
friend bool operator<(const date &d1, const std::shared_ptr<date> &d2) { return d1 < *d2; }
friend bool operator<(const std::shared_ptr<date> &d1, const date &d2) { return *d1 < d2; }
friend bool operator<=(const date &d1, const date &d2) { return d1.m_time_point <= d2.m_time_point; }
friend bool operator<=(const std::shared_ptr<date> &d1, const std::shared_ptr<date> &d2) { return *d1 <= *d2; }
friend bool operator<=(const date &d1, const std::shared_ptr<date> &d2) { return d1 <= *d2; }
friend bool operator<=(const std::shared_ptr<date> &d1, const date &d2) { return *d1 <= d2; }
friend bool operator==(const date &d1, const date &d2) { return d1.m_time_point == d2.m_time_point; }
friend bool operator==(const std::shared_ptr<date> &d1, const std::shared_ptr<date> &d2) { return *d1 == *d2; }
friend bool operator==(const date &d1, const std::shared_ptr<date> &d2) { return d1 == *d2; }
friend bool operator==(const std::shared_ptr<date> &d1, const date &d2) { return *d1 == d2; }
friend bool operator!=(const date &d1, const date &d2) { return d1.m_time_point != d2.m_time_point; }
friend bool operator!=(const std::shared_ptr<date> &d1, const std::shared_ptr<date> &d2) { return *d1 != *d2; }
friend bool operator!=(const date &d1, const std::shared_ptr<date> &d2) { return d1 != *d2; }
friend bool operator!=(const std::shared_ptr<date> &d1, const date &d2) { return *d1 != d2; }
private:
static int calc_max_day(int year, int mon);
static void calc_tm(struct tm *tm_);
static std::string replace_format(const char *format_string = "yyyy-MM-dd HH:mm:ss");
};
}
#endif //CLANGTOOLS_DATE_HPP