[date_time] time_periodS subtraction

Hi, As far as I see, the time_period class does not support any subtraction operation. Is it possible to add it to the "wishlist"? It would be very useful addition for the further releases of the library. I mean the following operation: ----[123456789)----- -----period1 -------[3456789abcdef)---period2 ---[0123456789abc)------perod3 period2 - period1: -----------------[abcdef) period3 - period1: ---[0)----------[abc) Igor'. _________________________________________________________________ News, entertainment and everything you care about at Live.com. Get it now! http://www.live.com/getstarted.aspx

On Sun, May 18, 2008 at 01:59:33PM +0300, Igor R. wrote:
Hi, As far as I see, the time_period class does not support any subtraction operation. Is it possible to add it to the "wishlist"? It would be very useful addition for the further releases of the library. I mean the following operation:
Your examples are equivalent to set operations on an ordinary real number line. The boost Math library doesn't seem to implement this. Anyway, I think that this what you're asking for should be made into a general-purpose utility, and not something specific to the date_time library.

Your examples are equivalent to set operations on an ordinary real number line.> The boost Math library doesn't seem to implement this. Anyway, I think that> this what you're asking for should be made into a general-purpose utility, and> not something specific to the date_time library.> Why is subtraction of periods less "specific to the date_time library", than the following functions? contains() intersects() intersection() shift() expand() merge() span()
Subtraction of intervals seems to be very "native" operation for time_period, since it's VERY common task when working with time intervals. For example: one of my classes evaluates requests expressed in terms time_period. Each request is expensive, so the implementation "caches" them: say, if a user had already issued the request for period1:[1..8) and now he requested period2:[5..15), the evaluation must take place for [8-15) only... _________________________________________________________________ Connect to the next generation of MSN Messenger http://imagine-msn.com/messenger/launch80/default.aspx?locale=en-us&source=wlmailtagline

On Sun, May 18, 2008 at 04:17:41PM +0300, Igor R. wrote:
Why is subtraction of periods less "specific to the date_time library", than the following functions? contains(), intersects(), intersection(), shift(), expand(), merge(), span()
Ugh, it's not; I wasn't aware that they were already included in the library. They are all good candidates for generalization and placement in a separate library/class. IMHO.
Subtraction of intervals seems to be very "native" operation for time_period, since it's VERY common task when working with time intervals.
I'm the wrong person to complain to. The class seems generally unable to represent non-convex sets, e.g. the documentation for the "merge" method says: "Returns union of two periods. Null if no intersection." So the union of disjoint intervals [2,3] and [4,5] will return null. (Yes, it's still possible to write a difference operator crippled[*] in the same way as the merge operator. Hence the suggestion for factoring into a separate general-purpose library.) [*] Conditionally speaking crippled. Period is explicitly defined as "[.. range] between two times", something that the set [2,3] U [4,5] most definitely is not. I.e. I'd say you're using the wrong tool for the job.

Igor R. wrote:
Hi,
As far as I see, the time_period class does not support any subtraction operation. Is it possible to add it to the "wishlist"? It would be very useful addition for the further releases of the library. I mean the following operation:
----[123456789)----- -----period1 -------[3456789abcdef)---period2 ---[0123456789abc)------perod3
period2 - period1: -----------------[abcdef)
period3 - period1: ---[0)----------[abc)
I'll stay out of the more general period discussion for now. So if I
understand your request, the signature of operator- would look something like
this:
std::pair

std::pair
operator-(time_period p1, time_period p2);> Right?
Yes, that's what I thought about.
And I guess in the case of period2 - period1 one of the return values would be a null interval?
I believe, the second one?
There's also another case where there's no intersection which I would assume would return the original interval?
Yes, it would be consistent with a math. set subtraction. Besides, period1 - null_period == period1 (*)
Looking at this I wouldn't have a problem adding it to the wishlist -- it looks useful. I'm unlikely to use operator- because it's a little too cute -- I'd prefer another name like remove_intersection that makes the function clearer.
Actually, if this operation works just like any ordinary set subtraction does, then operator -() seems to be ok, doesn't it? But, anyway, the name doesn't matter :) (*) It would be also very useful if the rest of operations could work correctly with null_period; eg., period1.span(null_period) == period1 /*certainly, must be commutative*/ This way the code that uses various operations with periods would be cleaner (no need for numerous for is_null() checks). Besides, if the time_period does not have a default constructor that initializes it to be null, probably it worth defining at least null_period constant? Now it's quite not trivial to make such an initialization: time_period period(time_period(ptime(boost::date_time::not_a_date_time), ptime(date_time::not_a_date_time))); Thanks a lot, Igor'. _________________________________________________________________ News, entertainment and everything you care about at Live.com. Get it now! http://www.live.com/getstarted.aspx
participants (3)
-
Igor R.
-
Jeff Garland
-
Zeljko Vrba