C语言中输出符合中国人习惯的当前系统时间

C函数库中的时间函数基本上都是输出符合欧美人习惯的时间格式,在网上找到一个函数,可以输出符合中国人习惯的当前系统时间,特此记录下来。

 1 #include <time.h>
 2 #include <sys/time.h>
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 #include <string.h>
 6   
 7 const char* now()
 8 {
 9     static char str[30];
10     struct tm* p_tm;
11   
12     time_t seconds = time(NULL);
13     p_tm = localtime(&seconds);
14     sprintf(str, "%d-%d-%d %d:%d:%d", p_tm->tm_year + 1900, p_tm->tm_mon + 1, p_tm->tm_mday, p_tm->tm_hour, p_tm->tm_min, p_tm->tm_sec);
15 
16     return str;
17 }

 

posted on 2013-10-22 16:04  laihaiteng  阅读(280)  评论(0)    收藏  举报

导航