#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
struct tm *tm_ptr,timestruct;
time_t the_time;
char buf[256];
(void) time(&the_time);
tm_ptr=localtime(&the_time);
strftime(buf,256,"%Y-%m-%d %A %d %B,%I:%S %p",tm_ptr);
printf("strftime gives: %s\n",buf);
strcpy(buf,"Sat 26 July 2003,17:53:24 will do fine");
printf("calling strptime with: %s\n",buf);
tm_ptr=×truct;
//result=strptime(buf,"%a %d %b %Y,%R",tm_ptr);
result=strptime(buf,"%a %d %b %Y,%H:%M:%S",tm_ptr);
printf("strptime consumed up to: %s\n",result);
printf("strptime gives:\n");
printf("date: %02d/%02d/%02d\n",tm_ptr->tm_year%100,tm_ptr->tm_mon+1,tm_ptr->tm_mday);
printf("time: %02d:%02d\n",tm_ptr->tm_hour,tm_ptr->tm_min);
exit(0);
}
本文介绍了一个C语言程序,该程序演示了如何使用time.h库中的函数来获取当前时间和日期,并将其格式化为字符串。此外,还展示了如何解析特定格式的时间字符串,并将其转换为时间结构。

被折叠的 条评论
为什么被折叠?



