【发布时间】:2017-09-15 10:00:55
【问题描述】:
通过下面的代码,我可以获得以毫秒为单位的当前时间。现在我想将毫秒添加到系统时间。有什么提示吗?
#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
【问题讨论】:
-
我已经有了以毫秒为单位的当前时间。现在我想将例如 300ms 添加到当前时间(代码中的系统时间)。
-
systemtime += 300;? -
"将毫秒添加到系统时间" 请说明您希望它具有的确切效果。
-
例如当前时间是 09:10:53.000。我想给它加300ms,所以结果是:09:10:53.300