-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.cpp
More file actions
69 lines (59 loc) · 1.9 KB
/
test2.cpp
File metadata and controls
69 lines (59 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
// regex_search example
#include <iostream>
#include <string>
#include <regex>
#include <cstdlib>
#include <stdio.h>
bool find_time(const char *str, time_t &t){
std::smatch m; // smatch also separate result in each ()
std::string s(str);
std::regex e1("(\\d{4})(\\d{2})(\\d{2})");
std::regex e2("(\\d{4})(-|/)(\\d{1,2})(-|/)(\\d{1,2})");
int year, month, day;
struct tm *timeinfo;
if(std::regex_search (s,m,e1)){ // find first one
year = atoi(m.str(1).c_str());
month = atoi(m.str(2).c_str());
day = atoi(m.str(3).c_str());
}
else if(std::regex_search (s,m,e2)){ // find first one
year = atoi(m.str(1).c_str());
month = atoi(m.str(3).c_str());
day = atoi(m.str(5).c_str());
}
else return false;
// check wether year, month, and day are valid
if(month > 12 || month < 1 || day > 31 || day < 1) return false;
else if(month == 2){
if(day > ((year % 4 == 0 && year % 100 !=0) || year % 400 == 0? 29 : 28))
return false;
}
else if(month == 4 || month == 6 || month == 9 || month == 11){
if(day > 30)
return false;
}
time(&t);
timeinfo = localtime(&t);
timeinfo->tm_year = year - 1900;
timeinfo->tm_mon = month - 1;
timeinfo->tm_mday = day;
t = mktime(timeinfo); // timeinfo->tm_wday and tm_yday will be set
return t != -1;
}
int main(){
std::string s;
time_t t;
struct tm *timeinfo;
printf("%c%c%c", 'a', '\0', 'b');
// while (std::getline(std::cin, s)) {
// if(find_time(s.c_str(), t)){
// timeinfo = localtime(&t);
// std::cout << timeinfo->tm_year + 1900 << " "
// << timeinfo->tm_mon + 1 << " "
// << timeinfo->tm_mday << std::endl;
// }
// else
// std::cout << "No valid date." << std::endl;
// }
return 0;
}