customized_DateAndTime.cpp
2.96 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
#include "simple_obd_test.h"
#include "FreematicsGPS.h"
#include "customized_DateAndTime.h"
FreematicsESP32* p_sys;
static SemaphoreHandle_t xSemaphore_Time=NULL;
time_t StringToDatetime(char *str)
{
tm tm_;
int year, month, day, hour, minute,second;
sscanf(str,"%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
tm_.tm_year = year-1900;
tm_.tm_mon = month-1;
tm_.tm_mday = day;
tm_.tm_hour = hour;
tm_.tm_min = minute;
tm_.tm_sec = second;
tm_.tm_isdst = 0;
time_t t_ = mktime(&tm_); // Has been lost 8 A time zone
return t_; // A second time
}
void UpdateTime(void){
GPS_DATA* gd = 0;
unsigned int GPS_Year;
unsigned int GPS_Month;
unsigned int GPS_Day;
unsigned int GPS_Hour;
unsigned int GPS_Minutes;
unsigned int GPS_Seconds;
unsigned int GPS_mSeconds;
(*p_sys).getCurrentUTC(&gd);
GPS_Year= (unsigned int) (gd->date % 100) + 2000;
GPS_Month= (unsigned int) (gd->date / 100) % 100;
GPS_Day= (unsigned int) (gd->date / 10000);
GPS_Hour = (unsigned int) gd->time / 1000000;
GPS_Minutes = (unsigned int)(gd->time % 1000000) / 10000;
GPS_Seconds = (unsigned int)(gd->time % 10000) / 100;
GPS_mSeconds = (gd->time % 100)/10;
char isoTime[100]={0};
sprintf(isoTime,"%04u-%02u-%02u %02u:%02u:%02u",\
GPS_Year,\
GPS_Month,\
GPS_Day,\
GPS_Hour,\
GPS_Minutes,\
GPS_Seconds);
time_t t_=StringToDatetime(isoTime);
struct timeval tv;
tv.tv_sec=t_;
tv.tv_usec=GPS_mSeconds*100000;
settimeofday(&tv,NULL);
}
void SetSysInstance(FreematicsESP32* sys){
p_sys=(FreematicsESP32*)sys;
xSemaphore_Time= xSemaphoreCreateMutex();
#if USE_SERIAL==1
Serial.println("Time is set correctly");
#endif
}
void GetCurrentTime(char* isoTime){
xSemaphoreTake(xSemaphore_Time,portMAX_DELAY);
struct timeval tv;
time_t nowtime;
struct tm *nowtm;
char tmbuf[64];
gettimeofday(&tv, NULL);
nowtime = tv.tv_sec;
nowtm = localtime(&nowtime);
strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm);
sprintf(isoTime,"%s.%06ld", tmbuf, tv.tv_usec);
xSemaphoreGive(xSemaphore_Time);
}
void GetCurrentGPSTime(char* isoTime){
GPS_DATA* gd = 0;
char* p;
unsigned int GPS_Year;
unsigned int GPS_Month;
unsigned int GPS_Day;
unsigned int GPS_Hour;
unsigned int GPS_Minutes;
unsigned int GPS_Seconds;
unsigned int GPS_mSeconds;
(*p_sys).getCurrentUTC(&gd);
GPS_Year= (unsigned int) (gd->date % 100) + 2000;
GPS_Month= (unsigned int) (gd->date / 100) % 100;
GPS_Day= (unsigned int) (gd->date / 10000);
GPS_Hour = (unsigned int) gd->time / 1000000;
GPS_Minutes = (unsigned int)(gd->time % 1000000) / 10000;
GPS_Seconds = (unsigned int)(gd->time % 10000) / 100;
GPS_mSeconds = (gd->time % 100)/10;
p=isoTime;
p+=sprintf(isoTime,"%04u-%02u-%02u %02u:%02u:%02u",\
GPS_Year,\
GPS_Month,\
GPS_Day,\
GPS_Hour,\
GPS_Minutes,\
GPS_Seconds);
sprintf(p,".%06ld", GPS_mSeconds*100000);
}
int GetShiftTime(char* tm1,char* tm2){
time_t t1=StringToDatetime(tm1);
time_t t2=StringToDatetime(tm2);
return t2-t1;
}