Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit fde5a28

Browse files
committed
upd
1 parent 174f090 commit fde5a28

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![Foo](https://img.shields.io/badge/Version-1.0-brightgreen.svg?style=flat-square)](#versions)
1+
[![Foo](https://img.shields.io/badge/Version-1.1-brightgreen.svg?style=flat-square)](#versions)
22
[![Foo](https://img.shields.io/badge/Website-AlexGyver.ru-blue.svg?style=flat-square)](https://alexgyver.ru/)
33
[![Foo](https://img.shields.io/badge/%E2%82%BD$%E2%82%AC%20%D0%9D%D0%B0%20%D0%BF%D0%B8%D0%B2%D0%BE-%D1%81%20%D1%80%D1%8B%D0%B1%D0%BA%D0%BE%D0%B9-orange.svg?style=flat-square)](https://alexgyver.ru/support_alex/)
44

@@ -100,6 +100,7 @@ void loop() {
100100
<a id="versions"></a>
101101
## Версии
102102
- v1.0
103+
- v1.1 - оптимизация
103104
104105
<a id="feedback"></a>
105106
## Баги и обратная связь

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=UnixTime
2-
version=1.0
2+
version=1.1
33
author=AlexGyver <[email protected]>
44
maintainer=AlexGyver <[email protected]>
55
sentence=Unix time stamp to date time conversion and vice versa

src/UnixTime.h

+36-20
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,85 @@
55
66
основано на https://www.oryx-embedded.com/doc/date__time_8c_source.html
77
и https://stackoverflow.com/questions/7960318/math-to-convert-seconds-since-1970-into-date-and-vice-versa
8+
и http://howardhinnant.github.io/date_algorithms.html#civil_from_days
89
910
AlexGyver, [email protected]
1011
https://alexgyver.ru/
1112
MIT License
1213
1314
Версии:
1415
v1.0 - релиз
16+
v1.1 - оптимизация
1517
*/
1618

17-
#ifndef UnixTime_h
18-
#define UnixTime_h
19+
#ifndef _UnixTime_h
20+
#define _UnixTime_h
1921
class UnixTime {
2022
public:
2123
// указать GMT
2224
UnixTime (int8_t gmt) {
2325
_gmt = gmt;
2426
}
25-
27+
2628
// получить unix stamp из установленной даты и времени
2729
uint32_t getUnix() {
28-
int mdays[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
29-
int yearB = year;
30-
yearB -= 1970;
31-
int minusYear = 0;
32-
if (month >= 3) {
33-
yearB++;
34-
minusYear = 1;
35-
}
36-
return ( ( (day - 1 + mdays[month - 1] + ( ( yearB + 1 ) >> 2 ) - ( ( yearB + 69 ) / 100 ) + ( ( yearB + 369 ) / 100 / 4 ) + 365 * ( yearB - minusYear )) * 24ul + hour - _gmt) * 60ul + minute ) * 60ul + second;
30+
int8_t my = (month >= 3) ? 1 : 0;
31+
uint16_t y = year + my - 1970;
32+
uint16_t dm = 0;
33+
for (int i = 0; i < month - 1; i++) dm += (i<7)?((i==1)?28:((i&1)?30:31)):((i&1)?31:30);
34+
return (((day-1+dm+((y+1)>>2)-((y+69)/100)+((y+369)/100/4)+365*(y-my))*24ul+hour-_gmt)*60ul+minute)*60ul+second;
3735
}
3836

3937
// конвертировать unix stamp дату и время (в переменные члены класса)
4038
void getDateTime(uint32_t t) {
39+
// http://howardhinnant.github.io/date_algorithms.html#civil_from_days
40+
t += _gmt * 3600ul;
41+
second = t % 60ul;
42+
t /= 60ul;
43+
minute = t % 60ul;
44+
t /= 60ul;
45+
hour = t % 24ul;
46+
t /= 24ul;
47+
dayOfWeek = (t + 4) % 7;
48+
if (!dayOfWeek) dayOfWeek = 7;
49+
uint32_t z = t + 719468;
50+
uint8_t era = z / 146097ul;
51+
uint16_t doe = z - era * 146097ul;
52+
uint16_t yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
53+
uint16_t y = yoe + era * 400;
54+
uint16_t doy = doe - (yoe * 365 + yoe / 4 - yoe / 100);
55+
uint16_t mp = (doy * 5 + 2) / 153;
56+
day = doy - (mp * 153 + 2) / 5 + 1;
57+
month = mp + (mp < 10 ? 3 : -9);
58+
y += (month <= 2);
59+
year = y;
60+
/*
61+
//https://www.oryx-embedded.com/doc/date__time_8c_source.html
4162
uint32_t a, b, c, d, e, f;
4263
int h, j, k;
43-
44-
t += _gmt * 3600;
64+
t += _gmt * 3600ul;
4565
second = t % 60ul;
4666
t /= 60;
4767
minute = t % 60ul;
4868
t /= 60;
4969
hour = t % 24ul;
5070
t /= 24;
51-
5271
a = (uint32_t)((4ul * t + 102032) / 146097 + 15);
5372
b = (uint32_t)(t + 2442113 + a - (a / 4));
5473
c = (20 * b - 2442) / 7305;
5574
d = b - 365 * c - (c / 4);
5675
e = d * 1000 / 30601;
5776
f = d - e * 30 - e * 601 / 1000;
58-
59-
// январь и февраль как 13 и 14 месяцы
6077
if (e <= 13) {
6178
c -= 4716;
6279
e -= 1;
6380
} else {
6481
c -= 4715;
6582
e -= 13;
6683
}
67-
6884
year = c;
6985
month = e;
7086
day = f;
71-
7287
if (e <= 2) {
7388
e += 12;
7489
c -= 1;
@@ -77,6 +92,7 @@ class UnixTime {
7792
k = c % 100;
7893
h = f + (26 * (e + 1) / 10) + k + (k / 4) + (5 * j) + (j / 4); // Уравнение Зеллера
7994
dayOfWeek = ((h + 5) % 7) + 1;
95+
*/
8096
}
8197

8298
// установка даты и времени (удобнее чем писать переменные вурчную)
@@ -101,4 +117,4 @@ class UnixTime {
101117
private:
102118
int8_t _gmt = 0;
103119
};
104-
#endif
120+
#endif

0 commit comments

Comments
 (0)