The question is simple "Say we have an integer 1 <= n <= 12,How to use strftime
to display January for '1',February for '2',March for '3' and so on ... ?"
views:
99answers:
2
+2
A:
struct tm tm = {0};
tm.tm_mon = n - 1;
strftime(s, len, "%B", &tm);
Laurence Gonsalves
2010-02-21 06:35:32
`tm.tm_mon = n-1;` /* range is 0 .. 11 */
wallyk
2010-02-21 06:41:03
@wallyk Thanks. Fixed.
Laurence Gonsalves
2010-02-21 06:43:16
My compiler is showing : "request for member `tm_mon' in something not a structure or union"
nthrgeek
2010-02-21 06:55:35
@nth: Which compiler? On which OS?
KennyTM
2010-02-21 06:58:47
@nthrgeek: That's a common error if you have a pointer and used `.` instead of `->`. (The code in this answer will work.)
Roger Pate
2010-02-21 07:28:23
+5
A:
#include <stdio.h>
#include <time.h>
size_t monthName( char* buf, size_t size, int month)
{
struct tm t = {0};
t.tm_mon = month - 1; // turn month 1..12 to 0..11 as `struct tm` wants
return strftime( buf, size, "%B", &t);
}
int main(int argc, char* argv[])
{
char buf[10];
monthName( buf, sizeof( buf), 9);
printf( "%s\n", buf);
return 0;
}
Michael Burr
2010-02-21 06:55:58