Reputation: 19
With the code beneath I can get the current time in milliseconds. Now I want to add milliseconds to the systemtime. Any hints?
#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;
}
EDIT: SOLVED
#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;
}
output:
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
Upvotes: 0
Views: 3811
Reputation: 2129
You can use std::chrono
library to perform this task.
Following code snippet will help you with this,
auto now = std::chrono::system_clock::now().time_since_epoch();
auto t100ms = std::chrono::milliseconds(100);
auto time = now + t100ms;
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(time).count();
Upvotes: 4
Reputation: 1
Did you try adjtime()? . http://man7.org/linux/man-pages/man3/adjtime.3.html
Upvotes: -3