Reputation: 5271
I have the following function:
string myClass::get_timestamp()
{
string res = "";
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time (&rawtime);
timeinfo = localtime (&rawtime);
// Wednesday, March 19, 2014 01:06:18 PM
strftime (buffer,80,"%A, %B %d, %Y %r",timeinfo);
res = buffer;
return res;
}
This function compiles well on Linux and Windows. However, while I'm getting the expected string running on Linux, I get an empty string running on Windows. Any idea?
Notes
timeinfo
is valid. I can access its member and verified it has the right value
buffer
is empty after strftime()
is called
Upvotes: 0
Views: 3511
Reputation: 3530
strftime is a library (not OS) function, so the behavior depends on the exact library you are linking with. [ your question does not say. ]
Not all specifiers you are using in strftime's format string are supported on all platforms because some of them are introduced with C99 and optional prior to C++11.
For example, http://www.cplusplus.com/reference/ctime/strftime/ says this:
Compatibility Particular library implementations may support additional specifiers or combinations. Those listed here are supported by the latest C and C++ standards (both published in 2011), but those in yellow were introduced in C99 (only required for C++ implementations since C++11), and may not be supported by libraries that comply with older standards.
The "yellow" C99 specifiers are: %C, %D %e %F %g %G %h %n %r %R %t %T %u %V %Z and the modifiers E and O.
Some Linux Based embedded platforms also do not support all the specifiers described as "yellow" on the web page above.
In your case, %r is one of these newer specifiers, so it is likely strftime is failing due to your particular runtime library.
You should also check the return value of strftime and see if it provides any additional data.
Accordingly, use only the older format specifiers:
// Wednesday, March 19, 2014 01:06:18 PM
strftime (buffer,80,"%A, %B %d, %Y %I:%M:%S %p",timeinfo);
Upvotes: 3
Reputation: 129504
It would appear that MS's strftime
doesn't support "%r"
, and it may be this that it is the issue. It does support "%p"
to show "AM/PM in current locale's format", so should do the same thing in most cases.
I don't have a windows machine to test this, but that's the only thing I can see that stands out.
Always check the return value, and if it's zero (and you didn't pass in an empty string to strftime
) print the value of errno
for understanding what went wrong.
Upvotes: 2