SBB Host
Host communication with the self balancing bike.
Loading...
Searching...
No Matches
Timepp.h
Go to the documentation of this file.
1
2#ifndef TIME_H
3#define TIME_H
4
5#include <time.h>
6#include <stdio.h>
7
8#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || defined(__WIN32__) || defined(WIN64) || defined(_WIN64) || defined(__WIN64) || defined(__WIN64__)
9#ifndef OS_WIN
10#define OS_WIN
11#endif
12#elif defined(linux) || defined(__linux) || defined(__linux__) || defined(__GNUC__) || defined(__GNUG__) || defined(unix) || defined(__unix) || defined(__unix__)
13#ifndef OS_LINUX
14#define OS_LINUX
15#endif
16#else
17#error OS not supported
18#endif
19
20#ifdef OS_LINUX
21#define localtime_s(pTm,pT) (*(pTm))=*localtime(pT) //in linux use localtime for localtime_s
22#define sprintf_s(buf, len, ...) snprintf((buf), (len), __VA_ARGS__)
23#endif
24
25/*
26* Class for timing purpose
27* This is actually just a C++ (i.e. class) wrapper for the C type tm
28* Note that not all functions has been implemented yet
29*/
30
37class Time {
38private:
39 struct tm tm;
40 static constexpr int STR_LEN = 256;
41
42public:
43
44
48 Time() {
50 }
51
55 void update_time() {
56 time_t t = time(NULL);
57 localtime_s(&tm, &t);
58 }
59
65 int getYear(bool update = true) { if (update) update_time(); return (tm.tm_year + 1900); }
66
72 int getMonth(bool update = true) { if (update) update_time(); return (tm.tm_mon + 1); }
73
79 int getDay(bool update = true) { if (update) update_time(); return (tm.tm_mday); }
80
86 int getHour(bool update = true) { if (update) update_time(); return (tm.tm_hour); }
87
93 int getMin(bool update = true) { if (update) update_time(); return (tm.tm_min); }
94
100 int getSec(bool update = true) { if (update) update_time(); return (tm.tm_sec); }
101
110 int getData(char* str, size_t len, bool update = true) { return sprintf_s(str, len, "%d-%02d-%02d %02d:%02d:%02d", getYear(update), getMonth(update), getDay(update), getHour(update), getMin(update), getSec(update)); }
111
118 std::string getData(bool update = true) {
119 char str[STR_LEN] = { 0 };
120 getData(str, STR_LEN, update);
121 return (std::string)str;
122 }
123};
124
125#endif
#define sprintf_s(buf, len,...)
Definition: Serial.h:27
A class to get system time.
Definition: Timepp.h:37
int getYear(bool update=true)
Get the current year.
Definition: Timepp.h:65
int getData(char *str, size_t len, bool update=true)
Get the current date-time.
Definition: Timepp.h:110
Time()
Constructor.
Definition: Timepp.h:48
int getSec(bool update=true)
Get the current second.
Definition: Timepp.h:100
static constexpr int STR_LEN
String length to use as buffer.
Definition: Timepp.h:40
int getDay(bool update=true)
Get the current month day.
Definition: Timepp.h:79
void update_time()
Update the time.
Definition: Timepp.h:55
int getMonth(bool update=true)
Get the current month.
Definition: Timepp.h:72
int getHour(bool update=true)
Get the current hour.
Definition: Timepp.h:86
struct tm tm
Variable to store the current time.
Definition: Timepp.h:39
std::string getData(bool update=true)
Get the current data-time.
Definition: Timepp.h:118
int getMin(bool update=true)
Get the current minute.
Definition: Timepp.h:93