-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate_process.cpp
More file actions
74 lines (60 loc) · 1.9 KB
/
Copy pathdate_process.cpp
File metadata and controls
74 lines (60 loc) · 1.9 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
#include <iostream>
#include <string>
#include <ctime>
#include <regex>
using namespace std;
enum class DateFormat {
RU,
RU_short,
ISO,
ISO_short,
UNKNOWN
};
int get_now_year() {
time_t now = std::time(nullptr);
tm* local_time = std::localtime(&now);
return local_time->tm_year + 1900;
}
void add_year_to_date(string& date, DateFormat format) {
switch (format) {
case DateFormat::RU_short: date += "." + to_string(get_now_year()); break;
case DateFormat::ISO_short: date = to_string(get_now_year()) + '-' + date; break;
default: break;
}
}
DateFormat check_format_date(string& date) {
static const vector<pair<regex, DateFormat>> formats = {
{ std::regex(R"(\d{2}\.\d{2}\.\d{4})"), DateFormat::RU }, // 13.07.2026
{ std::regex(R"(\d{2}\.\d{2})"), DateFormat::RU_short }, // 13.07.
{ std::regex(R"(\d{4}-\d{2}-\d{2})"), DateFormat::ISO }, // 2026-07-13
{ std::regex(R"(\d{2}-\d{2})"), DateFormat::ISO_short} // 07-13
};
for (const auto& [pattern, format_enum] : formats) {
if (std::regex_match(date, pattern)) {
return format_enum;
}
}
return DateFormat::UNKNOWN;
}
void to_iso_translate(string& date) {
date = date.substr(6, 4) + "-" + date.substr(3, 2) + "-" + date.substr(0, 2);
}
void date_processing(string& date) {
auto format_date = check_format_date(date);
switch (format_date) {
case DateFormat::RU:
to_iso_translate(date);
break;
case DateFormat::RU_short:
add_year_to_date(date, format_date);
to_iso_translate(date);
break;
case DateFormat::ISO_short:
add_year_to_date(date, format_date);
break;
case DateFormat::ISO:
break;
default:
cout << "Не верный формат даты" << endl;
}
}