getLocalTime
#include <WiFi>


Initial setup loop printLocalTime


/*H*******************************************************
Rui Santos
Complete project details at: 
https://RandomNerdTutorials.com/esp32-date-time-ntp-client-server-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
********************************************************/
#include <WiFi.h>
#include "time.h"

//************************* DEFINES ************************************
#define  BAUD  9600
//************************* VARIABLES ************************************
const char* ssid     = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 0;

//************************* PROTOTYPES ************************************
void printLocalTime();

const int   daylightOffset_sec = 3600;

/*F********************************************************************
*
**********************************************************************/
void 
setup()
{
	Serial.begin( BAUD );
	// Connect to Wi-Fi
	Serial.print( "Connecting to " );
	Serial.println( ssid );
	WiFi.begin( ssid, password );
	while( WiFi.status() != WL_CONNECTED ) 
	{
		delay( 500 );
		Serial.print( "." );
	}
	Serial.println( "" );
	Serial.println( "WiFi connected." );
	// INIT AND GET TIME
	configTime( gmtOffset_sec, daylightOffset_sec, ntpServer );
	printLocalTime();
//	WiFi.disconnect( true );    // DISCONNECT WiFi AS IT'S NO LONGER NEEDED
//	WiFi.mode( WIFI_OFF );
}
/*F********************************************************************
*
**********************************************************************/
void 
loop()
{
	delay( 1000 );
	printLocalTime();
}
/*F********************************************************************
*
**********************************************************************/
void 
printLocalTime()
{
	char timeHour[3], timeWeekDay[10];
	struct tm timeinfo;

	if( !getLocalTime( &timeinfo ) )
	{
		Serial.println( "Failed to obtain time" );
		return;
	}
	Serial.println( &timeinfo, "%A, %B %d %Y %H:%M:%S" );
	Serial.print( "Day of week: " );
	Serial.println( &timeinfo, "%A" );
	Serial.print( "Month: " );
	Serial.println( &timeinfo, "%B" );
	Serial.print( "Day of Month: " );
	Serial.println( &timeinfo, "%d" );
	Serial.print( "Year: " );
	Serial.println( &timeinfo, "%Y" );
	Serial.print( "Hour: " );
	Serial.println( &timeinfo, "%H" );
	Serial.print( "Hour ( 12 hour format ): " );
	Serial.println( &timeinfo, "%I" );
	Serial.print( "Minute: " );
	Serial.println( &timeinfo, "%M" );
	Serial.print( "Second: " );
	Serial.println( &timeinfo, "%S" );
	Serial.println( "Time variables" );
	strftime( timeHour,3, "%H", &timeinfo );
	Serial.println( timeHour );
	strftime( timeWeekDay,10, "%A", &timeinfo );
	Serial.println( timeWeekDay );
	Serial.println();
}