gettimeofday not working with timeval [IDFGH-3263]

halilarkl07
Posts: 10
Joined: Thu May 07, 2020 9:09 am

gettimeofday not working with timeval [IDFGH-3263]

Postby halilarkl07 » Thu May 07, 2020 9:44 am

Hi guys,
I have a problem with getting time. I need to get the current time in millisecond resolution.
I know that gettimeofday can get in microsecond resolution based on:
https://docs.espressif.com/projects/esp ... _time.html



I am using;

Code: Untitled.c Select all


struct timeval tv_now; 

gettimeofday(&tv_now, NULL);

int64_t time_us = (int64_t)tv_now.tv_sec * 1000000L + (int64_t)tv_now.tv_usec;

printf("time_us %" PRId64 "\n", time_us);
time_us 2806483326588896768
after 10 sec;
time_us 2961102149255896768
there is no sense between them,

also "tv_now.tv_usec" always 0 no mether what.

Code: Untitled.c Select all

printf("tv_now.tv_sec %" PRId64 "\n", (int64_t)tv_now.tv_usec);




This is the code;

Code: Untitled.c Select all


struct timeval tv_now;

gettimeofday(&tv_now, NULL);

int64_t time_us = (int64_t)tv_now.tv_sec * 1000000L + (int64_t)tv_now.tv_usec;

printf("tv_now.tv_sec %ld\n", (long)tv_now.tv_sec);

printf("PRId64 tv_now.tv_sec %" PRId64 "\n", (int64_t)tv_now.tv_sec);

printf("tv_now.tv_usec %" PRId64 "\n", (int64_t)tv_now.tv_usec);

printf("time_us %" PRId64 "\n", time_us);
And this is the serial monitor output for the code above:

tv_now.tv_sec 1588757091
PRId64 tv_now.tv_sec 39854590296675
tv_now.tv_usec 0
time_us 2961102149255896768

I am an amateur with all this,
Thank you all for helping me.

Alvin
Espressif staff
Espressif staff
Posts: 259
Joined: Thu May 17, 2018 2:26 am

Re: gettimeofday not working with timeval [IDFGH-3263]

Postby Alvin » Mon May 11, 2020 6:27 am

Moderator's note: edit the topic title for issue tracking, thanks.

Konstantin
Espressif staff
Espressif staff
Posts: 15
Joined: Tue Feb 05, 2019 7:31 am

Re: gettimeofday not working with timeval [IDFGH-3263]

Postby Konstantin » Mon May 11, 2020 5:54 pm

Hi @halilarkl07!
I tried your code and can not get the same result as you reported (it works as expected). I used the hello_world example as a base and put your code. I suggest you try my way, apply the hello_world_patch.txt patch for hello_world. The log like this:

tv_now.tv_sec 0
PRId64 tv_now.tv_sec 0
tv_now.tv_usec 38442
time_us 38442
...
tv_now.tv_sec 11
PRId64 tv_now.tv_sec 11
tv_now.tv_usec 374873
time_us 11374873
...
tv_now.tv_sec 22
PRId64 tv_now.tv_sec 22
tv_now.tv_usec 716287
time_us 22716287

Thanks.
Attachments
hello_world_patch.txt
(1.16 KiB) Downloaded 548 times

halilarkl07
Posts: 10
Joined: Thu May 07, 2020 9:09 am

Re: gettimeofday not working with timeval [IDFGH-3263]

Postby halilarkl07 » Mon May 11, 2020 7:31 pm

Problem solved,

Code: Untitled.c Select all


#include <sys/param>
Somehow this header cause the problem with timeval.
while this written, tv_sec was long long int, but when I erase the "#include <sys/param>" tv_sec become long int.

Now everything okay,

Thank you for helping me @Konstantin,
Have a great day.

Who is online

Users browsing this forum: Amazon [Bot], Baidu [Spider], Google [Bot], Qwantbot and 6 guests