C++ Get Current Time 獲取當前時間


 

在做項目中,我們經常需要獲取系統的當前時間,那么如何獲取呢,參見下面的代碼:

 

/* asctime example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, time, localtime, asctime */

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

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "The current date/time is: %s", asctime (timeinfo) );

  return 0;
}

 

輸出結果為:

The current date/time is:: Thu Feb 26 11:40:19 2015

 

但是,有些情況下我們想分別獲得年月日時分秒的值,而不是像這樣得到一個整個的字符串,那么既然時間信息都存在了一個結構體timeinfo中,那么我們就先來看看這個結構體的定義吧:

 

struct tm {
  int tm_sec;   // seconds of minutes from 0 to 61
  int tm_min;   // minutes of hour from 0 to 59
  int tm_hour;  // hours of day from 0 to 24
  int tm_mday;  // day of month from 1 to 31
  int tm_mon;   // month of year from 0 to 11
  int tm_year;  // year since 1900
  int tm_wday;  // days since sunday
  int tm_yday;  // days since January 1st
  int tm_isdst; // hours of daylight savings time
}

 

好了,看完了結構體的定義,我們再來看看asctime()函數是如何取出想要的數據,並加以轉換的:

 

char* asctime(const struct tm *timeptr)
{
  static const char wday_name[][4] = {
    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  };
  static const char mon_name[][4] = {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  };
  static char result[26];
  sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
    wday_name[timeptr->tm_wday],
    mon_name[timeptr->tm_mon],
    timeptr->tm_mday, timeptr->tm_hour,
    timeptr->tm_min, timeptr->tm_sec,
    1900 + timeptr->tm_year);
  return result;
}

 

如果我們直接將timeinfo結構體中的年月日時分秒打印出來,會得到:

115 1 26 11 46 37

 

而我們需要的是:

2015 2 26 11 46 37

 

仔細觀察asctime()函數,我們可以發現年的保存方式是自1900年之后,所以要+1900,月是把名稱簡寫存入了一個數組,數組從0開始,所以要得到月的數字,需要+1,后面的日時分秒沒有變化,可以直接使用。

 

若想獲得類似20150226114637這樣的年月日時分秒的字符串,可以用下面的函數:

 

std::string getTimeStamp() {
    time_t rawtime;
    struct tm * timeinfo;
    time( &rawtime );
    timeinfo = localtime( &rawtime );
    char year[5], mon[3], mday[3], hour[3], minute[3], sec[3];
    sprintf(year, "%d", timeinfo->tm_year + 1900);
    sprintf(mon, "%d", timeinfo->tm_mon + 1);
    sprintf(mday, "%d", timeinfo->tm_mday);
    sprintf(hour, "%d", timeinfo->tm_hour);
    sprintf(minute, "%d", timeinfo->tm_min);
    sprintf(sec, "%d", timeinfo->tm_sec);

    std::string yearStr = std::string(year);
    std::string monStr = std::string(mon);
    if (monStr.size() == 1) monStr = "0" + monStr;
    std::string mdayStr = std::string(mday);
    if (mdayStr.size() == 1) mdayStr = "0" + mdayStr;
    std::string hourStr = std::string(hour);
    if (hourStr.size() == 1) hourStr = "0" + hourStr;
    std::string minuteStr = std::string(minute);
    if (minuteStr.size() == 1) minuteStr = "0" + minuteStr;
    std::string secStr = std::string(sec);
    if (secStr.size() == 1) secStr = "0" + secStr;
    return yearStr + monStr + mdayStr +\
        hourStr + minuteStr + secStr;
}

 

 

另外,如果編譯器支持C++11的話,也可以使用std::chrono來操作時間,可參見下列實例代碼:

#include <iostream>
#include <chrono>
#include <ctime>

long fibonacci(int n)
{
    if (n < 3) return 1;
    return fibonacci(n-1) + fibonacci(n-2);
}

int main()
{
    std::chrono::time_point<std::chrono::system_clock> start, end;
    start = std::chrono::system_clock::now();
    std::cout << "f(42) = " << fibonacci(42) << '\n';
    end = std::chrono::system_clock::now();

    std::chrono::duration<double> elapsed_seconds = end-start;
    std::time_t end_time = std::chrono::system_clock::to_time_t(end);

    std::cout << "finished computation at " << std::ctime(&end_time)
              << "elapsed time: " << elapsed_seconds.count() << "s\n";
}

 


免責聲明!

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



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