Extracting integer out of "tm timeinfo"

Hello, I am looking to get some help in my task.

Here I have a piece of code:

void timeupdate() {
  struct tm timeinfo;
  int timeToDisplay = tm_hour * 100 + tm_min ;
}

Id like to get an integer showing the time in format HHMM from data gathered from NTP time servers.

I am successful in obtaining the time (&timeinfo, "%A, %B %d %Y %H:%M:%S"); but need to do some manipulation and have it in HHMM format.

This code, however, doesnt work. How to fix this?

You’re RE-declaring tm as local to the function, which will ignore your global tm,

Delete this line, and if you’ve declared and filled time info as a global, it should ‘just work’

There are many time libraries and examples of how to code to extract the time in several formats. Take a few minutes to do some research first.

Hi, @rafski07x
Welcome to the forum.

What clock library are you using?
Have you looked at the Examples that come with the library?

Tom.. :smiley: :+1: :coffee: :australia:

how do you do this ?

assuming you have timeinfo properly setup, the structure looks like this (give or take)

struct tm {
	int	tm_sec;		/* seconds after the minute [0-60] */
	int	tm_min;		/* minutes after the hour [0-59] */
	int	tm_hour;	/* hours since midnight [0-23] */
	int	tm_mday;	/* day of the month [1-31] */
	int	tm_mon;		/* months since January [0-11] */
	int	tm_year;	/* years since 1900 */
	int	tm_wday;	/* days since Sunday [0-6] */
	int	tm_yday;	/* days since January 1 [0-365] */
	int	tm_isdst;	/* Daylight Saving Time flag */
	long tm_gmtoff;	/* offset from UTC in seconds */
	char *tm_zone;	/* timezone abbreviation */
};

So if timeinfo is the instance of the struct and is a global variable, then you would do

inline int getHHMM() {
  return 100 * timeinfo.tm_hour  + timeinfo.tm_min;
}

and when you want to use it, you do

int timeToDisplay = getHHMM() ;

ideally you would not use a global variable and would pass the structure to the function.

Thanks for your help.

Deleting this line (struct tm timeinfo;) gives me an error:

'timeinfo' was not declared in this scope.

Possibly, this is because (struct tm timeinfo;) used in the code is inside another function.

Indeed. If you want to share data between functions, you must either make it a global variable, or pass it as a parameter from one function to the other.

Two functions with local variables, which happen to have the same name, is not sharing data,

This is beginner level stuff. What is your level of experience?

Thanks for your help.

I was not able to use inline int function, cause this function is trigged elsewhere in the code.
I managed to resolve the problem partially by using global struct tm timeinfo.

My code looks like this:

void timeupdate() {
int hours = timeinfo.tm_hour  ;
int minutes = timeinfo.tm_min ;
Serial.println("You have just updated time from NTP!");
Serial.println(hours);          // print an updated time
Serial.println(minutes);          // print an updated time
}

However, the integers hours and minutes are global integers with values defined at the start of the code. After that their values increase with microcontroller time.

Problem is, updating the values of hours and minutes by my function timeupdate() does not result in changing the current value of hours and minutes.

Serial monitor shows the updated values, but on 7 segment display they do not change.

How to resolve this?

I am using "time.h".

I have used also SimpleTime example and few others, I think my problem is related not to this particular library but the knowledge how the microcontroller processes information.
I have resolved my problem partially by defining a global struct tm timeinfo, just now I try to update initial values of integers by new values, obtained by my function timeupdate - and it seems to be not so obvious.

Any help will be appreciated.

at this point you'll need to post all your code to get help...

it's time to read about scope

You are creating hours and minutes as local variables inside the timeupdate() funtion. The global hours and minutes variables will be unaffected by any data stored in the local variables. Remove the "int" before hours and minutes to use the global variables in the function.

1 Like

i'm doing the following to convert secs to a date string.

do you just want

    strftime (str, sizeof(str), "%H%M", tm);
char *
getTime (
    unsigned long sec )
{
    time_t  time (sec ? sec : sec0 + secCnt);

    static char  str [20];
    struct tm   *tm = localtime (& time);
    strftime (str, sizeof(str), "%y/%m/%d %H:%M", tm);

    return str;
}

Thank you david_2018.
This solution works.
So easy when you have knowledge and experience.
Many thanks. I owe you a beer. :beers:

Thank you Jackson, all good now.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.