Arduino example: setting TZ env variable and getLocalTime().
Posted: Fri Sep 15, 2017 5:21 pm
Simple example showing TZ vars and sntp setup.
Code: Select all
#include "apps/sntp/sntp.h"
#include "WiFi.h"
struct tm timeinfo;
const String timeStr[7]= {
"EST-10EDT-11,M10.5.0/02:00:00,M3.5.0/03:00:00",
"CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00",
"GMT+0BST-1,M3.5.0/01:00:00,M10.5.0/02:00:00",
"WET-0WEST-1,M3.5.0/01:00:00,M10.5.0/02:00:00",
"MSK-3MSD,M3.5.0/2,M10.5.0/3",
"HAW10",
"MST7",
};
//TZ strings can be found at http://www.remotemonitoringsystems.ca/time-zone-abbreviations.php
const char wifissid[] = "your-network-ssid";
const char wifipsk[] = "your-network-psk";
void setup() {
Serial.begin(115200);
Serial.println( "\n\nSetting TZ environment variable example" );
Serial.println( "\nConnecting WiFi" );
WiFi.begin( wifissid, wifipsk );
while (WiFi.status() != WL_CONNECTED ) {
Serial.print(".");
delay(500);
}
Serial.println( "\nGetting NTP time...\n" );
Serial.println( "Getting time from pool.ntp.org\n\n" );
configTzTime( "", "pool.ntp.org" );
Serial.println( "Localtime with no TZ set..." );
getLocalTime( &timeinfo );
Serial.println( asctime( &timeinfo ) );
delay( 3000 );
}
void loop() {
Serial.println( "Now some random TZ setting examples...\n\n" );
for ( int count = 0; count < 7; count++)
{
setenv( "TZ", timeStr[count].c_str(), 1 );
getLocalTime( &timeinfo );
Serial.println( "TZ: " + timeStr[count] );
Serial.println( asctime( &timeinfo ) );
delay(3000);
}
}