How can I set the date / time?

pete_l
Posts: 2
Joined: Fri Mar 16, 2018 11:18 am

How can I set the date / time?

Postby pete_l » Sun Jun 10, 2018 5:42 pm

I an moving some code from an ESP8266 to an ESP32.
I am using the Arduino IDE
I wish to set an arbitrary date and time

In the ESP8266 code I can use a library function called setTime(). However this doesn't exist in the ESP32 version of the libraries.
There is an example in the Expressif code that uses the Unix-y settimeofday(&tv, &tz) function. However this example requires a header file called coredecls.h that does not appear on the code base I installed with the ESP32 Arduino environment.

What do you do to set the time without using NTP?

with thanks!

avdalimov14
Posts: 7
Joined: Tue Apr 17, 2018 2:24 pm

Re: How can I set the date / time?

Postby avdalimov14 » Sun Jun 17, 2018 2:13 pm

What do you do to set the time without using NTP?
I would like to join this question.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: How can I set the date / time?

Postby WiFive » Sun Jun 17, 2018 5:38 pm

Did you try

Code: Select all

#include <time.h>
#include <sys/time.h>

avdalimov14
Posts: 7
Joined: Tue Apr 17, 2018 2:24 pm

Re: How can I set the date / time?

Postby avdalimov14 » Mon Jun 18, 2018 1:35 pm

Yes, but I couldn't figure out what the time val that I should put in settimeofday?

Let's assume I want to update it to 18 June 2018 (now it is on his release data - default), How should I make it?

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: How can I set the date / time?

Postby WiFive » Mon Jun 18, 2018 2:48 pm


snahmad75
Posts: 445
Joined: Wed Jan 24, 2018 6:32 pm

Re: How can I set the date / time?

Postby snahmad75 » Thu Nov 15, 2018 1:54 pm

Once you set date and time using

settimeofday

How I can add seconds since boot up get from esp_timer_get_time in seconds.

I guess once you bootup. you lost your date and time. you need to save into nvs or file and on bootup. I need to set it again
using settimeofday and add boot time seconds to it before creation new files to get almost accurate time stamp for files.

I don't have access to internet to get clock time. I pass data time via HTTP when ever I upgrade my firmware.
Some logs files get created while firmware is running.


I set my my current system date/time on startup

Code: Select all

struct tm tm;
tm.tm_year = 2018 - 1900;
tm.tm_mon = 10;
tm.tm_mday = 15;
tm.tm_hour = 14;
tm.tm_min = 10;
tm.tm_sec = 10;
time_t t = mktime(&tm);
printf("Setting time: %s", asctime(&tm));
struct timeval now = { .tv_sec = t };
settimeofday(&now, NULL);
then I obtain later . which is fine. but file gets created with time stamp of 1980. any idea?

Code: Select all

		time_t now = time(0);
// Convert now to tm struct for local timezone
tm* localtm = localtime(&now);
printf("The local date and time is: %s", asctime(localtm));

travtrav
Posts: 1
Joined: Mon May 25, 2020 10:37 pm

Re: How can I set the date / time?

Postby travtrav » Mon May 25, 2020 10:48 pm

Yes, the timestamp of 1980 attach to the image file is something that I have found and wanting to correct. As a novice, new to programming and esp32, I would very appreciative if we can obtain some direction/solution to solve this.

fbiego
Posts: 3
Joined: Sun Jan 24, 2021 1:00 pm

Re: How can I set the date / time?

Postby fbiego » Sun Jan 24, 2021 1:04 pm

I wrote an Arduino Library to set and time
https://www.arduinolibraries.info/libraries/esp32-time

moefear85
Posts: 44
Joined: Sun Sep 05, 2021 4:55 pm

Re: How can I set the date / time?

Postby moefear85 » Mon Jun 13, 2022 11:19 am

avdalimov14 wrote:
Mon Jun 18, 2018 1:35 pm
Yes, but I couldn't figure out what the time val that I should put in settimeofday?

Let's assume I want to update it to 18 June 2018 (now it is on his release data - default), How should I make it?
Straight-up Answer:
settimeofday() takes two params, one for time, the other for timezone. the time param is of type "timeval", which is a struct that has two members that you set: "tv_sec" and "tv_usec". tv_sec is of type time_t, meaning it is seconds since epoch (Jan 1, 1970). tv_usec is set to microseconds, since the original time_t doesn't have that accuracy. The timezone is of type "timezone", that has two members "tz_minuteswest" and "tz_dsttime". Not sure what tz_dsttime is about, but tz_minuteswest is the timezone offset in minutes, but it is signed, with the west being positive.

Some Tips (if you don't have internet while working):
You need to know two things, the unit of the parameter, and the origin (ie the offset from which it counts). The unit I think is seconds, although the underlying functions it calls to set the time take microseconds. To find the offset, just set it using a value of 0, then obtain the date, and that will tell you microseconds since when. Most likely microseconds since epoch. So if you want to update the time to now, you would have to pass in how many microseconds have passed since the epoch (google it).

To get the unit, set the value to 0, then to 1, then multiples of 10, while reading out cTime(). From there you can deduce the units.
Last edited by moefear85 on Mon Jun 13, 2022 12:26 pm, edited 5 times in total.

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: How can I set the date / time?

Postby lbernstone » Mon Jun 13, 2022 11:27 am


Who is online

Users browsing this forum: DonEvans, MicroController and 31 guests