Minute time display problem, please help


I got a time code that shows time:

int get_time() {
  time_t now;
  char time_output[30];
  char minute_output[30];
  strftime(time_output, 30, "%H", localtime(&now));
  strftime(minute_output, 30, "%M", localtime(&now));
  currMinute = atoi(minute_output);
  return atoi(time_output);

the problem is that if I use the minute function on my LCD


it writes the minute without zero in front, like 18:4 instead of 18:04
and I need to use exactly 5 characters for time.

How can I make the code to show minutes with 0 in front, like 18:04 instead of 18:4?

You take the currMinute variable and if it is lower than 10 you print the char ‘0’ to the display before you print the currMinute variable.

Hello Grumpy_Mike, can you show me how can I do that?
I'm a beginner with Arduino and don't have a any coding knowledge.

Like this?

if (currMinute <10) {

Something like:

if( currMinute < 10 )
    lcd.print( "0" );


Thank you!

If you are using a non Arduino.cc board/core it likely has printf() support built into the Print class as nearly all non Arduino.cc boards come with that support.

So if you have say an ESPxxx board you can then do real output formatting like

lcd.printf("%02d:%02d", hour, minute);

You don't need to jump through all the hoops to get the local hour and minute.
You also need to be using the other standard time functions to get the time to ensure you get the time properly.
Most importantly the way you are doing things is not atomic. i.e. it is possible that the time values you get are incorrect and off by an hour. For example should the time be just before an hour change. You might get the hour before the rollover and the minute after.
i.e. suppose the time is 10:59 (but nearly 11:00) you get the hour (10) and then the time changes to 11:00 and get the minute as (00)
So now you just grabbed the time as 10:00 when it was actually 10:59

You need to be using the localtime() function to get a struct tm structure which has all the broken down time elements in local time. You can then grab them and use them as needed. You can even use some of the other time functions to format the output.
If you are on a machine running a real os you can look at the man pages for these API functions. i.e. ctime, asctime, localtime, mktime, localtime_r etc...

Otherwise you can google for them.
But if on an ESP core, they have all the documentation online.

--- bill

Thank you!

Please read the code before spouting rubbish. Typical software engineers attitude seeing problems where none exist.

The code takes a reading of the time in milliseconds and then breaks that time into its component parts. So your supposed scenario can NEVER exist. With sloppy code that situation could exist, but it is not what we have here, and it is not something that a real embedded engineer would do any way.

The big trouble when you get software engineers venturing into real time is they simply do not understand what they are doing.

What a hoop is a single if statement? Compared to the complexity of having the code to implement a printf is way way over the top. Software engineers like you have no concept of memory usage. I used to manage people like you, who thought like you and they were always a complete nightmare to manage. They were more interested in doing things “correctly” as they saw it while making bolted code that eventually would not fit into the memory available and ran so slow it was embarrassing.

Ok, I did overlook that the strings were being generated from a time_t so true. My fault I didn't look close enough.
I was very wrong on this part and atomicity issues
You are correct there is no atomicity issue given both are generated from the same time_t value.

Huh? The strftime() function has formatting similar to xxprintf() and in fact if you go look at the glibc source code (which I assume is what they pulled into the ESP32 library) you will see that it actually calls sprintf() to do some of the formatting. It also contains some static strings for date/time things so in effect it is possible that it is more code/data space than if xxprintf() functions were called directly with members of the tm struct.

The "hoops" I'm twas talking about is the use of calling something that does fancy character formatting into a buffer (which then calls xxprintf() functions) and then calling atoi() to convert it back to a number.

Your rant is over the top and is nothing but a clueless personal attack.
IMO, you should get a timeout for this.

You know nothing about me.
You are very very wrong on the assumptions you have made about me.
Just for you, Here is a bit of background on me.
I have MANY decades of s/w & h/w experience going back to the 70's.
I am very familiar with all kinds of real world tradeoffs. Things like code size vs speed, time to market, development time costs vs maintenance vs support costs.
I have done many projects in assembler including on 4 bit systems where you had to hand assemble the code, count instruction cycles and memory was VERY limited. Environments were if the code was too big it wouldn't fit in a part and the project would be dead, or if the code wasn't fast enough to meet a time critical window, bad things would happen like the disk platter could get its format trashed or a display would burn up.
So yes I'm very familiar with the difference between what might be the "ideal" way of doing things and then the reality of how things sometimes may have to be done to meet certain real world requirements or necessities.

I've done real time coding on things such as high speed disk controllers which was implemented part in h/w and part in s/w (to keep costs down) where you couldn't even use C since the realtime interrupt needs were so tight that the push multiple instruction used by C function prologue and epilogues would mask interrupts long since they were not interruptible.
Environments were cost margins are so slim you have push to get code to fit into smaller lower cost parts to increase margins or even to just be viable with any profit.
I have done h/w design including ASIC design, did some work on gcc in the very early days of gcc on the 68k compiler and later the 29k compilers.
I did unix kernel level development for various companies in the early days of SUN, MIPs, and Silicon Graphics.
I have code in products that went to space on the shuttle as part of its controls, other projects/products that were used in super computers used by Cray and Silicon Graphics, and Tandem Fault tolerant systems. Systems that had to have 100% uptime and controlled things like American Airlines SABER, travelocity, and many financial systems.
In the early 90's I started a company that grew from 5 employees to over 1000 and became the #1 DSL provider of customer equipment on the planet to pretty much every telco around the world.
When it went public in 1999 it was the biggest IPO day stock price mover in NASDAQ history.

So be careful with your comments.
In this case you were way way wrong.

Yes, I have also managed the types of people you speak of.
But those types were not that bad to manage and keep under control.
There are others that were much worse. Those that would choose to make changes and force them into the repository outside of established procedures. when it was not permitted.
Things like making changes as a release is finalizing and testing is nearing completion.
At one point I had to seize control of a repository undo commits and lock a person out of the repository for while.

--- bill

A bit faster (1 char vs. array) [and I prefer keeping it on 1 line]

if (currMinute <10) lcd.print('0');

I have begun to be in the habit of using sprintf() to a text buffer and then printing it. I do that because it's easier (at least from my perspective) to make running changes while prototyping. It combines the number conversion with the fixed test fields, like "temp=" or whatever, all in one line.

OK sorry for the assumption I made, I was wrong about you.

Apology accepted.

Wouldn't you agree that getting the hour and minute by going from a time_t to a character string formatted by strftime() to get the local time hour and minute as an ascii character string saved into a local buffer and then converting those strings to a number using atoi() on the buffer rather than calling localtime() and using the struct tm fields is jumping through some hoops?

--- bill

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