获取系统时间(以毫秒为单位)作为int/double
Get System Time In MilliSeconds as an int/double
我是c++的新手,但我根本无法让它工作。我试图获得系统的当前时间(毫秒),并用它做一些事情,但它不会像我尝试的那样工作。
Qt
QDateTime qt = new QDateTime();
int x = qt.currentDateTimeUtc();
if(x%5 ==0){
//something
}
c++
double sysTime = time(0);
if(sysTime%5.00 ==0.00){
}
我得到类型为double的无效操作数到二进制运算符错误。我不知道为什么?任何人都可以指向正确的方向
对于QT,请尝试使用函数QDateTime::toMSecsSinceEpoch()
http://doc.qt.io/qt-5/qdatetime.html#toMSecsSinceEpoch
这将返回一个qint64http://doc.qt.io/qt-5/qtglobal.html#qint64-typedef
如果您试图在C中获得以毫秒为单位的unix时间戳,您可以尝试以下代码:
include "time.h"
...
time_t seconds;
time(&seconds);
unsigned long long millis = (unsigned long long)seconds * 1000;
尽管请注意,这是乘以1000的——看起来像毫秒,但准确度是秒——如果你试图每5秒做一件事,根据你的x % 5
代码判断,这可能就足够了,所以以下应该足够了:
time_t seconds; time(&seconds);
相关文章:
- 在 C 和 C++ 中作为函数参数,int **a 和 int a[][] 之间有什么确切的区别
- <string> 如何在使用 SWIG 时将 int 数组和 List 作为参数传递给 C# C++
- z3 提取 Seq Int 作为 std::vector<int>
- 在 C/C++ 中将数组作为形式参数作为 int arr[] 和 int arr[N] 传递的区别
- 无法在初始化时将"日期"转换为"int"作为错误
- 使用无符号int作为二进制来在c++中实现一个集
- 是否有可能在没有复制的情况下传递 std::vector<int> 作为参数来获得 std::vector<std::array<int, 3>>?
- 使用 pair<int、int> 或字符串作为 map<> 键,哪个更有效?
- 作为 int 传递和类内部指针之间的差异给我带来了分段错误
- 创建一个程序,要求用户输入两个数字,并有三个可能的答案.代码未编译.数字(作为int)
- 如何使用 strlen() 作为 int
- 访问矢量<矢量>作为<int>平面数组
- 获取系统时间(以毫秒为单位)作为int/double
- 如何在不复制的情况下将只读向量传递给作为<int> pair<int,int>向量一部分的函数
- C/C++:正在使用比较结果作为int真正的无分支
- 将指向函数的指针数组作为"int"参数传递是否可以将地址转换为整数?
- 为什么系统在没有警告的情况下接受作为int参数传递的long int
- 在64位机器上使用8字节作为int数据类型大小的任何C/ c++编译器
- 当我们在 c++ 中没有适当的左值时,应该返回什么作为 int&?
- C++ 将字节值作为 int 添加到 CString