#include <time.h>
char *strptime(const char *buf, const char *format, struct tm *timeptr);
01 #include <time.h>
02 #include <stdio.h>
03 #include <stdlib.h>
04 #include <string.h>
05
06 int main()
07 {
08 struct tm *tm_ptr, timestruct;
09 time_t the_time;
10 char buf[256];
11 char *result;
12
13 (void)time(&the_time);
14 tm_ptr = localtime(&the_time);
15 strftime(buf, 256, "%A %d %B, %I:%S %p", tm_ptr);
16
17 printf("strftime gives: %s\n", buf);
18
19 strcpy(buf,"Thu 26 July 2007, 17:53 will do fine");
20
21 printf("calling strptime with: %s\n", buf);
22 tm_ptr = ×truct;
23 /*----------------strptime()应用------------------------------*/
24 result = strptime(buf, "%a %d %b %Y, %R", tm_ptr);
25 printf("strptime consumed up to: %s\n", result);
26
27 printf("strptime gives:\n");
28 printf("date: %02d/%02d/%02d\n",
29 tm_ptr->tm_year % 100,
30 tm_ptr->tm_mon+1,
31 tm_ptr->tm_mday);
32
33 printf("time: %02d:%02d\n",tm_ptr->tm_hour, tm_ptr->tm_min);
34
35 exit(0);
36 }
[root@localhost C]# ./strftime.exe
strftime gives: Saturday 21 February, 12:05 PM
calling strptime with: Thu 26 July 2007, 17:53 will do fine
strptime consumed up to: will do fine
strptime gives:
date: 07/07/26
time: 17:53
strftime.c: In function ‘main’:
strftime.c:26: warning: assignment makes pointer from integer without a cast
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |