How to simplify these strftime program???

How to simplify the code below?
I got stuck with the third parameter of strftime.
Assume that the time is already set

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
char t_timedate[30];
int tc_timedate[5];

inr main()
{
   time_t seconds = time(NULL);
   strftime(t_timedate, 4,"%M", localtime(&seconds));
   tc_timedate[0] = atoi(t_timedate);
   printf("%d min\n", tc_timedate[0]);

   strftime(t_timedate, 4, "%d", localtime(&seconds));
   tc_timedate[1] = atoi(t_timedate);
   printf("%d day\n", tc_timedate[1]);

   strftime(t_timedate, 4, "%W", localtime(&seconds));
   tc_timedate[2] = atoi(t_timedate);
   printf("%d week\n", tc_timedate[2]);

   strftime(t_timedate, 4, "%m" , localtime(&seconds));
   tc_timedate[3] = atoi(t_timedate);
   printf("%d month\n", tc_timedate[3]);

   strftime(t_timedate, 4, "%Y", localtime(&seconds));
   tc_timedate[4] = atoi(t_timedate);
   printf("%d year\n", tc_timedate[4]);
}


How to simplify the program shown above to a similar program show below.
I got stuck with the third parameter of strftime!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
char t_timedate[4];
char *name[] = {"min", "day", "week", "month", "year" }; 
int tc_timedate[5];

int main()
{
   for(i = 0; i<5; i++) {
       memset(t_timedate, 0, 4);
       time_t seconds = time(NULL);
       strftime(t_timedate, 4,??????, localtime(&seconds));
       tc_timedate[i] = atoi(t_timedate);
       printf("%d %s\n", tc_timedate[i], name[i]);
   }
}
I believe it will be:
strftime(t_timedate, 4,"%Y.&m.%d %H:%M", localtime(&seconds));
It should give you date and time in format 2013.04.20 10:54

http://www.cplusplus.com/reference/ctime/strftime/
If you want to customise it
RE: MiiNiPaa

I would like the results to be

min 11
day 22
week 33
month 12
year 2345

tc_timedate[i] is required for my other function.
add:
char* frm[] = {"%M", "%d", "%W", "%m", "%Y"};
after your name array. And use
strftime(t_timedate, 4, frm[i], localtime(&seconds));
In your loop.
Thanks alot!
Last edited on
Topic archived. No new replies allowed.