Difference Between Two datetimes
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Mon Dec 28 18:05:13 EST 2009
On Mon, 28 Dec 2009 10:54:46 -0800, W. eWatson wrote:
> Ben Finney wrote:
>> "W. eWatson" <wolftracks at invalid.com> writes:
>>
>>> Lie Ryan wrote:
>>>> what's strange about it? the difference between 2009/01/02 13:01:15
>>>> and 2009/01/04 13:01:15 is indeed 2 days... Can you elaborate what do
>>>> you mean by 'strange'?
>>
>>> Easily. In one case, it produces a one argument funcion, and the other
>>> 2, possibly even a year if that differs.
[...]
> Well, it just seems weird to me. <g>. I'm modestly familiar with
> objects, but this seems like doing the following.
>
> Suppose we have a module called trigonometry, trig for short. It
> contains lots of trig functions, and sort of uses the same concepts as
> datetime. Bear with me on that. Here's my imagined interpretive session:
>
> >> import trig
> >> c=trig.sin(90.0) # arg is in degrees
> >> print c
> trig.cos(1.0)
> >> type(c)
> <type 'trig'>
> >> value = c.value
> >> print value
> 1.0
> I'd call that weird. Maybe in this case it is ... <g>
No, that's an invalid analogy, because trig functions are defined to
return unitless numbers and so there is no point in distinguishing
between the 1.0 you get from the sine of 90 degrees and the 1.0 you get
from halving 2.0. They are the same thing.
But time is different. Not only do times have a unit, but we also
distinguish between two different concepts: times as points on a
calendar, as well as differences between such times.
A calendar time of 1000 seconds is not the same as a time difference of
1000 seconds: 1000 seconds is 'Thu Jan 1 10:16:40 1970' according to the
POSIX standard, Windows may pick a different moment for zero. But a time
difference doesn't correspond to any specific moment in time at all, and
represents a distance between two points on the calendar -- a duration.
Hence Python provides timedelta objects for working with time
differences, and datetime objects for working with calendar times.
Of course, one might have chosen to take a different approach, and use
(say) raw ints only for working with dates no matter whether they
represent an absolute time or a relative time. Then the programmer would
be responsible for interpreting 1000 as either 10:16:40 Jan 1 1970 or a
duration of 16 minutes 40 seconds, whichever is appropriate. That's a
legitimate design choice too, but not the one Python uses.
--
Steven
More information about the Python-list
mailing list