tags:

views:

55

answers:

1

I need to generate values according to time of the day, is there a neat way to accomplish that ?

from 9-12 value should be between x - y

from 6-9 value should be between a - b

is there any other way than getting timeinfo struct and extracting hour out of it?

+1  A: 

You should take a look at boost::posix_time.

using namespace boost::posix_time;
using namespace boost::gregorian;

ptime now = second_clock::local_time();

// You can compare now with other ptime values
ptime nine_o_clock = day_clock::local_day() + hours(9);
ptime twelve_o_clock = day_clock::local_day() + hours(12);

if ((now >= nine_o_clock) && (now < twelve_o_clock))
{
  // Do what you want.
}
ereOn
@ereOn day_clock::local_day() returns date_timewhich can not be casted into ptime, that's the error I get when I try this. din't check into further as I had to submit that so used the *strftime* instead
Gollum
@Gollumn: Sorry, when I typed this I had no compiler on the machine. However, I'm pretty sure there is a way to convert a `gregorian_date` to a `ptime` using some specific constructor or function. Did you check the page I linked ? The `boost` time library is the most complete one I've ever seen, I would be astonished if such a simple thing wasn't possible.
ereOn