Skip to content

Commit 1fcb505

Browse files
committed
добавлены файлы библиотеки
1 parent 4dc43bd commit 1fcb505

File tree

2 files changed

+356
-0
lines changed

2 files changed

+356
-0
lines changed

src/xtime.cpp

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
/*
2+
* xtime_cpp - Library for work with time.
3+
*
4+
* Copyright (c) 2018 Yaroslav Barabanov. Email: elektroyar@yandex.ru
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#include "xtime.hpp"
26+
27+
namespace FunctionsForTime {
28+
29+
unsigned long long getUnixTime(int day, int month, int year, int hour, int minutes, int seconds) {
30+
unsigned long long _secs;
31+
long _mon, _year;
32+
long _days;
33+
_mon = month - 1;
34+
const long _TBIAS_YEAR = 1900;
35+
const long lmos[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
36+
const long mos[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
37+
_year = year - _TBIAS_YEAR;
38+
_days = (((_year - 1) / 4) + ((((_year) & 03) || ((_year) == 0)) ? mos[_mon] : lmos[_mon])) - 1;
39+
_days += 365 * _year;
40+
_days += day;
41+
const long _TBIAS_DAYS = 25567;
42+
_days -= _TBIAS_DAYS;
43+
_secs = 3600 * hour;
44+
_secs += 60 * minutes;
45+
_secs += seconds;
46+
_secs += _days * 86400;
47+
return _secs;
48+
}
49+
50+
cTime::cTime() {};
51+
52+
cTime::cTime(int day, int month, int year) {
53+
cTime::day = day;
54+
cTime::month = month;
55+
cTime::year = year;
56+
cTime::hour = 0;
57+
cTime::minutes = 0;
58+
cTime::seconds = 0;
59+
}
60+
61+
cTime::cTime(int day, int month, int year, int hour, int minutes, int seconds) {
62+
cTime::day = day;
63+
cTime::month = month;
64+
cTime::year = year;
65+
cTime::hour = hour;
66+
cTime::minutes = minutes;
67+
cTime::seconds = seconds;
68+
}
69+
70+
cTime::cTime(unsigned long long timestamp) {
71+
unsigned long long _secs;
72+
long _mon, _year;
73+
long _days;
74+
long i;
75+
76+
_secs = timestamp;
77+
const long _TBIAS_DAYS = 25567;
78+
_days = _TBIAS_DAYS;
79+
80+
_days += _secs / 86400; _secs = _secs % 86400;
81+
hour = _secs / 3600; _secs %= 3600;
82+
minutes = _secs / 60; seconds = _secs % 60;
83+
const long lmos[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
84+
const long mos[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
85+
86+
for (_year = _days / 365; _days < (i = (((_year - 1) / 4) + ((((_year) & 03) || ((_year) == 0)) ? mos[0] : lmos[0]) + 365*_year)); ) { --_year; }
87+
_days -= i;
88+
const long _TBIAS_YEAR = 1900;
89+
year = _year + _TBIAS_YEAR;
90+
91+
if(((_year) & 03) || ((_year) == 0)) {
92+
// mos
93+
for (_mon = 12; _days < mos[--_mon]; );
94+
month = _mon + 1;
95+
day = _days - mos[_mon] + 1;
96+
} else {
97+
for (_mon = 12; _days < lmos[--_mon]; );
98+
month = _mon + 1;
99+
day = _days - lmos[_mon] + 1;
100+
}
101+
}
102+
103+
unsigned long long cTime::getUnixTime() {
104+
unsigned long long _secs;
105+
long _mon, _year;
106+
long _days;
107+
_mon = month - 1;
108+
const long _TBIAS_YEAR = 1900;
109+
const long lmos[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
110+
const long mos[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
111+
_year = year - _TBIAS_YEAR;
112+
_days = (((_year - 1) / 4) + ((((_year) & 03) || ((_year) == 0)) ? mos[_mon] : lmos[_mon])) - 1;
113+
_days += 365 * _year;
114+
_days += day;
115+
const long _TBIAS_DAYS = 25567;
116+
_days -= _TBIAS_DAYS;
117+
_secs = 3600 * hour;
118+
_secs += 60 * minutes;
119+
_secs += seconds;
120+
_secs += _days * 86400;
121+
return _secs;
122+
}
123+
124+
void cTime::setUnixTime(unsigned long long timestamp) {
125+
unsigned long long _secs;
126+
long _mon, _year;
127+
long _days;
128+
long i;
129+
130+
_secs = timestamp;
131+
const long _TBIAS_DAYS = 25567;
132+
_days = _TBIAS_DAYS;
133+
134+
_days += _secs / 86400; _secs = _secs % 86400;
135+
hour = _secs / 3600; _secs %= 3600;
136+
minutes = _secs / 60; seconds = _secs % 60;
137+
const long lmos[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
138+
const long mos[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
139+
140+
for (_year = _days / 365; _days < (i = (((_year - 1) / 4) + ((((_year) & 03) || ((_year) == 0)) ? mos[0] : lmos[0]) + 365*_year)); ) { --_year; }
141+
_days -= i;
142+
const long _TBIAS_YEAR = 1900;
143+
year = _year + _TBIAS_YEAR;
144+
145+
if(((_year) & 03) || ((_year) == 0)) {
146+
// mos
147+
for (_mon = 12; _days < mos[--_mon]; );
148+
month = _mon + 1;
149+
day = _days - mos[_mon] + 1;
150+
} else {
151+
for (_mon = 12; _days < lmos[--_mon]; );
152+
month = _mon + 1;
153+
day = _days - lmos[_mon] + 1;
154+
}
155+
}
156+
157+
unsigned long long getUnixTime(cTime& timedata) {
158+
unsigned long long _secs;
159+
long _mon, _year;
160+
long _days;
161+
_mon = timedata.month - 1;
162+
const long _TBIAS_YEAR = 1900;
163+
const long lmos[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
164+
const long mos[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
165+
_year = timedata.year - _TBIAS_YEAR;
166+
_days = (((_year - 1) / 4) + ((((_year) & 03) || ((_year) == 0)) ? mos[_mon] : lmos[_mon])) - 1;
167+
_days += 365 * _year;
168+
_days += timedata.day;
169+
const long _TBIAS_DAYS = 25567;
170+
_days -= _TBIAS_DAYS;
171+
_secs = 3600 * timedata.hour;
172+
_secs += 60 * timedata.minutes;
173+
_secs += timedata.seconds;
174+
_secs += _days * 86400;
175+
return _secs;
176+
}
177+
178+
cTime getTime(unsigned long long timestamp) {
179+
cTime outTime;
180+
unsigned long long _secs;
181+
long _mon, _year;
182+
long _days;
183+
long i;
184+
185+
_secs = timestamp;
186+
const long _TBIAS_DAYS = 25567;
187+
_days = _TBIAS_DAYS;
188+
189+
_days += _secs / 86400; _secs = _secs % 86400;
190+
outTime.hour = _secs / 3600; _secs %= 3600;
191+
outTime.minutes = _secs / 60; outTime.seconds = _secs % 60;
192+
const long lmos[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
193+
const long mos[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
194+
195+
for (_year = _days / 365; _days < (i = (((_year - 1) / 4) + ((((_year) & 03) || ((_year) == 0)) ? mos[0] : lmos[0]) + 365*_year)); ) { --_year; }
196+
_days -= i;
197+
const long _TBIAS_YEAR = 1900;
198+
outTime.year = _year + _TBIAS_YEAR;
199+
200+
if(((_year) & 03) || ((_year) == 0)) {
201+
// mos
202+
for (_mon = 12; _days < mos[--_mon]; );
203+
outTime.month = _mon + 1;
204+
outTime.day = _days - mos[_mon] + 1;
205+
} else {
206+
for (_mon = 12; _days < lmos[--_mon]; );
207+
outTime.month = _mon + 1;
208+
outTime.day = _days - lmos[_mon] + 1;
209+
}
210+
return outTime;
211+
}
212+
213+
int getWday(int day, int month, int year) {
214+
int a, y, m, R;
215+
a = ( 14 - month ) / 12;
216+
y = year - a;
217+
m = month + 12 * a - 2;
218+
R = 7000 + ( day + y + y / 4 - y / 100 + y / 400 + (31 * m) / 12 );
219+
return R % 7;
220+
}
221+
222+
int getWday(unsigned long long unix) {
223+
cTime temp = getTime(unix);
224+
return getWday(temp.day, temp.month, temp.year);
225+
}
226+
227+
}

src/xtime.hpp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* xtime_cpp - Library for work with time.
3+
*
4+
* Copyright (c) 2018 Yaroslav Barabanov. Email: elektroyar@yandex.ru
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#ifndef XTIME_HPP_INCLUDED
26+
#define XTIME_HPP_INCLUDED
27+
28+
//functions for working with time
29+
namespace FunctionsForTime {
30+
31+
/** \brief Ïîëó÷èòü Unix-âðåìÿ èç äàòû è ñòàíäàðòíîãî âðåìåíè
32+
* \param day äåíü
33+
* \param month ìåñÿö
34+
* \param year ãîä
35+
* \param hour ÷àñ
36+
* \param minutes ìèíóòû
37+
* \param seconds ñåêóíäû
38+
* \return Unix-âðåìÿ
39+
*/
40+
unsigned long long getUnixTime(int day, int month, int year, int hour, int minutes, int seconds);
41+
42+
/** \brief Êëàññ äëÿ õðàíåíèÿ âðåìåíè
43+
*/
44+
class cTime {
45+
public:
46+
char seconds; /**< ñåêóíäû */
47+
char minutes; /**< ìèíóòû */
48+
char hour; /**< ÷àñ */
49+
char day; /**< äåíü */
50+
char month; /**< ìåñÿö */
51+
short year; /**< ãîä */
52+
53+
/** \brief Êîíñòðóêòîð êëàññà áåç íà÷àëüíîé èíèöèàëèçàöèè âðåìåíè
54+
*/
55+
cTime();
56+
57+
/** \brief Èíèöèàëèçàöèÿ ñ óêàçàíèåì âðåìåíè
58+
* Ñåêóíäû, ìèíóòû è ÷àñ áóäóò èíèöèàëèçèðîâàíû íóëåâûì çíà÷åíèåì
59+
* \param day äåíü
60+
* \param month ìåñÿö
61+
* \param year ãîä
62+
*/
63+
cTime(int day, int month, int year);
64+
65+
/** \brief Èíèöèàëèçàöèÿ ñ óêàçàíèåì âðåìåíè è äàòû
66+
* \param day äåíü
67+
* \param month ìåñÿö
68+
* \param year ãîä
69+
* \param hour ÷àñ
70+
* \param minutes ìèíóòû
71+
* \param seconds ñåêóíäû
72+
*/
73+
cTime(int day, int month, int year, int hour, int minutes, int seconds);
74+
75+
/** \brief Èíèöèàëèçàöèÿ ñ óêàçàíèåì unix-âðåìåíè
76+
* \param timestamp unix-âðåìÿ
77+
*/
78+
cTime(unsigned long long timestamp);
79+
80+
/** \brief Ïîëó÷èòü Unix-âðåìÿ
81+
* \return Unix-âðåìÿ
82+
*/
83+
unsigned long long getUnixTime();
84+
85+
/** \brief Óñòàíîâèòü unix-âðåìÿ
86+
* \param timestamp unix-âðåìÿ
87+
*/
88+
void setUnixTime(unsigned long long timestamp);
89+
};
90+
91+
/** \brief Ïîëó÷èòü unix âðåìÿ
92+
* \param timedata ñòàíäàðòíîå âðåìÿ
93+
* \return unix âðåìÿ
94+
*/
95+
unsigned long long getUnixTime(cTime& timedata);
96+
97+
/** \brief Ïîëó÷èòü ñòàíäàðòíîå âðåìÿ
98+
* \param timestamp unix âðåìÿ
99+
* \return ñòàíäàðòíîå âðåìÿ
100+
*/
101+
cTime getTime(unsigned long long timestamp);
102+
103+
enum eWday {
104+
SUN = 0, /**< Âîñêðåñåíüå */
105+
MON, /**< Ïîíåäåëüíèê */
106+
TUS, /**< Âòîðíèê */
107+
WED, /**< Ñðåäà */
108+
THU, /**< ×åòâåðã */
109+
FRI, /**< Ïÿòíèöà */
110+
SAT, /**< Ñóááîòà */
111+
};
112+
113+
/** \brief Ïîëó÷èòü äåíü íåäåëè
114+
* \param day äåíü
115+
* \param month ìåñÿö
116+
* \param year ãîä
117+
* \return äåíü íåäåëè (SUN = 0, MON = 1, ... SAT = 6)
118+
*/
119+
int getWday(int day, int month, int year);
120+
121+
/** \brief Ïîëó÷èòü äåíü íåäåëè
122+
* \param unix Unix-âðåìÿ
123+
* \return äåíü íåäåëè (SUN = 0, MON = 1, ... SAT = 6)
124+
*/
125+
int getWday(unsigned long long unix);
126+
127+
}
128+
129+
#endif // XTIME_HPP_INCLUDED

0 commit comments

Comments
 (0)