声明:本文章主题来自 http://blog.sina.com.cn/s/blog_59a9e96201019o9f.html
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
int main(int argc, char *argv[])
{ struct tm *pt;
int cur_sec, cur_min, cur_hour, cur_day, cur_mouth, cur_year, cur_weekday;
char weekday[7][3] = {"日","一","二","三","四","五","六"};
time_t t = time(NULL);
pt = localtime(&t);
cur_sec = pt->tm_sec;
cur_min = pt->tm_min;
cur_hour = pt->tm_hour;
cur_day = pt->tm_mday;
cur_mouth = pt->tm_mon + 1;
cur_year = pt->tm_year + 1900;
cur_weekday = pt->tm_wday;
printf("时间: %d:%d:%d \n", cur_hour, cur_min, cur_sec);
printf("日期: %d-%d-%d \n", cur_year, cur_mouth, cur_day);
printf("%s\n",asctime(pt));
switch(cur_weekday)
{
case 0:
printf("星期: %*.*s\n",4,4, weekday[0]);
break;
case 1:
printf("星期: %*.*s\n",4,4, weekday[1]);
break;
case 2:
printf("星期: %*.*s\n",4,4, weekday[2]);
break;
case 3:
printf("星期: %*.*s\n",4,4, weekday[3]);
break;
case 4:
printf("星期: %*.*s\n",4,4, weekday[4]);
break;
case 5:
printf("星期: %*.*s\n",4,4, weekday[5]);
break;
case 6:
printf("星期: %*.*s\n",4,4, weekday[6]);
break;
default:
break;
}
return 0;
}
表头文件
#include<time.h>
定义函数
char * asctime(const struct tm * timeptr);
函数说明
asctime()将参数timeptr所指的tm结构中的信息转
换成真实世界所使用的时间日期表示方法,然后将结果以字
符串形态返回。此函数已经由时区转换成当地时间,字符串
格式为:
“Wed Jun 30 21:49:08 1993\n”
结构tm的定义为
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒
int tm_min 代表目前分数,范围0-59
int tm_hour 从午夜算起的时数,范围为0-23
int tm_mday 目前月份的日数,范围01-31
int tm_mon 代表目前月份,从一月算起,范围从0-11
int tm_year 从1900 年算起至今的年数
int tm_wday 一星期的日数,从星期一算起,范围为0-6
int tm_yday 从今年1月1日算起至今的天数,范围为0-365
int tm_isdst 日光节约时间的旗标
此函数返回的时间日期未经时区转换,而是UTC时间。
返回值 返回结构tm代表目前UTC 时间