Reputation: 2593
I am using boost::posix_time
to implement clock.
date d(2000,Jan,20);
ptime start(d);
time_iterator titr(start,microseconds(15)); //increment by 15 microseconds
Now when I want to retrieve microseconds, I do ++time_iterator
, so it will increment clock by that duration. After this increment, I want to retrieve microseconds since past hour not total microseconds. One way is to convert it to string extract data from string. But is there any better way of doing that?
Upvotes: 1
Views: 120
Reputation: 393769
You can construct a time-of-day to mark the start-of-the-hour (soh
), and calculate the difference from there:
for (time_iterator titr(start, /*micro*/seconds(15)); *titr < end; ++titr) {
ptime soh(titr->date(), time_duration(titr->time_of_day().hours(), 0, 0));
std::cout << *titr << "\t" << soh << " elapsed: " << (*titr-soh).total_microseconds() << "\n";
}
It can be more efficient, realizing the date doesn't matter (hours align with dates):
for (time_iterator titr(start, /*micro*/seconds(15)); *titr < end; ++titr) {
std::cout << *titr << "\t elapsed: " << (titr->time_of_day()-hours(titr->time_of_day().hours())).total_microseconds() << "\n";
}
Maybe it could even be more efficient to take the total microseconds and do the modulo trick:
std::cout << *titr << "\t elapsed: " << (titr->time_of_day().total_microseconds() % hours(1).total_microseconds()) << "\n";
Always profile your code if it matters.
Upvotes: 1