在Java中,您可以这样做:
long now = (new Date()).getTime();
如何在C++中实现同样的功能?
namespace sc = std::chrono;
auto time = sc::system_clock::now(); // get the current time
auto since_epoch = time.time_since_epoch(); // get the duration since epoch
// I don't know what system_clock returns
// I think it's uint64_t nanoseconds since epoch
// Either way this duration_cast will do the right thing
auto millis = sc::duration_cast<sc::milliseconds>(since_epoch);
long now = millis.count(); // just like java (new Date()).getTime();
这个程序在gcc 4.4+上可以运行。使用--std=c++0x
编译它。我不知道VS2010是否已经实现了std::chrono
。
Sat, 11 Nov 47426 02:25:18 GMT
。为什么会发生这种情况? - W.K.S标准C++中没有这种方法(在标准C++中,只有秒级精度,而不是毫秒)。您可以使用非便携式的方式来实现,但由于您没有指定,因此我将假设您需要一种便携式的解决方案。我会建议您使用boost函数microsec_clock::local_time()。
我希望有一个名为time_ms
的函数,其定义如下:
// Used to measure intervals and absolute times
typedef int64_t msec_t;
// Get current time in milliseconds from the Epoch (Unix)
// or the time the system started (Windows).
msec_t time_ms(void);
#if defined(__WIN32__)
#include <windows.h>
msec_t time_ms(void)
{
return timeGetTime();
}
#else
#include <sys/time.h>
msec_t time_ms(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (msec_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
#endif
请注意,Windows分支返回的时间是自系统启动以来的毫秒数,而Unix分支返回的时间是自1970年以来的毫秒数。因此,如果您使用此代码,请仅依赖于时间之间的差异,而不是绝对时间本身。
GetSystemTimeAsFileTime
(http://msdn.microsoft.com/en-us/library/ms724397%28v=VS.85%29.aspx)函数。个人而言,我喜欢`microsec_clock::local_time()` - 所有的艰苦工作已经完成了 :-) - Dean Hardingtime_ms
原型上面的注释? - Matthew Flaschen你可以尝试这段代码(从StockFish棋类引擎源码中获取(GPL)):
#include <iostream>
#include <stdio>
#if !defined(_WIN32) && !defined(_WIN64) // Linux - Unix
# include <sys/time.h>
typedef timeval sys_time_t;
inline void system_time(sys_time_t* t) {
gettimeofday(t, NULL);
}
inline long long time_to_msec(const sys_time_t& t) {
return t.tv_sec * 1000LL + t.tv_usec / 1000;
}
#else // Windows and MinGW
# include <sys/timeb.h>
typedef _timeb sys_time_t;
inline void system_time(sys_time_t* t) { _ftime(t); }
inline long long time_to_msec(const sys_time_t& t) {
return t.time * 1000LL + t.millitm;
}
#endif
int main() {
sys_time_t t;
system_time(&t);
long long currentTimeMs = time_to_msec(t);
std::cout << "currentTimeMs:" << currentTimeMs << std::endl;
getchar(); // wait for keyboard input
}
long long
是打错了还是有意为之? - ndtrevivlong long
的长度是long
的两倍,因此即使将系统时间乘以1000,它们也不太可能溢出。 - NullPointerException标准C++没有具有亚秒精度的时间函数。
然而,几乎每个操作系统都有。因此,您需要编写依赖于操作系统的代码。
Win32:
GetSystemTime()
GetSystemTimeAsFileTime()
Unix/POSIX:
gettimeofday()
clock_gettime()
http://www.boost.org/doc/libs/1_43_0/doc/html/date_time.html
ptime microsec_clock::local_time() 或 ptime second_clock::local_time()
Java:
package com.company;
public class Main {
public static void main(String[] args) {
System.out.println(System.currentTimeMillis());
}
}
C++:
#include <stdio.h>
#include <windows.h>
__int64 currentTimeMillis() {
FILETIME f;
GetSystemTimeAsFileTime(&f);
(long long)f.dwHighDateTime;
__int64 nano = ((__int64)f.dwHighDateTime << 32LL) + (__int64)f.dwLowDateTime;
return (nano - 116444736000000000LL) / 10000;
}
int main() {
printf("%lli\n ", currentTimeMillis());
return 0;
}
System.currentTimeMillis()
。 - Steffen Heil