How to get time from structure

Status
Not open for further replies.

Wajiduddaim

Junior Member level 1
Joined
May 12, 2011
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
darbar-e-Sadria
Activity points
1,464
hi
i m using esp32 and its wifi library in Arduino

After getting NTP time

when i Call this function everything fine

C++:
void printLocalTime()
{
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo))
  {
    Serial.println("Failed to obtain time");
    TimeSuccess = false;
    return;
  }
  TimeSuccess = true;

  Serial.println(&timeinfo, "%A, %d %B %Y %H:%M:%S");
}

i want to get time separately like

LocalHour = timeinfo's Hours value
ect.

i tried some methods but they didnt work like
Localhour = timeinfo.tm_hour;

the result is 0 (Zero)

how can i get values
 
Last edited:

It depends which library you are using but your code shows nothing to load the "%A...%S" values.
Look at the structure elements in "tm" then try printing them individually, for example try:
Serial.print(timeinfo.hour);
to see if the hour value is shown. You may have to use different names, they will be defined in the library you are using.

Brian.
 

Hi,

if
Serial.println(&timeinfo, "%A, %d %B %Y %H:%M:%S");
works...then what does
Serial.println(&timeinfo, "%H");
do?

Klaus
 

thanks for your reply
It depends which library you are using
i am using TimeLib.h library and wifi library

Serial.print(timeinfo.hour);
i tried but it didnt work


if
Serial.println(&timeinfo, "%A, %d %B %Y %H:%M:%S");
works...then what does
Serial.println(&timeinfo, "%H");
do?
yes its work and print hour value
but i need to compare hour with another value

suppose tm_hour = timeinfo.hour

if(tm_hour == LocalHour)
.....
or
if(tm_hour <= 12)
 

Maybe %H is a string or String. Arduino has some strange variable types!
If what Klaus suggested doesn't work, try "if(%H.toInt() == LocalHour)".
You can always use sprintf but at the expense of needing lots of memory.

Brian.
 

Looking at the code in initial post #1, I have several questions

1. I don't see struct tm defined in TimeLib.h
tm is a static struct in Time.cpp
2. where is getLocalTime() defined, what's the argument type?
3. where are the format specifiers used in "%A, %d %B %Y %H:%M:%S" defined or explained? Only %d is standard C syntax.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…