您的位置:首页 > 编程语言 > C语言/C++

C时间函数

2015-08-27 14:54 323 查看
C的几个时间函数,写一段小代码,把其全部囊括。

#include <stdio.h>
#include <time.h>
#include <assert.h>

int main()
{
time_t now = time(NULL);
printf("now utc is %d\n", now);

struct tm* gtmie = gmtime(&now);
struct tm* ctmie = localtime(&now);
time_t now2 = mktime(gtmie );
time_t now3 = mktime(ctmie );
assert (now==now2);
assert (now==now3);

printf("Time now is %s\n", asctime(gtmie));
printf("Time now is %s\n", asctime(ctmie));
printf("Time now is %s\n", ctime(&now));

char buf[124];
size_t len;

len = strftime(buf,124,"%Y-%m-%d %H:%M:%S %Z",gtmie);
printf("Time now is %s\n",buf);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言