-
Notifications
You must be signed in to change notification settings - Fork 0
/
ah-date.H
122 lines (99 loc) · 2.58 KB
/
ah-date.H
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
/* Aleph-w
/ \ | | ___ _ __ | |__ __ __
/ _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
/ ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
/_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
|_|
This file is part of Aleph-w library
Copyright (c) 2002-2018 Leandro Rabindranath Leon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
# ifndef AH_DATE_H
# define AH_DATE_H
# include <chrono>
# include <ctime>
# include <string.h>
# include <sstream>
using namespace std;
namespace Aleph
{
inline bool is_leap_year(const size_t & yy)
{
if ((yy % 400) == 0)
return true;
if ((yy % 100) == 0)
return false;
return (yy % 4) != 0;
}
inline bool valid_month(size_t mm) { return mm <= 12; }
inline bool valid_day(const size_t yy, const size_t mm, const size_t dd)
{
switch (mm)
{
case 1:
case 3:
case 5:
case 7 ... 8:
case 10:
case 12:
return dd <= 31;
case 2:
if (is_leap_year(yy))
return dd <= 29;
else
return dd <= 28;
case 4:
case 6:
case 9:
case 11:
return dd <= 30;
default:
return false;
}
}
inline time_t
to_time_t(const size_t & dd, const size_t & mm, const size_t & yy)
{
ostringstream s;
s << yy << "-" << mm << "-" << dd;
struct tm tm;
memset(&tm, 0, sizeof(tm));
strptime(s.str().c_str(), "%Y-%m-%d", &tm);
return mktime(&tm);
}
inline time_t to_time_t(const string & s, const string & format)
{
struct tm tm;
memset(&tm, 0, sizeof(tm));
strptime(s.c_str(), format.c_str(), &tm);
return mktime(&tm);
}
inline time_t to_time_t(const string & s)
{
struct tm tm;
memset(&tm, 0, sizeof(tm));
strptime(s.c_str(), "%Y-%m-%d %H:%M:%S", &tm);
return mktime(&tm);
}
inline size_t to_days(const time_t & t)
{
return t / (24*60*60);
}
inline string to_string(const time_t & t, const string & format)
{
static constexpr size_t n = 100;
char buff[n];
strftime(buff, n, format.c_str(), localtime(&t));
return buff;
}
}
# endif