在c++中是否有跨平台的方法来获取当前的日期和时间?


当前回答

我发现这个链接对我的实现非常有用: c++日期和时间

下面是我在实现中使用的代码,以获得清晰的“YYYYMMDD HHMMSS”输出格式。参数in用于在UTC和本地时间之间切换。您可以轻松地修改我的代码以满足您的需要。

#include <iostream>
#include <ctime>

using namespace std;

/**
 * This function gets the current date time
 * @param useLocalTime true if want to use local time, default to false (UTC)
 * @return current datetime in the format of "YYYYMMDD HHMMSS"
 */

string getCurrentDateTime(bool useLocalTime) {
    stringstream currentDateTime;
    // current date/time based on current system
    time_t ttNow = time(0);
    tm * ptmNow;

    if (useLocalTime)
        ptmNow = localtime(&ttNow);
    else
        ptmNow = gmtime(&ttNow);

    currentDateTime << 1900 + ptmNow->tm_year;

    //month
    if (ptmNow->tm_mon < 9)
        //Fill in the leading 0 if less than 10
        currentDateTime << "0" << 1 + ptmNow->tm_mon;
    else
        currentDateTime << (1 + ptmNow->tm_mon);

    //day
    if (ptmNow->tm_mday < 10)
        currentDateTime << "0" << ptmNow->tm_mday << " ";
    else
        currentDateTime <<  ptmNow->tm_mday << " ";

    //hour
    if (ptmNow->tm_hour < 10)
        currentDateTime << "0" << ptmNow->tm_hour;
    else
        currentDateTime << ptmNow->tm_hour;

    //min
    if (ptmNow->tm_min < 10)
        currentDateTime << "0" << ptmNow->tm_min;
    else
        currentDateTime << ptmNow->tm_min;

    //sec
    if (ptmNow->tm_sec < 10)
        currentDateTime << "0" << ptmNow->tm_sec;
    else
        currentDateTime << ptmNow->tm_sec;


    return currentDateTime.str();
}

输出(UTC, EST):

20161123 000454
20161122 190454

其他回答

#include <Windows.h>

void main()
{
     //Following is a structure to store date / time

SYSTEMTIME SystemTime, LocalTime;

    //To get the local time

int loctime = GetLocalTime(&LocalTime);

    //To get the system time

int systime = GetSystemTime(&SystemTime)

}

ffead-cpp为各种任务提供了多个实用程序类,其中一个类是Date类,它提供了从Date操作到日期算术的许多特性,还有一个Timer类用于计时操作。你也可以看看。

你可以使用boost和chrono库:

#include <iostream>
#include <chrono>
#include <boost/date_time/posix_time/posix_time.hpp>

using boost::posix_time::to_iso_extended_string;
using boost::posix_time::from_time_t;
using std::chrono::system_clock;

int main()
{
  auto now = system_clock::now();
  std::cout << to_iso_extended_string(from_time_t(system_clock::to_time_t(now)));
}

下面是不弃用的现代c++解决方案,用于将时间戳作为std::string用于例如文件名:

std::string get_file_timestamp()
{
    const auto now = std::chrono::system_clock::now();
    const auto in_time_t = std::chrono::system_clock::to_time_t(now);

    std::stringstream output_stream;

    struct tm time_info;
    const auto errno_value = localtime_s(&time_info, &in_time_t);
    if(errno_value != 0)
    {
        throw std::runtime_error("localtime_s() failed: " + std::to_string(errno_value));
    }

    output_stream << std::put_time(&time_info, "%Y-%m-%d.%H_%M_%S");
    return output_stream.str();
}
#include <stdio.h>
#include <time.h>

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

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "Current local time and date: %s", asctime (timeinfo) );

  return 0;
}