boost::gregorian::date_period,how to increment/decrement the period

hello! boost::gregorian::days size(1); boost::gregorian::date_period tmp(period.begin()+=size,period.end()-=size); period=tmp; is there any better way to increment/decrement the period object I want period.begin+=size; period.end()-=size to be permanent to period but it doesn't,it returns an object... any idea?? thanks for any help.

On Sun, 26 Mar 2006 19:19:44 +0000 (UTC), george wrote
hello!
boost::gregorian::days size(1);
boost::gregorian::date_period tmp(period.begin()+=size,period.end() -=size);
period=tmp;
is there any better way to increment/decrement the period object
I want period.begin+=size; period.end()-=size
to be permanent to period but it doesn't,it returns an object...
any idea??
No there isn't an easy way to do this in the current release. However, I've checked in a new function to boost/date_time/period.hpp called 'expand' which provides this capability. So you can now say: date_period dp(.....); dp.expand(days(2)); Of course, due to the wonders of generic programming this will now work for time periods too :-) time_period tp(.....); tp.expand(minutes(10)); You can easily patch it in yourself or download a new period.hpp from cvs. Basically in the period.hpp file add this: template<class point_rep, class duration_rep> class period : private //.... void expand(const duration_rep& d); //... Then later in the file /** Expands the size of the period by the duration on both ends. * *So before expand *@code * * [-------] * ^ ^ ^ ^ ^ ^ ^ * 1 2 3 4 5 6 7 * *@endcode * After expand(2) *@code * * [----------------------] * ^ ^ ^ ^ ^ ^ ^ * 1 2 3 4 5 6 7 * *@endcode */ template<class point_rep, class duration_rep> inline void period<point_rep,duration_rep>::expand(const duration_rep& d) { begin_ = begin_ - d; last_ = last_ + d; } Jeff

template<class point_rep, class duration_rep> inline void period<point_rep,duration_rep>::expand(const duration_rep& d) { begin_ = begin_ - d; last_ = last_ + d; }
Jeff
thanks Jeff,nice patch. but if I want period to get smaller? I have to go with a tmp period object? are you planing to add a method for this also? I don't want to play with boost::date_time and do it by myself becouse the source will be open to others and I want to use the 'standard' boost. this patch will be in 1.34? thanks for any advise.

On Sun, 26 Mar 2006 22:25:13 +0000 (UTC), george wrote
template<class point_rep, class duration_rep> inline void period<point_rep,duration_rep>::expand(const duration_rep& d) { begin_ = begin_ - d; last_ = last_ + d; }
Jeff
thanks Jeff,nice patch. but if I want period to get smaller? I have to go with a tmp period object? are you planing to add a method for this also?
I don't want to play with boost::date_time and do it by myself becouse the source will be open to others and I want to use the 'standard' boost. this patch will be in 1.34?
Well, you could write a 'contract' method. However, contract is much more dangerous in that there is the possibility of making an ill formed period. I'd stick with the temporary object approach -- you can easily write this into your own little functions that hide the details. Something like this: date_period contract(const date_period& dp, days len) { if (dp.length() < len) { //handle errors here -- after contraction nothing is left... } return date_period(dp.start() + len, dp.end()-len); } In use: date_period dp(....); dp = contract(dp, days(2)); expand won't be in 1.34 b/c we've already had a feature freeze. For now you could trivially write the expand as I'm suggesting above. Jeff

date_period contract(const date_period& dp, days len) { if (dp.length() < len) { //handle errors here -- after contraction nothing is left... } return date_period(dp.start() + len, dp.end()-len); }
In use: date_period dp(....); dp = contract(dp, days(2));
thanks Jeff,I will go this way. I suggest you to add also this functionality in a next version(1.34.1(?)) it's very usefull. thanks
participants (2)
-
george
-
Jeff Garland