I'm trying to write a function that will return a formatted time for output to serial, LCD, or webserver. I created a function to return a pointer to a char array with a formated time in it. I am having trouble getting snprintf_P to do my bidding when I use it in the function call. I can make it work if I create a char array in the function, but I want it to work by passing a pointer to a char array and have the function return a pointer to the same char array. I know that sounds confusing. Probably simpler to see my example code below.
The example code below returns:
5
5:40:01 PM Oct 22, 2012
I expect it to return:
5:40:01 PM Oct 22, 2012
5:40:01 PM Oct 22, 2012
Any ideas why snprintf_P doesn't work as I expect it to in the first function call?
#include <Time.h>
//****************************************************************
void setup() {
Serial.begin(115200);
setTime(17,40,1,22,10,2012); //setTime(hr,min,sec,day,mnth,yr)
char timebuf[30];
time_t tnow = now();
Serial.println(format_tA(timebuf,tnow));
Serial.println(format_tB(timebuf,tnow));
}
//****************************************************************
char* format_tA(char* t_bufA, time_t tA){
snprintf_P(t_bufA,sizeof(t_bufA),PSTR("%d:%02d:%02d %s %s %d, %4d"),
hourFormat12(tA),minute(tA),second(tA),(isPM(tA) ? "PM" : "AM"),
monthShortStr(month(tA)),day(tA),year(tA));
return t_bufA;
}
//****************************************************************
char* format_tB(char* t_bufB, time_t tB){
char temp[30];
snprintf_P(temp,sizeof(temp),PSTR("%d:%02d:%02d %s %s %d, %4d"),
hourFormat12(tB),minute(tB),second(tB),(isPM(tB) ? "PM" : "AM"),
monthShortStr(month(tB)),day(tB),year(tB));
return strcpy(t_bufB,temp);
}
void loop() {
}