使用下面的代碼我可以得到當前時間以毫秒爲單位。現在我想將毫秒添加到系統時間。任何提示?添加毫秒到timeval C++
#include <stdio.h>
#include <sys/time.h>
int main (int argc, char** argv) {
struct timeval time;
gettimeofday (&time, NULL);
long systemtime = time.tv_sec*1000L + time.tv_usec/1000L;
printf("Time in milliseconds: %ld milliseconds\n", systemtime);
//sample output: 1492592522106
return 0;
}
編輯:解決
#include <stdio.h>
#include <sys/time.h>
int main (int argc, char** argv) {
struct timeval time;
gettimeofday (&time, NULL);
printf("Time in milliseconds: %ld milliseconds\n", time.tv_sec*1000L +
(time.tv_usec/1000L));
printf("Time in milliseconds+300: %ld milliseconds\n", time.tv_sec*1000L
+ (time.tv_usec/1000L+300));
printf("usec: %ld", time.tv_usec/1000L);
return 0;
}
輸出:
Time in milliseconds: 1492595580965 milliseconds (Wed, 19 Apr 2017 09:53:00.965 GMT)
Time in milliseconds+300: 1492595581265 milliseconds (Wed, 19 Apr 2017 09:53:01.265 GMT)
usec: 965
的可能的複製[如何使用timeval結構計算毫秒?](http://stackoverflow.com/questions/18650057/how-to-calculate-milliseconds- using-timeval-structure) –
我已經有了以毫秒爲單位的當前時間。現在我想添加例如300毫秒到當前時間(代碼中的系統時間)。 – Janoshh
'systemtime + = 300;'? –