forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime.h
More file actions
143 lines (113 loc) · 4.29 KB
/
Copy pathTime.h
File metadata and controls
143 lines (113 loc) · 4.29 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#ifndef MO_TIME_H
#define MO_TIME_H
#include <functional>
#include <stdint.h>
#include <stddef.h>
#include <MicroOcpp/Core/Memory.h>
#include <MicroOcpp/Platform.h>
#ifndef MO_ENABLE_TIMESTAMP_MILLISECONDS
#define MO_ENABLE_TIMESTAMP_MILLISECONDS 0
#endif
#if MO_ENABLE_TIMESTAMP_MILLISECONDS
#define JSONDATE_LENGTH 24 //max. ISO 8601 date length, excluding the terminating zero
#else
#define JSONDATE_LENGTH 20 //ISO 8601 date length, excluding the terminating zero
#endif //MO_ENABLE_TIMESTAMP_MILLISECONDS
namespace MicroOcpp {
class Timestamp : public MemoryManaged {
private:
/*
* Internal representation of the current time. The initial values correspond to UNIX-time 0. January
* corresponds to month 0 and the first day in the month is day 0.
*/
int16_t year = 1970;
int16_t month = 0;
int16_t day = 0;
int32_t hour = 0;
int32_t minute = 0;
int32_t second = 0;
#if MO_ENABLE_TIMESTAMP_MILLISECONDS
int32_t ms = 0;
#endif //MO_ENABLE_TIMESTAMP_MILLISECONDS
public:
Timestamp();
Timestamp(const Timestamp& other);
#if MO_ENABLE_TIMESTAMP_MILLISECONDS
Timestamp(int16_t year, int16_t month, int16_t day, int32_t hour, int32_t minute, int32_t second, int32_t ms = 0);
#else
Timestamp(int16_t year, int16_t month, int16_t day, int32_t hour, int32_t minute, int32_t second);
#endif //MO_ENABLE_TIMESTAMP_MILLISECONDS
/**
* Expects a date string like
* 2020-10-01T20:53:32.486Z
*
* as generated in JavaScript by calling toJSON() on a Date object
*
* Only processes the first 19 characters. The subsequent are ignored until terminating 0.
*
* Has a semi-sophisticated type check included. Will return true on successful time set and false if
* the given string is not a JSON Date string.
*
* jsonDateString: 0-terminated string
*/
bool setTime(const char* jsonDateString);
bool toJsonString(char *out, size_t buffsize) const;
Timestamp &operator=(const Timestamp &rhs);
#if MO_ENABLE_TIMESTAMP_MILLISECONDS
Timestamp &addMilliseconds(int ms);
#endif //MO_ENABLE_TIMESTAMP_MILLISECONDS
/*
* Time periods are given in seconds for all of the following arithmetic operations
*/
Timestamp &operator+=(int secs);
Timestamp &operator-=(int secs);
int operator-(const Timestamp &rhs) const;
friend Timestamp operator+(const Timestamp &lhs, int secs);
friend Timestamp operator-(const Timestamp &lhs, int secs);
friend bool operator==(const Timestamp &lhs, const Timestamp &rhs);
friend bool operator!=(const Timestamp &lhs, const Timestamp &rhs);
friend bool operator<(const Timestamp &lhs, const Timestamp &rhs);
friend bool operator<=(const Timestamp &lhs, const Timestamp &rhs);
friend bool operator>(const Timestamp &lhs, const Timestamp &rhs);
friend bool operator>=(const Timestamp &lhs, const Timestamp &rhs);
};
extern const Timestamp MIN_TIME;
extern const Timestamp MAX_TIME;
class Clock {
private:
Timestamp mocpp_basetime = Timestamp();
decltype(mocpp_tick_ms()) system_basetime = 0; //the value of mocpp_tick_ms() when OCPP server's time was taken
decltype(mocpp_tick_ms()) lastUpdate = 0;
Timestamp currentTime = Timestamp();
public:
Clock();
Clock(const Clock&) = delete;
Clock(const Clock&&) = delete;
Clock& operator=(const Clock&) = delete;
const Timestamp &now();
/**
* Expects a date string like
* 2020-10-01T20:53:32.486Z
*
* as generated in JavaScript by calling toJSON() on a Date object
*
* Only processes the first 23 characters. The subsequent are ignored
*
* Has a semi-sophisticated type check included. Will return true on successful time set and false if
* the given string is not a JSON Date string.
*
* jsonDateString: 0-terminated string
*/
bool setTime(const char* jsonDateString);
/*
* Timestamps which were taken before the Clock was initially set can be adjusted retrospectively. Two
* conditions must be true: the Clock was set in the meantime and the Timestamp was taken at the same
* run of this library. The caller must check this
*/
Timestamp adjustPrebootTimestamp(const Timestamp& t);
};
}
#endif