ESP32 setenv()
#include <>
From: https://github.com/G6EJD/ESP32-Time-Services-and-SETENV-variable
ESP32-Time-Services-and-SETENV-variable
How to get time and use SETENV variable for Time Zones and DST
See:
https://github.com/espressif/esp-idf/blob/master/examples/protocols/sntp/README.md
https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
Time-zones: https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
GMTGMT-1, M3.4.0/1,M10.4.0/2 GMTGMT-1 is the name of the time zone GMT-1 is
the abbreviation used when DST is off 1 hour is the time difference from GMT
GMT is the abbreviation used when DST is on ,M3 is the third month .4 is the
forth occurrence of the day in the month when DST starts .0 is Sunday /1 is
the time in hours when DST starts ,M10 is the tenth month .4 is the forth
occurrence of the day in the month when DST ends .0 is Sunday /2 is the time
in hours when DST ends
// Set timezone to British DST setenv("TZ","GMTGMT-1,M3.4.0/01,M10.4.0/02",1);
See: https://www.timeanddate.com/time/zones/ for your country code
Example
/*H********************************************************************
*
**********************************************************************/
#include <WiFi.h>
#include "time.h"
//************************* DEFINES ************************************
//************************* PROTOTYPES ************************************
String printLocalTime();
int Start_WiFi( const char* ssid, const char* password);
//************************* VARIABLES ************************************
String time_str;
const char* ssid = "yourSSID";
const char* password = "yourPASSWORD";
/*F********************************************************************
*
**********************************************************************/
void
setup()
{
Serial.begin( BAUD );
Start_WiFi( ssid, password );
configTime( 0, 0, "pool.ntp.org");
}
/*F********************************************************************
*
**********************************************************************/
void
loop()
{
//See: https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
// CST6CDT, M3.2.0/2:00:00, M11.1.0/2:00:00
// CST6CDT is the name of the time zone
// CST is the abbreviation used when DST is off
// 6 hours is the time difference from GMT
// CDT is the abbreviation used when DST is on
// ,M3 is the third month
// .2 is the second occurrence of the day in the month
// .0 is Sunday
// /2:00:00 is the time
// ,M11 is the eleventh month
// .1 is the first occurrence of the day in the month
// .0 is Sunday
// 2:00:00 is the time
// Set timezone to British DST
// must include '0' after first designator e.g. GMT0GMT-1, ',1' is true or ON
setenv( "TZ", "GMT0BST,M3.5.0/01,M10.5.0/02",1);
Serial.println( " UK Time = "+printLocalTime());
// Set timezone to Eastern Standard Time
setenv( "TZ", "EST5EDT,M3.2.0/2,M11.1.0", 1);
Serial.println( "US (EST) Time = "+printLocalTime());
// Set timezone to Indian Standard Time
setenv( "TZ", "UTC-05:30", 1);
Serial.println( " INDIAN Time = "+printLocalTime());
// Set timezone to China Standard Time
setenv( "TZ", "CST-8CDT-8,M4.2.0/2,M9.2.0/3", 1);
Serial.println( " CHINA Time = "+printLocalTime());
Serial.println();
delay( 2000 );
}
/*F********************************************************************
*
**********************************************************************/
String
printLocalTime()
{
struct tm timeinfo;
if( !getLocalTime( &timeinfo ) )
{
Serial.println( "Failed to obtain time");
return( "Time Error" );
}
//See http://www.cplusplus.com/reference/ctime/strftime/
char output[80];
strftime( output, 80, "%d-%b-%y, %H:%M:%S", &timeinfo);
time_str = String( output );
return( String( output ));
}
/*F********************************************************************
*
**********************************************************************/
int
Start_WiFi( const char* ssid, const char* password)
{
int connAttempts = 0;
Serial.println( "\r\nConnecting to: "+String(ssid));
WiFi.begin( ssid, password );
while( WiFi.status() != WL_CONNECTED )
{
delay( 500 );
Serial.print( ".");
if( connAttempts > 20)
return( -5 );
connAttempts++;
}
return( 1 );
}