RE: datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ?

Discussion in 'Python' started by Tim Peters, Sep 9, 2003.

  1. Tim Peters

    Tim Peters Guest

    [Christos TZOTZIOY Georgiou]
    > Is there any reason for datetime.time subtraction not returning a
    > datetime.timedelta object, just like datetime.datetime subtraction
    > does?


    Guido didn't want time objects participating in arithmetic because the
    desired semantics are unclear. For example, if you allow

    time1 - time2

    to return a timedelta, then you'll also want to allow

    time2 + timedelta

    to return a time, and then you've got to make up rules for what happens when
    adding spills over a midnight boundary. Should it "wrap around"? Raise
    OverflowError? Guido didn't want to endure arguments about that.

    Note that the analogous question for datetime objects is easy, because
    there's a well-defined Beginning and End to the span of times datetime
    objects can represent -- OverflowError is the obvious thing to get if you
    try to compute a result spilling over those bounds. Try adding "a year" to
    "midnight", though, and half the world will curse you if it raises an
    exception, while the other half will curse you if it says "hmm -- still
    midnight". A third half will whine about leap seconds <wink>.

    > It doesn't feel good having to use dumb year, month, day
    > arguments constructing datetime object when doing only time
    > arithmetic.


    What's wrong with using timedeltas?

    >>> print timedelta(hours=3, minutes=15) - timedelta(hours=2, minutes=45)

    0:30:00
    >>>


    > If there is no specific reason apart from lack of time (no pun), I'm
    > willing to research it and write a patch (I haven't seen the code,
    > but I believe it would be quite easy).


    Selling it would be harder than writing it.
     
    Tim Peters, Sep 9, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    763
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Lad
    Replies:
    5
    Views:
    556
    John Machin
    Aug 7, 2006
  3. kath
    Replies:
    2
    Views:
    955
    Fredrik Lundh
    Oct 6, 2006
  4. Replies:
    2
    Views:
    487
    Skip Montanaro
    Sep 1, 2007
  5. Skip Montanaro

    datetime.timedelta.replace?

    Skip Montanaro, Oct 9, 2013, in forum: Python
    Replies:
    0
    Views:
    108
    Skip Montanaro
    Oct 9, 2013
Loading...

Share This Page