想要得到系统时间需要调用Linux系统函数获取。
#include
#include
/// 系统时间结构
typedef struct SYSTEM_TIME
{
int year;///< 年。
int month;///< 月,January = 1, February = 2, and so on.
int day;///< 日。
int wday;///< 星期,Sunday = 0, Monday = 1, and so on
int hour;///< 时。
int minute;///< 分。
int second;///< 秒。
int isdst;///< 夏令时标识。
}SYSTEM_TIME;
int SystemGetCurrentTime(SYSTEM_TIME *ptime)
{
time_t t = time(NULL);
struct tm time;
localtime_r(&t, &time);
ptime->second = time.tm_sec;
ptime->minute = time.tm_min;
ptime->hour = time.tm_hour;
ptime->wday = time.tm_wday;
ptime->day = time.tm_mday;
ptime->month = time.tm_mon + 1;
ptime->year = time.tm_year + 1900;
ptime->isdst = 0;
return 0;
}
int main(int argc, char **argv)
{
SYSTEM_TIME myTime;
SystemGetCurrentTime(&myTime);
printf("%d-%d-%d %d:%d:%d weekday:%d dst:%d\n", myTime.year, myTime.month, myTime.day, myTime.hour, myTime.minute, myTime.second, myTime.wday, myTime.isdst);
return 0;
}