魅族mx6好还是vivox7好:C语言获取系统当前时间

来源:百度文库 编辑:九乡新闻网 时间:2024/10/02 20:30:00
C语言获取系统当前时间2009年01月15日 星期四 17:10#include
#include

int main ()
{
    time_t rawtime;
    struct tm * timeinfo;

    time(&rawtime); /* get the current time */
    timeinfo = localtime(&rawtime);

    printf("The current time is: %s\n", asctime(timeinfo));
    printf("%4d-%02d-%02d %02d:%02d:%02d\n",
            1900+timeinfo->tm_year, /* year */
            1+timeinfo->tm_mon, /* month */
            timeinfo->tm_mday, /* date */
            timeinfo->tm_hour, /* hour */
            timeinfo->tm_min, /* minute */
            timeinfo->tm_sec); /* second */

    return 0;
}

#include -- 必须的时间函数头文件
time_t -- 时间类型(time.h 定义)
struct tm -- 时间结构,time.h 定义如下:
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;

time ( &rawtime ); -- 获取时间,以秒计,从1970年1月一日起算,存于rawtime
localtime ( &rawtime ); -- 转为当地时间,tm 时间结构
asctime ()-- 转为标准ASCII时间格式:
星期 月 日 时:分:秒 年

就是直接打印tm,tm_year 从1900年计算,所以要加1900,
月tm_mon,从0计算,所以要加1
其它你一目了然啦。