【问题标题】:How do I get the current date in C++?如何在 C++ 中获取当前日期?
【发布时间】:2017-02-27 06:29:54
【问题描述】:

我一直试图在 C++ 中获取当前日期一段时间,但我无法弄清楚我做错了什么。我查看了几个站点,并且我实施的所有解决方案都收到一条错误消息:“此函数或变量可能不安全。考虑改用 localtime_s。”我尝试了找到here 的几个解决方案(包括下面的一个),但我无法让它们中的任何一个工作。我做错了什么?

#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>

using namespace std;

int main()
{

    const int SALARY = 18;
    const int COMMISSION = .08;
    const int BONUS = .03;

    int monthlySales;
    int appointmentNumber;

    time_t t = time(0);   // get time now
    struct tm * now = localtime(&t);

    string name;


//this is where the user adds their name and date
    cout << "Please enter the sales representative's name: ";
    cin >> name;
    cout << "Please enter the number of appointments: ";
    cin >> appointmentNumber;
    cout << "Please enter the amount of sales for the month: $";
    cin >> monthlySales;

//clear screen and execute code
    system("cls");

    cout << setfill(' ');
    cout << "Sales Representative:" << name << endl;
    cout << "Pay Date:" << (now->tm_mon + 1) << " " << now->tm_mday << " " << (now->tm_year + 1900) << endl;
    cout << "Work Count:" << appointmentNumber << "Sale Amount" 
        << monthlySales << endl;

        system("pause");

    return 0;
}

【问题讨论】:

标签: c++


【解决方案1】:

这是我的做法:

#include "date/tz.h"
#include <iostream>

int
main()
{
    using namespace std::chrono;
    std::cout << date::make_zoned(date::current_zone(), system_clock::now()) << '\n';
}

这只是为我输出:

2016-10-18 10:39:10.526768 EDT

我使用这个 C++11/14 portable, free, open-source library。它是线程安全的。它基于&lt;chrono&gt;。它是类型安全且易于使用的。如果你需要更多的功能,这个库可以做到。

  • 获取另一个时区的当地时间
  • 将本地时间直接从一个时区转换为另一个时区。
  • 在时间计算中考虑闰秒。
  • 以任意精度流式传输/流式传输往返时间戳,并且不会丢失信息。
  • 在所有时区中搜索属性(例如缩写或偏移量)。

这个库正被提交给 C++ 标准委员会,draft here

【讨论】:

  • 未定义对date::current_zone()的引用;我在namespace date 中也找不到它的声明。
  • link above 请参阅标题为Installation 的部分。
【解决方案2】:

你可以试试下面的代码和下面的描述。

#include <iostream>
#include <ctime>

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
  char buffer[80];

  time (&rawtime);
  timeinfo = localtime(&rawtime);

  strftime(buffer,80,"%d-%m-%Y %I:%M:%S",timeinfo);
  std::string str(buffer);

  std::cout << str;

  return 0;
}

功能

time_t时间(time_t*定时器);

函数返回这个值,如果参数不是空指针,它也会把这个值设置为定时器指向的对象。

参数

  • 定时器 指向 time_t 类型对象的指针,存储时间值。您也可以将空指针传递给它,以防不需要

返回值

当前日历时间作为time_t对象。如果函数无法检索日历时间,则返回值-1。

【讨论】:

    【解决方案3】:

    您收到此警告可能是因为localtime() 不是线程安全的。调用此函数的两个实例可能会导致一些差异。

    [...] localtime 返回指向静态缓冲区 (std::tm*) 的指针。 另一个线程可以调用该函数,静态缓冲区可以是 在第一个线程完成读取内容之前被覆盖 std::tm* 结构。

    【讨论】:

      【解决方案4】:

      标准的跨平台方式是使用chrono

      #include <iostream>
      #include <chrono>
      
      int main(){
          std::time_t now_time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
          std::cout << "Now:" << std::ctime(&now_time);
      }
      

      【讨论】:

        【解决方案5】:

        这是另一种可行的方法:

        time_t current_time;
        struct tm *timeinfo;
        time(&current_time);
        timeinfo = localtime(&current_time);
        string date = asctime(timeinfo);
        

        【讨论】:

        • 它仍然给我,“错误 C4996 'localtime':此函数或变量可能不安全。考虑改用 localtime_s。要禁用弃用,请使用 _CRT_SECURE_NO_WARNINGS。有关详细信息,请参阅在线帮助。”
        【解决方案6】:

        非常感谢您及时的回复。最终,我能够使用 Heemanshu Bhalla 的回应的变体。我通过 here 将“_CRT_SECURE_NO_WARNINGS”添加到预处理器定义中,然后我将 Heemanshu 的代码更改为以下代码。这符合我的需要。

        #include <iostream>
        #include <ctime>
        #include <string>
        
        using namespace std;
        
        int main()
        {
            time_t rawtime;
            struct tm * timeinfo;
            char buffer[80];
        
            time(&rawtime);
            timeinfo = localtime(&rawtime);
        
            strftime(buffer, 80, "%m/%d/%Y ", timeinfo);
            string str(buffer);
        
            cout << str << endl;
        
            system("PAUSE");
        
            return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-29
          • 2010-11-07
          • 2010-12-04
          • 2013-03-27
          相关资源
          最近更新 更多