C++ 如何以秒为单位获取系统的当前日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14315497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get current date time of the system in seconds
提问by olidev
How can I get current date time of the systems in seconds in C++?
如何在 C++ 中以秒为单位获取系统的当前日期时间?
I tried this one:
我试过这个:
struct tm mytm = { 0 };
time_t result;
result = mktime(&mytm);
printf("%lld\n", (long long) result);
but I got: -1?
但我得到:-1?
回答by Asmita
/* time example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t seconds;
seconds = time (NULL);
printf ("%ld seconds since January 1, 1970", seconds);
return 0;
}
回答by Zeta
C++11 version, which ensures that the representation of ticks is actually an integral:
C++11 版本,确保刻度的表示实际上是一个整数:
#include <iostream>
#include <chrono>
#include <type_traits>
std::chrono::system_clock::rep time_since_epoch(){
static_assert(
std::is_integral<std::chrono::system_clock::rep>::value,
"Representation of ticks isn't an integral value."
);
auto now = std::chrono::system_clock::now().time_since_epoch();
return std::chrono::duration_cast<std::chrono::seconds>(now).count();
}
int main(){
std::cout << time_since_epoch() << std::endl;
}
回答by Asmita
Try this: I hope it will work for you.
试试这个:我希望它对你有用。
#include <iostream>
#include <ctime>
using namespace std;
int main( )
{
// current date/time based on current system
time_t now = time(0);
// convert now to string form
char* dt = ctime(&now);
cout << "The local date and time is: " << dt << endl;
// convert now to tm struct for UTC
tm *gmtm = gmtime(&now);
dt = asctime(gmtm);
cout << "The UTC date and time is:"<< dt << endl;
}
回答by user5972998
possibly a simpler version of example provided by @Zeta
可能是@Zeta 提供的示例的更简单版本
time_t time_since_epoch()
{
auto now = std::chrono::system_clock::now();
return std::chrono::system_clock::to_time_t( now );
}
回答by Navin
This will give the current date/time in seconds,
这将以秒为单位给出当前日期/时间,
#include <time.h>
time_t timeInSec;
time(&timeInSec);
PrintLn("Current time in seconds : \t%lld\n", (long long)timeInSec);
回答by Shital Shah
I'm using below function with few minor enhancements but as other have suggested definition of epoch might not be portable. For GCC it returns seconds as double value since Unix epoch but in VC++ it returns value since machine boot time. If your goal is just to get some value to do diff between two timestamps without persisting and sharing them, this should be ok however. If you need to persist or share timestamps then I would suggest to explicitly subtract some epoch from now() to get the duration object to be portable.
我正在使用下面的函数,但没有一些小的改进,但正如其他人所建议的那样,epoch 的定义可能不可移植。对于 GCC,它返回自 Unix 纪元以来的 double 值的秒数,但在 VC++ 中,它返回自机器启动时间以来的值。如果您的目标只是获得一些值来在两个时间戳之间进行差异处理而不保留和共享它们,那么这应该没问题。如果您需要保留或共享时间戳,那么我建议从 now() 中明确减去一些纪元以使持续时间对象可移植。
//high precision time in seconds since epoch
static double getTimeSinceEpoch(std::chrono::high_resolution_clock::time_point* t = nullptr)
{
using Clock = std::chrono::high_resolution_clock;
return std::chrono::duration<double>((t != nullptr ? *t : Clock::now() ).time_since_epoch()).count();
}