Leading zero in time string

I have read many topics here but didn't find what I want, or I didn't understand it good enough.

Question is, how to add leading zero in time string?
If I have 22:10:50, it is ok. But two minutes earlier it was 22:8:50 and I need it to be 22:08:50. The same goes for seconds.

tft.print((String)hour+":"+minute+":"+second);

It will be printed on the TFT display.

Thanks in advance.

Why is this complicated? Any number less than 10, print a zero digit first. But you have to split up the print statements because it's conditional.

Or use sprintf

But the conditional is cheaper.

I`m not sure if u really need to cast to String but I would use as simple cstring like this:
code not tested but must work. Just add into appropriate sections main or loop.

#define HOUR_LENGTH 3 //2 digits+null
#define MINUTE_LENGTH 3
#define SECOND_LENGTH 3
#define TIME_LENGTH 9 //00:00:00+null
#define length 

char _hour[HOUR_LENGTH]={0};
char _minute[MINUTE_LENGTH]={0};
char _second[SECOND_LENGTH]={0};

char _time[TIME_LENGTH]={0};

void prefix(char *a);

//get your time and store in _hour,_minute_second
prefix(_hour);
prefix(_minute);
prefix(_second);

strcpy(_time,_hour);
strcat(_time,":");
strcat(_time,_minute);
strcat(_time,":");
strcat(_time,second);

print_to_somewhere(_time);

}

void prefix(char *a){
if (strlen(a) == 1 ) {
    a[1] = a[0];
    a[0] = '0';
  }
}

surepic:
I`m not sure if u really need to cast to String but I would use as simple cstring like this:
code not tested but must work. Just add into appropriate sections main or loop.

#define HOUR_LENGTH 3 //2 digits+null

#define MINUTE_LENGTH 3
#define SECOND_LENGTH 3
#define TIME_LENGTH 9 //00:00:00+null
#define length

char _hour[HOUR_LENGTH]={0};
char _minute[MINUTE_LENGTH]={0};
char _second[SECOND_LENGTH]={0};

char _time[TIME_LENGTH]={0};

void prefix(char *a);

//get your time and store in _hour,_minute_second
prefix(_hour);
prefix(_minute);
prefix(_second);

strcpy(_time,_hour);
strcat(_time,":");
strcat(_time,_minute);
strcat(_time,":");
strcat(_time,second);

print_to_somewhere(_time);

}

void prefix(char *a){
if (strlen(a) == 1 ) {
a[1] = a[0];
a[0] = ‘0’;
}
}

What about the string terminator?

If you’re going to get that complex, just use sprintf - it’ll be easier to read and already debugged.

String terminator was initialized right at the beginning.

char array={0};
which is equal to char array={'\0'};

actually I will give example right from that code:
char _hour[HOUR_LENGTH]={0}; all elements are 0 array elements are 3
hour can tak 2 elements so trailing one will always be 0 and so it can be thought of already null terminated without explicitly writing 3-rd element to 0;

prefix hour will take in current case _hour array check for strlen because its null terminated so strlen wont go over last element of array.

then swapping occurs
a[1]=a[0]
a[0]='0'//char 0 i.e. 48

automatically _hour array got updated cos of pass by reference of array.

prefix() didn't touch the 3-rd element so its still 0 and that string is null terminated.

then strcpy is kicking in with strcats and building the string inside _time and last element of which will be untouched so it will always be a string null terminated.