-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData.cpp
More file actions
106 lines (97 loc) · 2.79 KB
/
Data.cpp
File metadata and controls
106 lines (97 loc) · 2.79 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
#ifndef __linux__
#include <windows.h>
#else
#include <time.h>
#include <sys/time.h>
#endif
#include <stdio.h>
#include "Data.h"
#include <cstring>
#include <cstdarg>
#include <linux/rtc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <unistd.h>
void GetCurDateTime( SDateTime* pDT ) {
#ifndef __linux__
SYSTEMTIME time;
GetLocalTime(&time);
pDT->Milliseconds = (uint16)time.wMilliseconds;
pDT->Second = (uint8)time.wSecond;
pDT->Minute = (uint8)time.wMinute;
pDT->Hour = (uint8)time.wHour;
pDT->Day = (uint8)time.wDay;
pDT->Month = (uint8)time.wMonth;
pDT->Year = (uint8)(time.wYear - 2000);
#else
char buffer[16];
char tmp[3];
int milli;
struct timeval tv;
struct timespec ts;
time_t t = time(NULL);
struct tm *t_m;
clock_gettime (CLOCK_REALTIME, &ts);
t_m = localtime(&ts.tv_sec);
pDT->Milliseconds = ts.tv_nsec/1000000UL;
pDT->Second = (uint8)t_m->tm_sec;
pDT->Minute = (uint8)t_m->tm_min;
pDT->Hour = (uint8)t_m->tm_hour;
pDT->Day = (uint8)t_m->tm_mday;
pDT->Month = (uint8)t_m->tm_mon+1;
pDT->Year = (uint8)(t_m->tm_year - 100);
#endif
}
char hex2bin( const char *s )
{
int ret=0;
int i;
for( i=0; i<2; i++ )
{
char c = *s++;
int n=0;
if( '0'<=c && c<='9' )
n = c-'0';
else if( 'a'<=c && c<='f' )
n = 10 + c-'a';
else if( 'A'<=c && c<='F' )
n = 10 + c-'A';
ret = n + ret*16;
}
return ret;
}
const char *DateTime2Str( SDateTime *pDT ) {
static char str[32];
sprintf( str, "%02u%02u%02u.%02u%02u%02u%03u", pDT->Year, pDT->Month, pDT->Day,
pDT->Hour, pDT->Minute, pDT->Second, pDT->Milliseconds );
str[16] = 0;
return str;
}
//============================================================================
//
//============================================================================
int SetRTCDateTime( struct tm* udt ) {
struct rtc_time rtc_tm;
int rtc_fd = open( "/dev/rtc0", O_RDWR);
if ( rtc_fd < 0 ) {
printf( "/dev/rtc open error\n" );
return -1;
}
// Set the RTC time/date
rtc_tm.tm_mday = udt->tm_mday;
rtc_tm.tm_mon = udt->tm_mon;
rtc_tm.tm_year = udt->tm_year;
rtc_tm.tm_hour = udt->tm_hour;
rtc_tm.tm_min = udt->tm_min;
rtc_tm.tm_sec = udt->tm_sec;
int ret = ioctl( rtc_fd, RTC_SET_TIME, &rtc_tm );
if ( ret < 0 ) {
printf( "GetDateTime RTC_SET_TIME error (%d) %s\n", ret , strerror(errno));
return ret;
}
close(rtc_fd);
return 0;
}