1. 程式人生 > >獲取系統時間資訊

獲取系統時間資訊

struct tm結構體   需include<time.h>

struct tm
{
    int tm_sec;   // seconds after the minute - [0, 60] including leap second
    int tm_min;   // minutes after the hour - [0, 59]
    int tm_hour;  // hours since midnight - [0, 23]
    int tm_mday;  // day of the month - [1, 31]
    int tm_mon;   // months since January - [0, 11]
    int tm_year;  // years since 1900
    int tm_wday;  // days since Sunday - [0, 6]
    int tm_yday;  // days since January 1 - [0, 365]
    int tm_isdst; // daylight savings time flag
};

localtime, localtime_s, localtime_r

localtime不安全------Windows

原型   struct tm* localtime(_In_ time_t const* const _Time )

#include <time.h>

time_t t = time(nullptr);
struct tm* nowTime;
nowTime = localtime(&t);

cout << nowTime->tm_hour << ":" << nowTime->tm_min << ":" << nowTime->tm_sec << endl;

localtime_s安全------Windows

原型   errno_t localtime_s(_Out_ struct tm* const _Tm, _In_ time_t const* const _Time)

#include <time.h>

time_t t = time(nullptr);
struct tm nowTime;
localtime_s(&nowTime, &t);

cout << nowTime.tm_hour << ":" << nowTime.tm_min << ":" << nowTime.tm_sec << endl;