Newbie - string concatenation with NTP library

My sketch gets UTC time with the NTP library v1.7. I want to place into a listbox a series of timestamps. The NTP library has methods to covert UTC time to something readable. I am concatenating strings.

This works:

string readable_Timestamp;

readable_Timestamp = ntp.formattedTime("%d %B %Y");
readable_Timestamp = readable_Timestamp + "   " + ntp.formattedTime("%T");
Serial.println(readable_Timestamp);

But using the string + operand , this fails:

string readable_Timestamp;

readable_Timestamp = ntp.formattedTime("%d %B %Y") + "   " +      ntp.formattedTime("%T");
Serial.println(readable_Timestamp);

I get this error: Compilation error: invalid operands of types 'const char*' and 'const char [4]' to binary 'operator+'

Why does it work splitting up the string addition into 2 steps while 1 step fails?

1 Like

please tell us which Microcontroller you want to use.
Furthermore, as you write you want to put timestamps "into a listbox" ... please explain if you are coding a webserver.

Note that string and String are two different things

What is the exact output of ntp.formattedTime() ? A string ? A String ? A const char * ?

Try

string readable_Timestamp;

readable_Timestamp = ntp.formattedTime("%d %B %Y");
readable_Timestamp += "   " ;
readable_Timestamp +=ntp.formattedTime("%T");

But may be your function supports building it in one go?

readable_Timestamp = ntp.formattedTime("%d %B %Y   %T");
1 Like

NPT library method formattedTime() returns a const char *

 const char* formattedTime(const char *format);

try casting it to string then use +

readable_Timestamp = (string)ntp.formattedTime("%d %B %Y") + "   " +      ntp.formattedTime("%T");

If NTP method formattedTime() returns a const Char*, why does this work:

readable_Timestamp = ntp.formattedTime("%d %B %Y");
readable_Timestamp = readable_Timestamp + "   " + ntp.formattedTime("%T");

I would like to understand why the compiler allows this syntax and not the complete concatenated version?

J-M-L, that worked fine but still curious why the parts could not be concatenated .

When you write

On the right side you only have char arrays or pointers to char arrays - so the compiler tries to add pointers and it does not know how to do that type of math (ie adding 5 to a char pointer makes you point 5 char further in memory but adding an array to a pointer makes no sense).

When you write

The first line initializes the string variable with a char pointer and the string class understands that you mean to convert the cString into a string.

Then with the next line, on the right side of the = sign you are adding now a string object and a c-string. The string class is equipped to do that math and will understand that as a concatenation request, so the result of the first + is also a string (temporary) and then you have another + with again a c-string and the same happens you create a new string that will be used to initialize the string that is on the left side of the =

have a look at operator-overloading where there is an example of overloading operator+ to add two complex numbers

   // overload the + operator
         friend Complex operator + (const Complex& obj1, const Complex& obj2) {
             Complex temp;
              temp.real = obj1.real + obj2.real;
              temp.img = obj1.img + obj2.img;
              return temp;
    }

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