-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtools.cpp
More file actions
147 lines (138 loc) · 3.3 KB
/
tools.cpp
File metadata and controls
147 lines (138 loc) · 3.3 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
#include "tools.h"
#include"file_encrypt.h"
#include<algorithm>
#include<sstream>
#include<stdio.h>
#include<ctime>
#include<iomanip>
#include<iostream>
#include<regex>
#include<fstream>
bool fileExists(const std::string& filename)
{
std::ifstream file(filename);
return file.good();
}
void generate_txtfiles(std::string& username, std::string& password)
{
if (!fileExists(username))//如果不存在该用户名下面的二进制文件
{
// 创建txt文件并写入内容
std::ofstream file(username + ".txt");
if (file.is_open()) {
file << "ID" << "\t" << "task_name" << "\t" << "begin_time" << "\t" << "priority";
file << "\t" << "type" << "\t" << "remind_time";
file.close();
printf("文件写入成功\n");
//std::remove(username.c_str());
}
else {
printf("create failure\n");
}
}
else
{
//已经存在
DecryptFile(username, username + ".txt", password);
}
}
void to_lower(std::string& str)
{
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) {
return std::tolower(c);
});
}
time_t to_time_t(const std::string& begin_t)
{
std::tm time_struct = {};
std::istringstream iss(begin_t);
iss >> std::get_time(&time_struct, "%Y/%m/%d/%H:%M:%S");
if (iss.fail()) {
std::cout << "解析日期时间失败!" << std::endl;
return -1;
}
std::time_t timestamp = std::mktime(&time_struct);
return timestamp;
}
bool isLeapYear(int year)
{
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
return true;
else
return false;
}
else
return true;
}
else
return false;
}
bool checkTimeFormat(const std::string& inputTime)
{
std::regex pattern("^\\d{4}/\\d{2}/\\d{2}/\\d{2}:\\d{2}:\\d{2}$");
return std::regex_match(inputTime, pattern);
}
bool isValidDate(const std::string& inputTime)
{
// 验证时间格式是否正确
if (!checkTimeFormat(inputTime))
return false;
// 解析时间字符串
std::stringstream ss(inputTime);
std::string token_year, token_month, token_day, token_hour, token_minute, token_second;
int year, month, day, hour, minute, second;
if (std::getline(ss, token_year, '/') &&
std::getline(ss, token_month, '/') &&
std::getline(ss, token_day, '/') &&
std::getline(ss, token_hour, ':') &&
std::getline(ss, token_minute, ':') &&
std::getline(ss, token_second, ':'))//格式正确为2022/02/02/00:00:00的格式
{
year = stoi(token_year);
month = stoi(token_month);
day = stoi(token_day);
hour = stoi(token_hour);
minute = stoi(token_minute);
second = stoi(token_second);
if (year < 0) return false;
if (month < 1 || month > 12) return false;
if (day < 1) return false;
if (hour < 0 || hour > 23) return false;
if (minute < 0 || minute > 59) return false;
if (second < 0 || second > 59) return false;
if (month == 2)
{
// 如果是闰年,二月可以有29天
if (isLeapYear(year))
{
if (day > 29) return false;
}
else
{
if (day > 28) return false;
}
}
else if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30)
{
return false;
}
else if (day > 31)
{
return false;
}
return true;
}
else
return false;
}
std::string to_string_t(time_t s)
{
struct tm* timeinfo = localtime(&s);
char formatted_time[20];
strftime(formatted_time, sizeof(formatted_time), "%Y/%m/%d/%H:%M:%S", timeinfo);
return formatted_time;
}