OK, here we go
Time
is an object of a class that contains a function called
timeStr()
which will hand you back an object of StringClass
containing the date/time stored in the Time
object in some predefined format.
But since sprintf()
(the C function that is used to do the formatting in String::format()
) does not know how to deal with String
objects but only knows C strings, you have to call on another function of StringClass
(which is "part" of the String
returned by timeStr()
) that hands you a C string, which can then be passed to sprintf()
.
Having said this String::format("%s", Time.timeStr().c_str())
is just short hand for this
// we got Time instaciated by the system
String dmyStr = Time.timeStr();
const char* dmyCStr = dmyStr.c_str();
String resultStr = String::format("%s", dmyCStr);
Another less obvious way of doing the same would be String::format("%s", (const char*)Time.timeStr())
And for @johnventions' question why this can't be done without c_str()
roots in the way how sprintf()
(or other functions with dynamic parameter list) work.
With these functions the dynamic parameters get passed into the function as void*
, hence there is no way for an implicit type cast at build time (String
does not provide an overload for a (void*)
cast operator).
The only way around this is by explicit type cast or any other way that produces a compatible data type.
And Time.zone()
is another function (method) of the TimeClass
object that acts upon (and changes) the Time
object to instruct it to hand you any value you request of it to be processed according to the set time offset.
This was already tried earlier on this thread with this result