Reputation: 5401
I tried strftime( ) to get a formatted time-stamp.
char ft[ 256 ];
struct tm *tmp;
strftime( ft, 256, "%D - %T", tmp );
My problem is that I get "13/02/60 - 03:07:-17958194" as a result. Is there a way to display the seconds properly? (I'm using Mac OS X and gcc)
Upvotes: 0
Views: 1890
Reputation: 88796
%s and %S are different formatting characters.
Upvotes: 1
Reputation: 8229
"%D - %T" should give you proper results, assuming you want "mm/dd/yy - hh:mm:ss".
If your example is accurate to your usage, you may want to consider giving the function a real time instead of some random address in memory.
Upvotes: 3
Reputation: 53310
You aren't initialising struct tm *tmp;
with anything - please could you post a complete example?
Upvotes: 0
Reputation: 3165
You're using an uninitialized value in the code that you've posted, which would explain why you have this problem. The following code results in what you're looking for:
#include <stdio.h>
#include <time.h>
int
main(int argc, char *argv[]) {
char ft[256];
struct tm *tmp;
time_t curtime;
time(&curtime);
tmp = localtime(&curtime);
strftime(ft, 256, "%D - %T %s", tmp);
printf("%s\n", ft);
return(0);
}
Then, compile and run the example:
cc -o time_ex time_ex.c
./time_ex
02/26/09 - 11:26:34 1235665594
ETA: Drop the %s if you just want the local time without UNIX time after it, of course.
Upvotes: 7