-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimestamp_converter.cpp
More file actions
116 lines (100 loc) · 3.5 KB
/
timestamp_converter.cpp
File metadata and controls
116 lines (100 loc) · 3.5 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
#include "timestamp_converter.h"
#include <iomanip>
#include <sstream>
#include <ctime>
#include <QDebug>
#include <regex>
namespace perry {
bool isValidTimestamp(const std::string& input) {
// 正则表达式:匹配1到10位的数字
std::regex timestampPattern(R"(^\d{1,10}$)");
// 如果正则不匹配,返回false
if (!std::regex_match(input, timestampPattern)) {
return false;
}
// 转换为整数并检查是否在uint32_t范围内
uint64_t number = std::stoull(input);
if (number > 4294967295) {
return false;
}
return true;
}
bool isValidTimeStr(const std::string& input) {
// 正则表达式:匹配格式为 YYYY-MM-DD HH:MM:SS
std::regex pattern(R"(^(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2}):(\d{2})$)");
std::smatch match;
// 如果格式不匹配,直接返回 false
if (!std::regex_match(input, match, pattern)) {
qDebug() << "格式不匹配";
return false;
}
// 提取各部分日期和时间
int year = std::stoi(match[1].str());
int month = std::stoi(match[2].str());
int day = std::stoi(match[3].str());
int hour = std::stoi(match[4].str());
int minute = std::stoi(match[5].str());
int second = std::stoi(match[6].str());
// 检查时间的合法性
if (year < 0 || month < 1 || month > 12 || day < 1 || hour < 0 || hour > 23 ||
minute < 0 || minute > 59 || second < 0 || second > 59) {
qDebug() << "年月日超过范围";
return false;
}
// 每个月的天数限制
int daysInMonth[] = {31, (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 29 : 28,
31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 检查天数是否在该月范围内
if (day > daysInMonth[month - 1]) {
qDebug() << "日超过范围";
return false;
}
// 检查1970年
if (year < 1970) {
qDebug() << "年小于1970";
return false;
}
return true;
}
std::string timestamp2LocaltimeStr(std::time_t timestamp)
{
std::tm* tmPtr = std::localtime(×tamp);
std::ostringstream oss;
oss << std::put_time(tmPtr, "%Y-%m-%d %H:%M:%S");
return oss.str();
}
std::string timestamp2UtctimeStr(std::time_t timestamp)
{
// 使用 std::gmtime 将时间戳转换为 UTC 时间
std::tm* tmPtr = std::gmtime(×tamp);
// 使用 stringstream 来格式化时间
std::ostringstream oss;
oss << std::put_time(tmPtr, "%Y-%m-%d %H:%M:%S");
// 返回格式化后的字符串
return oss.str();
}
std::time_t localtime2Timestamp(const std::string& timeString)\
{
std::tm tm = {};
std::istringstream iss(timeString);
iss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
if (iss.fail()) {
throw std::invalid_argument("Invalid time format");
}
return std::mktime(&tm);
}
std::time_t utctime2Timestamp(const std::string& timeString)
{
std::tm tm = {};
std::istringstream iss(timeString);
iss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
if (iss.fail()) {
throw std::invalid_argument("Invalid time format");
}
#if defined(_WIN32)
return _mkgmtime(&tm);
#else
return timegm(&tm);
#endif
}
}