C++ How to get the Day as a number from BOOST localtime

Hi there,
I want to get the day as a number from:
boost::posix_time::ptime today = boost::posix_time::second_clock::local_time();

(i.e, I want to get day of week 0-6 with Sunday==0  )


Thank you.
ambuliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
If you use the to_tm() function you can get the days of the week out of the tm struct.

http://www.boost.org/doc/libs/1_55_0/doc/html/date_time/local_time.html

tm to_tm(local_date_time)
A function for converting a local_date_time object to a tm struct.
// 6am, 2005-Jul-05 local time
std::string z("EST-05EDT,M4.1.0,M10.1.0");

ptime pt(date(2005,Jul,5),  hours(10));
time_zone_ptr zone( new posix_time_zone(z));
local_date_time ldt(pt, zone);

tm ldt_tm = to_tm(ldt);
/* tm_year => 105
   tm_mon  => 6
   tm_mday => 5
   tm_wday => 2 (Tuesday)
   tm_yday => 185
   tm_hour => 6
   tm_min  => 0
   tm_sec  => 0
   tm_isddst => 1 */

Open in new window


tm_wday      // int days since Sunday 0-6
http://www.cplusplus.com/reference/ctime/tm/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ambuliAuthor Commented:
Excellent.  Thank you!
0
evilrixSenior Software Engineer (Avast)Commented:
Thanks, ambuli. It's not the most elegant of solutions but it does work. There may be a better way but the truth is I really don't like the Boost date/time library as I think it's way over-the-top complex for 99% of use cases.

As an aside, if you are using a C++11 compatible compiler you might want to consider using the Chrono library instead. It's quite different from the Boost library and will take a little bit of reading to use it properly but it is way better and significantly simpler to use.

Best wishes.
0
sarabandeCommented:
you could do

time_duration td = today - pdate(date(2015,Jan,4), time_duration(0));
int day_of_week = (td.hours()/24)%7;

Open in new window


jan 4 was a sunday.

Sara
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.