Linux c++(獲取時間)


1.獲取系統時間函數

#include <ctime>
 
using namespace std;
 
int main( )
{
   // 基於當前系統的當前日期/時間
   time_t now = time(0);
   
   // 把 now 轉換為字符串形式
   char* dt = ctime(&now);
 
   cout << "本地日期和時間:" << dt << endl;
 
   // 把 now 轉換為 tm 結構
   tm *gmtm = gmtime(&now);
   dt = asctime(gmtm);
   cout << "UTC 日期和時間:"<< dt << endl;
}

2.獲取系統時間函數

#include <time.h>
time_t timer = time(NULL);
struct tm *localtm = localtime(&timer);
struct tm {
    int tm_sec;  /* 秒 – 取值區間為[0,59] */
    int tm_min;  /* 分 - 取值區間為[0,59] */
    int tm_hour;  /* 時 - 取值區間為[0,23] */
    int tm_mday;  /* 一個月中的日期 - 取值區間為[1,31] */
    int tm_mon;  /* 月份(從一月開始,0代表一月) - 取值區間為[0,11] */
    int tm_year;  /* 年份,其值等於實際年份減去1900 */
    int tm_wday; /* 星期 – 取值區間為[0,6],其中0代表星期天,1代表星期一 */
    int tm_yday; /* 從每年1月1日開始的天數– 取值區間[0,365],其中0代表1月1日 */
    int tm_isdst; /* 夏令時標識符,夏令時tm_isdst為正;不實行夏令時tm_isdst為0 */
  };


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM