add seconds since epoch to datetime struct

This commit is contained in:
2021-02-07 18:35:10 +01:00
parent 27f646860d
commit 0957fc4830
2 changed files with 9 additions and 4 deletions

View File

@ -71,7 +71,7 @@ uint16_t ntp_retry_cnt=0; //counting the ntp retry number
48) UTC+13:00 Tonga 48) UTC+13:00 Tonga
49) UTC+14:00 Kiribati (Line Islands) 49) UTC+14:00 Kiribati (Line Islands)
*/ */
void get_seconds_from_ntp_server(uint8_t *buf, uint16_t idx) uint64_t get_seconds_from_ntp_server(uint8_t *buf, uint16_t idx)
{ {
tstamp seconds = 0; tstamp seconds = 0;
uint8_t i=0; uint8_t i=0;
@ -79,6 +79,8 @@ void get_seconds_from_ntp_server(uint8_t *buf, uint16_t idx)
{ {
seconds = (seconds << 8) | buf[idx + i]; seconds = (seconds << 8) | buf[idx + i];
} }
uint64_t rawSeconds = seconds;
switch (time_zone) switch (time_zone)
{ {
case 0: case 0:
@ -132,7 +134,7 @@ void get_seconds_from_ntp_server(uint8_t *buf, uint16_t idx)
case 20: case 20:
seconds -= 1*3600; seconds -= 1*3600;
break; break;
case 21: //<2F><EFBFBD>? case 21: //<2F>?
case 22: case 22:
break; break;
case 23: case 23:
@ -213,6 +215,8 @@ void get_seconds_from_ntp_server(uint8_t *buf, uint16_t idx)
//calculation for date //calculation for date
calcdatetime(seconds); calcdatetime(seconds);
return rawSeconds;
} }
void SNTP_init(uint8_t s, uint8_t *ntp_server, uint8_t tz, uint8_t *buf) void SNTP_init(uint8_t s, uint8_t *ntp_server, uint8_t tz, uint8_t *buf)
@ -262,7 +266,7 @@ int8_t SNTP_run(datetime *time)
if (RSR_len > MAX_SNTP_BUF_SIZE) RSR_len = MAX_SNTP_BUF_SIZE; // if Rx data size is lager than TX_RX_MAX_BUF_SIZE if (RSR_len > MAX_SNTP_BUF_SIZE) RSR_len = MAX_SNTP_BUF_SIZE; // if Rx data size is lager than TX_RX_MAX_BUF_SIZE
recvfrom(NTP_SOCKET, data_buf, RSR_len, (uint8_t *)&destip, &destport); recvfrom(NTP_SOCKET, data_buf, RSR_len, (uint8_t *)&destip, &destport);
get_seconds_from_ntp_server(data_buf,startindex); time->seconds = get_seconds_from_ntp_server(data_buf,startindex);
time->yy = Nowdatetime.yy; time->yy = Nowdatetime.yy;
time->mo = Nowdatetime.mo; time->mo = Nowdatetime.mo;
time->dd = Nowdatetime.dd; time->dd = Nowdatetime.dd;

View File

@ -56,6 +56,7 @@ typedef struct _datetime
uint8_t hh; uint8_t hh;
uint8_t mm; uint8_t mm;
uint8_t ss; uint8_t ss;
uint64_t seconds;
} datetime; } datetime;
#define ntp_port 123 //ntp server port number #define ntp_port 123 //ntp server port number
@ -63,7 +64,7 @@ typedef struct _datetime
#define UTC_ADJ_HRS 9 // SEOUL : GMT+9 #define UTC_ADJ_HRS 9 // SEOUL : GMT+9
#define EPOCH 1900 // NTP start year #define EPOCH 1900 // NTP start year
void get_seconds_from_ntp_server(uint8_t *buf, uint16_t idx); uint64_t get_seconds_from_ntp_server(uint8_t *buf, uint16_t idx);
void SNTP_init(uint8_t s, uint8_t *ntp_server, uint8_t tz, uint8_t *buf); void SNTP_init(uint8_t s, uint8_t *ntp_server, uint8_t tz, uint8_t *buf);
int8_t SNTP_run(datetime *time); int8_t SNTP_run(datetime *time);
tstamp changedatetime_to_seconds(void); tstamp changedatetime_to_seconds(void);