c++如何获取当前系统时间
希望大神给出程序给出上面每个变量的值
或者说如何换算time(0) 展开
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "\007The current date/time is: %s", asctime (timeinfo) );
//exit(0);
int year,month,day,hour,minute,second;
year = 1900+timeinfo->tm_year; //年
month = 1+timeinfo->tm_mon;//月
day = timeinfo->tm_mday; //日
hour = timeinfo->tm_hour; //时
minute = timeinfo->tm_min; //分
second = timeinfo->tm_sec; //秒
printf("%d-%d-%d %d:%d:%d\n",year,month,day,hour,minute,second);
}
请问我用time_t now_time=time(0)获取系统时间,取得的格式是1401435919这样的,请问能给出方法也换算成上面六个变量的值吗?万分感谢
给个好评吧,程序和截图如下:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void main ()
{
time_t now_time=time(0); //这是你要这样写的,其实差不多
struct tm * timeinfo;
timeinfo = localtime ( &now_time );
printf("%s\n",asctime (timeinfo));
int year,month,day,hour,minute,second;
year = 1900+timeinfo->tm_year; //年
month = 1+timeinfo->tm_mon;//月
day = timeinfo->tm_mday; //日
hour = timeinfo->tm_hour; //时
minute = timeinfo->tm_min; //分
second = timeinfo->tm_sec; //秒
printf("%d-%02d-%d %d:%d:%d\n",year,month,day,hour,minute,second);
}
time_t t=time(0);//系统时间
strftime(tmp,sizeof(tmp),"%Y-%m-%d %X",localtime(&t)); //按年-月-日 时:分:秒保存在tmp中