A question regarding gregorian week_iterator
I can’t understand what, if indeed anything, I am doing wrong here. Using boost 1.69 on Arch Linux I am trying to use a gregorian::week_iterator to iterate a gregorian date_period that has a length of 365 which produces this output: Starts 2018-07-10 Ends 2019-07-10 true false true 365
From this code:
boost::gregorian::date_period dp{ m_Start, m_Ends } ; BOOST_LOG(lg) << "Starts " << boost::gregorian::to_iso_extended_string(dp.begin()) << " Ends " << boost::gregorian::to_iso_extended_string(dp.end()) << " " << isYear << " " << isMonth << " " << isWeek << " " << dp.length() ; The following is the code I am using to iterate the date_period: for(boost::gregorian::week_iterator wi = dp.begin(); wi != dp.end(); ++wi) { items.push_back(formatParams(pi, (*wi))) ; } However, when this code executes it eventually fails with the following error: Year is out of valid range: 1400..9999 In my formatParams method the code generates the correct strings, but it doesn’t stop at Jun 2019 as I’d expect but continues to run out to 2375! I would appreciate some feedback on this as I have identical code that uses a gregorian::month_iterator and this generates the 12 items from the same date_period range as I’d expect. -- Bill
Bill Moo via Boost-users wrote:
I can’t understand what, if indeed anything, I am doing wrong here.
The following is the code I am using to iterate the date_period:
for(boost::gregorian::week_iterator wi = dp.begin(); wi != dp.end(); ++wi) { items.push_back(formatParams(pi, (*wi))) ; }
Hi Bill, The problem is that you are looking for a match with 'wi != dp.end()'. You are not getting a match so you are blowing right though the end. Use: wi < dp.end() For your comparison and it will stop properly. Best, Dan.
Thank you for the reply Dan.
Changing the iteration logic does indeed stop when expected, but for
me it raises a couple of question.
When using the month_iterator the != approach works so why not for the
week_iterator? As the month_iterator option I have uses the same
date_period approach.
The second question is since the iteration is over a predefined date
range whose start and ends are set why didn’t the iterations exhaust
on say Aug 2019? What allowed it to continue way outside of the
constrained range implying that the range is in fact much larger.
--
Bill
On Wed, 10 Jul 2019 at 17:48, Dan Bloomquist via Boost-users
Bill Moo via Boost-users wrote:
I can’t understand what, if indeed anything, I am doing wrong here.
The following is the code I am using to iterate the date_period:
for(boost::gregorian::week_iterator wi = dp.begin(); wi != dp.end(); ++wi) { items.push_back(formatParams(pi, (*wi))) ; }
Hi Bill, The problem is that you are looking for a match with 'wi != dp.end()'. You are not getting a match so you are blowing right though the end. Use:
wi < dp.end()
For your comparison and it will stop properly. Best, Dan.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org https://lists.boost.org/mailman/listinfo.cgi/boost-users
Bill Moo wrote:
Thank you for the reply Dan.
Changing the iteration logic does indeed stop when expected, but for me it raises a couple of question.
When using the month_iterator the != approach works so why not for the week_iterator? As the month_iterator option I have uses the same date_period approach.
The functor for incriminating by week simply adds 7 to the day count and 365 % 7 != 0. For the month iterator it digs down to find the actual calendar spans for the year and month given. The 'day' of each iteration by month lands on the same day of the month. It will work for leap years. for (boost::gregorian::month_iterator mi = dp.begin(); mi < dp.end(); ++mi) { std::cout << mi->month() << " " << mi->day() << std::endl; } For boost::gregorian iterators, I would only use the '<' rather than '!=' to insure you don't miss a hit.
The second question is since the iteration is over a predefined date range whose start and ends are set why didn’t the iterations exhaust on say Aug 2019? What allowed it to continue way outside of the constrained range implying that the range is in fact much larger.
These are not container iterators. The iterator is valid throughout the calendar range, they are only a day position in the calendar. Much like a char* can point anywhere in memory yet char* can be treated like an iterator. There is no specialization to 'contain' the iterator. Best, Dan.
Ah! of course, that makes perfect sense to me now, so I am wiser now
than I was when I posted so thanks again Dan.
--
Bill
On Thu, 11 Jul 2019 at 17:50, Dan Bloomquist
Bill Moo wrote:
Thank you for the reply Dan.
Changing the iteration logic does indeed stop when expected, but for me it raises a couple of question.
When using the month_iterator the != approach works so why not for the week_iterator? As the month_iterator option I have uses the same date_period approach.
The functor for incriminating by week simply adds 7 to the day count and 365 % 7 != 0. For the month iterator it digs down to find the actual calendar spans for the year and month given. The 'day' of each iteration by month lands on the same day of the month. It will work for leap years.
for (boost::gregorian::month_iterator mi = dp.begin(); mi < dp.end(); ++mi) { std::cout << mi->month() << " " << mi->day() << std::endl; }
For boost::gregorian iterators, I would only use the '<' rather than '!=' to insure you don't miss a hit.
The second question is since the iteration is over a predefined date range whose start and ends are set why didn’t the iterations exhaust on say Aug 2019? What allowed it to continue way outside of the constrained range implying that the range is in fact much larger.
These are not container iterators. The iterator is valid throughout the calendar range, they are only a day position in the calendar. Much like a char* can point anywhere in memory yet char* can be treated like an iterator. There is no specialization to 'contain' the iterator. Best, Dan.
-----Original Message----- From: Boost-users
On Behalf Of Dan Bloomquist via Boost-users Sent: 11 July 2019 17:50 To: Bill Moo ; boost-users@lists.boost.org Cc: Dan Bloomquist Subject: Re: [Boost-users] A question regarding gregorian week_iterator Bill Moo wrote:
Thank you for the reply Dan.
Changing the iteration logic does indeed stop when expected, but for me it raises a couple of question.
When using the month_iterator the != approach works so why not for the week_iterator? As the month_iterator option I have uses the same date_period approach.
The functor for incriminating by week simply adds 7 to the day count and 365 % 7 != 0. For the month iterator it digs down to find the actual calendar spans for the year and month given. The 'day' of each iteration by month lands on the same day of the month. It will work for leap years.
for (boost::gregorian::month_iterator mi = dp.begin(); mi < dp.end(); ++mi) { std::cout << mi->month() << " " << mi->day() << std::endl; }
For boost::gregorian iterators, I would only use the '<' rather than '!=' to insure you don't miss a hit.
The second question is since the iteration is over a predefined date range whose start and ends are set why didn’t the iterations exhaust on say Aug 2019? What allowed it to continue way outside of the constrained range implying that the range is in fact much larger.
These are not container iterators. The iterator is valid throughout the calendar range, they are only a day position in the calendar. Much like a char* can point anywhere in memory yet char* can be treated like an iterator. There is no specialization to 'contain' the iterator.
That is a surprise to me (and Bill too I suspect 😉). I think this should be prominently mentioned in the documentation. Paul Paul A. Bristow Prizet Farmhouse Kendal, Cumbria LA8 8AB UK
participants (3)
-
Bill Moo
-
Dan Bloomquist
-
pbristow@hetp.u-net.com