C++程序获取当前时间

huangapple go评论84阅读模式
英文:

C++ program to get the current time

问题

我必须创建一个简单的窗口服务程序,该程序将在Visual Studio中使用C++语言执行。我已经使用了ctimelocaltime关键字,但出现错误,提示:
> 这个函数或变量可能不安全。考虑使用其他...

我正在使用Visual Studio 2019。

英文:

I have to create a simple window service program which is to be executed in visual studio using C++ language. I have used ctime and localtime keywords but it returns an error saying:
> This function or variable may be unsafe. Consider using any other ....

I am using Visual Studio 2019

答案1

得分: 4

你可以使用std::chrono::system_clock::now()函数来获取当前时间点。

英文:

You can use std::chrono::system_clock::now() function to get the current time point.

答案2

得分: 3

ctimelocaltime 是不安全的,因为它们返回指向静态数据的指针。

因此,在VS2019(以及一些较早版本中)提供了 ctime_slocaltime_s,它们需要预先分配的指针来存储返回的值,因此使用起来更安全。

英文:

ctime and localtime are unsafe as these returns pointer to static data.

Therefore, ctime_s and localtime_s are provided in VS2019 (also in some earlier versions), which takes a pre allocated pointer in which value need to be returned and hence safe to use.

huangapple
  • 本文由 发表于 2020年1月3日 14:41:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定