Xyris
0.5
time.cpp
Go to the documentation of this file.
1
/**
2
* @file time.hpp
3
* @author Michel (JMallone) Gomes (
[email protected]
)
4
* @brief Time lib code
5
* @version 0.1
6
* @date 2021-07-27
7
*
8
* @copyright Copyright the Xyris Contributors (c) 2021
9
*
10
* References:
11
* https://github.com/sidsingh78/EPOCH-to-time-date-converter/blob/master/epoch_conv.c
12
* https://www.oryx-embedded.com/doc/date__time_8c_source.html
13
*/
14
#include <
Devices/Clock/rtc.hpp
>
15
#include <
Library/time.hpp
>
16
17
namespace
Time
{
18
19
TimeDescriptor::TimeDescriptor
()
20
{
21
toDate
();
22
}
23
24
TimeDescriptor::TimeDescriptor
(
int
sec,
int
min,
int
hr,
int
d,
int
m,
int
y)
25
: _seconds(sec)
26
, _minutes(min)
27
, _hour(hr)
28
, _day(d)
29
, _month(m)
30
, _year(y)
31
{
32
// Initialize nothing.
33
}
34
TimeDescriptor::~TimeDescriptor
()
35
{
36
// Nothing to deconstruct
37
}
38
39
void
TimeDescriptor::converterEpochToDate
(uint64_t epoch)
40
{
41
int
a, b, c,
day
,
month
,
year
;
42
setSeconds
(epoch % 60);
43
epoch /= 60;
44
setMinutes
(epoch % 60);
45
epoch /= 60;
46
setHour
(epoch % 24);
47
epoch /= 24;
48
49
// This is the formula used by everyone. No idea how it works :p
50
a = ((4 * epoch + 102032) / 146097 + 15);
51
b = (epoch + 2442113 + a - (a / 4));
52
year
= (20 * b - 2442) / 7305;
53
c = b - 365 *
year
- (
year
/ 4);
54
month
= c * 1000 / 30601;
55
day
= c -
month
* 30 -
month
* 601 / 1000;
56
if
(
month
<= 13) {
57
year
-= 4716;
58
month
-= 1;
59
}
else
{
60
year
-= 4715;
61
month
-= 13;
62
}
63
64
setDay
(
day
);
65
setMonth
(
month
);
66
setYear
(
year
);
67
}
68
69
void
TimeDescriptor::toDate
()
70
{
71
uint64_t epoch =
RTC::getEpoch
();
72
converterEpochToDate
(epoch);
73
// TODO: for another GMT, add or subtract time zone in epoch number
74
// e.g
75
// for GMT -3 =(-3*3600)= -10800
76
// converterEpochToDate(epoch-10800);
77
converterEpochToDate
(epoch);
78
}
79
80
}
// !namespace Time
Time
Definition:
time.cpp:17
Time::TimeDescriptor::setYear
void setYear(int y)
Definition:
time.hpp:38
rtc.hpp
Time::TimeDescriptor::setDay
void setDay(int d)
Definition:
time.hpp:36
RTC::month
uint32_t month
Definition:
rtc.cpp:39
Time::TimeDescriptor::converterEpochToDate
void converterEpochToDate(uint64_t Number)
Definition:
time.cpp:39
Time::TimeDescriptor::setSeconds
void setSeconds(int sec)
Definition:
time.hpp:33
RTC::getEpoch
uint64_t getEpoch()
Get a epoch number from rtc.
Definition:
rtc.cpp:193
Time::TimeDescriptor::~TimeDescriptor
~TimeDescriptor()
Definition:
time.cpp:34
RTC::day
uint32_t day
Definition:
rtc.cpp:38
Time::TimeDescriptor::TimeDescriptor
TimeDescriptor()
Definition:
time.cpp:19
Time::TimeDescriptor::setMonth
void setMonth(int m)
Definition:
time.hpp:37
Time::TimeDescriptor::setMinutes
void setMinutes(int min)
Definition:
time.hpp:34
time.hpp
Time lib code.
Time::TimeDescriptor::toDate
void toDate()
Definition:
time.cpp:69
Time::TimeDescriptor::setHour
void setHour(int hr)
Definition:
time.hpp:35
RTC::year
uint32_t year
Definition:
rtc.cpp:40
Kernel
Library
time.cpp
Generated by
1.8.17