繁体   English   中英

如何在C ++中获取当前日期?

[英]How do I get the current date in C++?

我一直试图用C ++获取当前日期一段时间,我无法弄清楚我做错了什么。 我查看了几个站点和我实现的所有解决方案我得到一个错误,上面写着“这个函数或变量可能不安全。 请考虑使用localtime_s。“我尝试了这里找到的几个解决方案(包括下面的解决方案),但我无法使用它们。 我究竟做错了什么?

#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;
}

我是这样做的:

#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 便携式免费开源库 它是线程安全的。 它基于<chrono> 它类型安全且易于使用。 如果您需要更多功能,此库将执行此操作。

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

该库正在向C ++标准委员会提出,在此草拟

您可以尝试下面的代码和说明。

#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(time_t * timer);

函数返回此值,如果参数不是空指针,它还将此值设置为timer指向的对象。

参数

  • timer指向time_t类型的对象的指针,其中存储了时间值。如果不需要,您也可以传递空指针

回报价值

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

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

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

标准和跨平台的方式是使用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);
}

这是另一种可行的方法:

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

我非常感谢你及时回复。 最终我能够使用Heemanshu Bhalla的变化作出回应。 我说“_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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM