• AVR Freaks

AnsweredHot![SOLVED] time.h not compiling - getting timestamp from NTP server - PIC32MX - MPLAB X

Author
seccoxiru
Junior Member
  • Total Posts : 102
  • Reward points : 0
  • Joined: 2011/10/27 13:42:59
  • Location: 0
  • Status: offline
2019/09/22 06:40:37 (permalink)
0

[SOLVED] time.h not compiling - getting timestamp from NTP server - PIC32MX - MPLAB X

 Hi.
I am using MPLAB X v5.25, XC32 compiler v1.43, PIC32MX270F256D.
I am using a ESP8266/ESP-01 Wifi module together with the PIC32MX to get the current timestamp from a NTP server.

For example, yesterday I sent a request to the server and got this number as response: ‭
--> 0xE1311EC3 = 3778092739‬. This is the number os seconds since 1/Jan/1900.
As UNIX systems have the timestamp related to 1/Jan/1970, I have to substract 2208988800 (seconds) from the original value, ant this resulted in the following number:
--> 1569103939 (This is the timestamp that I will convert to obtain current date/time).
Having that number is hands, I went to the website "www.epochconverter.com", placed number 1569103939 in the field and clicked the button "Timestamp to Human date", and these were the results of the conversion:
--> GMT: Saturday, 21 September 2019 22:12:19
--> Your time zone: Sábado, 21 de Setembro de 2019 às 19:12:19 GMT-03:00 (BRAZILIAN CAPITAL)
And the conversion was correct.

I have tried this type of conversion using functions of time.h but no sucess, it is not compiling.
I would like to know if is it possible to do the conversion that the website did using time.h or other lib of the IDE system.
I have tried this:

#include <time.h>
 
 
 
void main (void)
{
struct tm *current_date;
time_t seconds = 1569103939;
 
 
 
time(&seconds);
current_date = localtime(&seconds);
 
 
 
//REMAINING CODE...
}

 
And the Output of compilation shows this:

time.c:(.text.time+0x14): warning: Linking code with default gettimeofday routine. Hint: Write an app-specific version.
Error: A heap is required, but has not been specified


Compiling this way:

time(&seconds);
//current_date = localtime(&seconds);

Output is:
time.c:(.text.time+0x14): warning: Linking code with default gettimeofday routine. Hint: Write an app-specific version

Compiling this way:
//time(&seconds);
current_date = localtime(&seconds);

Output is:
Error: A heap is required, but has not been specified

 
In my application, each 10 seconds (I have a Timer that increments a uint32_t variable each 1ms via interrupt, with Timer5) I will do a request to the NTP server to obtain the Timestamp value, and I just need to convert it to Human readable format, how can I do this in the easiest way?
 
Or, what more can I do?
Regards.
 
EDIT:
time_t seconds = 1569103939;

post edited by seccoxiru - 2019/09/22 10:08:55
#1
aschen0866
Super Member
  • Total Posts : 4504
  • Reward points : 0
  • Joined: 2006/01/08 22:18:32
  • Location: San Diego
  • Status: offline
Re: time.h not compiling - getting timestamp from NTP server - PIC32MX 2019/09/22 08:18:24 (permalink) ☼ Best Answerby seccoxiru 2019/09/22 08:38:38
+2 (2)
I'll go to Project Properties -> xc32-ld, put 0 in the heap size field to satisfy the compiler. Running this in the MPLABX simulator,

struct tm *current_date;
time_t seconds = 1569103939;
current_date = localtime(&seconds);

printf("tm_year = %d\r\n", current_date->tm_year);
printf("tm_mon = %d\r\n", current_date->tm_mon);
printf("tm_mday = %d\r\n", current_date->tm_mday);

I get

tm_year = 119
tm_mon = 8
tm_mday = 21

That's 2019-9-21
#2
seccoxiru
Junior Member
  • Total Posts : 102
  • Reward points : 0
  • Joined: 2011/10/27 13:42:59
  • Location: 0
  • Status: offline
Re: time.h not compiling - getting timestamp from NTP server - PIC32MX 2019/09/22 08:50:03 (permalink)
0
Hi aschen0866, thanks for your help.
Not its working.
Regards.
#3
aschen0866
Super Member
  • Total Posts : 4504
  • Reward points : 0
  • Joined: 2006/01/08 22:18:32
  • Location: San Diego
  • Status: offline
Re: time.h not compiling - getting timestamp from NTP server - PIC32MX 2019/09/22 09:05:06 (permalink)
+1 (1)
Do keep in mind that time_t is a signed 32-bit value in the current implementation. This will become a problem when the year reaches 2038. In the above test case, if seconds = 0x80000000, you'll get

tm_year = 1
tm_mon = 11
tm_mday = 13

This is a known issue to Microchip but I don't know when they are going to fix it.
#4
seccoxiru
Junior Member
  • Total Posts : 102
  • Reward points : 0
  • Joined: 2011/10/27 13:42:59
  • Location: 0
  • Status: offline
Re: time.h not compiling - getting timestamp from NTP server - PIC32MX 2019/09/22 10:07:27 (permalink)
0
aschen0866
Do keep in mind that time_t is a signed 32-bit value in the current implementation. This will become a problem when the year reaches 2038. In the above test case, if seconds = 0x80000000, you'll get

tm_year = 1
tm_mon = 11
tm_mday = 13

This is a known issue to Microchip but I don't know when they are going to fix it.


I know about this possible bug when reach year 2038, thanks for note, but currently I don't worry about that, we have many years to fix that :) Maybe then can simply change to unsigned int32 in future...
 
Regards.
#5
seccoxiru
Junior Member
  • Total Posts : 102
  • Reward points : 0
  • Joined: 2011/10/27 13:42:59
  • Location: 0
  • Status: offline
Re: [SOLVED] time.h not compiling - getting timestamp from NTP server - PIC32MX - MPLAB X 2019/09/22 10:33:49 (permalink)
0
Hi.
When I try to do this in code:
seconds -= 2208988800;

 
I get this warning in the Output
warning: this decimal constant is unsigned only in ISO C90 [enabled by default]

 
Do you know how to disable this warning? In order it will not be shown when compiling?
 
Regards.
#6
NKurzman
A Guy on the Net
  • Total Posts : 17847
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: time.h not compiling - getting timestamp from NTP server - PIC32MX 2019/09/22 10:34:56 (permalink)
0
we have many years to fix that ?
That’s what all the Y2K programmers said.
#7
pippo18
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2018/08/16 13:02:07
  • Location: 0
  • Status: offline
Re: time.h not compiling - getting timestamp from NTP server - PIC32MX 2019/09/22 11:15:09 (permalink)
0
seccoxiru
I know about this possible bug when reach year 2038, thanks for note, but currently I don't worry about that, we have many years to fix that :)




 
Servus :) John Titor here
#8
Jump to:
© 2019 APG vNext Commercial Version 4.5