joh*_*ohn 5 c++ timestamp milliseconds c++11
我正在运行我的下面代码,检查是否data_timestamp超过两周.如果它是两周大,那么打印你好否则打印世界.
我是一名Java开发人员,最近开始使用C++.通过互联网学到了一些东西,所以我在这个程序中使用它.
#include <ctime>
#include <chrono>
#include <iostream>
int main()
{
// this has to be uint64_t bcoz of old code
uint64_t data_timestamp = 1406066507000;
const auto now = std::chrono::system_clock::now();
auto twoWeeks = std::chrono::hours(24 * 14);
auto lastTwoWeeks = now - twoWeeks;
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(lastTwoWeeks.time_since_epoch()).count();
std::cout << "Time stamp in milliseconds since UNIX epoch start: "<< millis << std::endl;
if (data_timestamp < millis) {
std::cout << "Hello";
} else {
std::cout << "World";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我将在Ubuntu 12.04上运行此代码.当我在运行时编译它时make install,它给了我这个例外 -
warning: âautoâ changes meaning in C++11; please remove it [-Wc++0x-compat]
error: ânowâ does not name a type
warning: âautoâ changes meaning in C++11; please remove it [-Wc++0x-compat]
âtwoWeeksâ does not name a type
warning: âautoâ changes meaning in C++11; please remove it [-Wc++0x-compat]
error: âlastTwoWeeksâ does not name a type
warning: âautoâ changes meaning in C++11; please remove it [-Wc++0x-compat]
error: âmillisâ does not name a type
error: âmillisâ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
可能是,我没有C++11.这是我制作的一个简单的程序,但是我在大型C++项目中使用它的程序的核心逻辑,所以看起来,我无法将所有内容移植到C++ 11中以使其工作.有没有其他方法可以编写不使用C++ 11的代码?
更新: -
这是我在代码的某个部分的大项目中以毫秒为单位获取当前时间戳的方式 -
struct timeval tp;
gettimeofday(&tp, NULL);
uint64_t current_ms = tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds
Run Code Online (Sandbox Code Playgroud)
Bri*_*ian 13
auto(推断类型)的新含义是在C++ 11中引入的.使用标志编译代码-std=c++11.