Difference Between Two datetimes

W

W. eWatson

According to one web source, this program:

import datetime
bree = datetime.datetime(1981, 6, 16, 4, 35, 25)
nat = datetime.datetime(1973, 1, 18, 3, 45, 50)

difference = bree - nat
print "There were", difference, "minutes between Nat and Bree"

yields:
There were 3071 days, 0:49:35 minutes between Nat and Bree

That's fine, but I'd like to start with two dates as strings, as
"1961/06/16 04:35:25" and "1973/01/18 03:45:50"

How do I get the strings into a shape that will accommodate a difference?

For example,
t1=datetime.datetime.strptime("2009/01/02 13:01:15","%y/%m/%d %H:%M:%S")
doesn't do it.
ValueError: time data did not match format: data=2009/01/02 13:01:15
fmt=%y/%m/%d %H:%M:%S
 
R

Roy Smith

"W. eWatson said:
t1=datetime.datetime.strptime("2009/01/02 13:01:15","%y/%m/%d %H:%M:%S")
doesn't do it.
ValueError: time data did not match format: data=2009/01/02 13:01:15
fmt=%y/%m/%d %H:%M:%S

The first thing that jumps out at me is that %y is the two-digit year. You
want %Y for 4-digit year.

One thing to keep in mind is that "2009/01/02 13:01:15" is ambiguous
without a time zone. Even if you assume that both timestamps were from the
same location, you need to know what daylight savings rules that location
uses, to do this right.
 
W

W. eWatson

You're right. Y. Works fine. The produces datetime.datetime(2009, 1, 2,
13, 1, 15).
If I now use
t2=datetime.datetime.strptime("2009/01/04 13:01:15","%Y/%m/%d %H:%M:%S")
I get tw as
datetime.datetime(2009, 1, 4, 13, 1, 15)
Then t2-t1 gives,
datetime.timedelta(2)
which is a 2 day difference--I guess. Strange.
Changing
t2=datetime.datetime.strptime("2009/01/04 14:00:30","%Y/%m/%d %H:%M:%S")
and differencing gives me,
datetime.timedelta(2, 3555), which seems to indicate a 2 day and 3555
second difference. Interesting, but I think there must be another way to
do this. Maybe not.
 
L

Lie Ryan

You're right. Y. Works fine. The produces datetime.datetime(2009, 1, 2,
13, 1, 15).
If I now use
t2=datetime.datetime.strptime("2009/01/04 13:01:15","%Y/%m/%d %H:%M:%S")
I get tw as
datetime.datetime(2009, 1, 4, 13, 1, 15)
Then t2-t1 gives,
datetime.timedelta(2)
which is a 2 day difference--I guess. Strange.

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'?
Changing
t2=datetime.datetime.strptime("2009/01/04 14:00:30","%Y/%m/%d %H:%M:%S")
and differencing gives me,
datetime.timedelta(2, 3555), which seems to indicate a 2 day and 3555
second difference. Interesting, but I think there must be another way to
do this. Maybe not.

to do... what?
 
W

W. eWatson

Lie said:
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. How does one "unload" this
structure to get the seconds and days?
to do... what?
To find the difference more clearly. Why not just return (0,2,3555)
 
R

Roy Smith

"W. eWatson said:
BTW, all times are local to my city. Same time zone.

Yes, but how much time has elapsed between "2009/0/04 13:01:15" and
"2009/06/04 13:01:15"? Even if I tell you that both timestamps were done
in the same city, you don't have enough information.

Hint #1: The answer in Sydney, Bangalore, and New York are all different.

Hint #2: Two of those cities are in temperate zones, and one is in the
tropics.

Hint #3: If you don't pay attention to this, you will be bitten twice a
year.
 
L

Lie Ryan

Hint #3: If you don't pay attention to this, you will be bitten twice a
year.

Not really. Some areas don't have DST and the answer to that is always
exactly 5 months.
 
W

W. eWatson

Roy said:
Yes, but how much time has elapsed between "2009/0/04 13:01:15" and
"2009/06/04 13:01:15"? Even if I tell you that both timestamps were done
in the same city, you don't have enough information.

Hint #1: The answer in Sydney, Bangalore, and New York are all different.

Hint #2: Two of those cities are in temperate zones, and one is in the
tropics.

Hint #3: If you don't pay attention to this, you will be bitten twice a
year.
Sort of the opposite of a stopped clock. It's right twice a day. How
does one solve the DST problem?
 
D

D'Arcy J.M. Cain

Sort of the opposite of a stopped clock. It's right twice a day. How
does one solve the DST problem?

Depends on which DST problem you have. There is more than one solution
depending on what the problem is. Store and compare in UTC and display
in local time is one solution but it may not be yours.
 
W

W. eWatson

D'Arcy J.M. Cain said:
Depends on which DST problem you have. There is more than one solution
depending on what the problem is. Store and compare in UTC and display
in local time is one solution but it may not be yours.
Actually, UTC is quite relevant here. I would guess there is some way to
use datetime for UTC?
 
W

W. eWatson

Ben said:
In both cases it produces not a function, but a ‘datetime.timedelta’
object::

<type 'datetime.timedelta'>

What you're seeing in the interactive interpreter is a string
representation of the object::

datetime.timedelta(2)

This is no different from what's going on with any other string
representation. The representation is not the value.


It's customary to consult the documentation for questions like that


Because the ‘datetime.timedelta’ type is more flexible than a tuple, and
has named attributes as documented at the above URL::

0
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:
1.0
I'd call that weird. Maybe in this case it is ... <g>
 
W

W. eWatson

This is quirky.
t1: 2009-12-05 22:11:00 <type 'datetime.datetime'>

but in the program:
import datetime

t1=datetime.datetime.strptime("20091205_221100","%Y%m%d_%H%M%S")
print "t1: ",t1, type(t1)

produces
t1: 2009-12-05 22:11:00 <type 'datetime.datetime'>

Where did the hyphens and colons come from?
 
P

Peter Otten

W. eWatson said:
This is quirky.

t1: 2009-12-05 22:11:00 <type 'datetime.datetime'>

but in the program:
import datetime

t1=datetime.datetime.strptime("20091205_221100","%Y%m%d_%H%M%S")
print "t1: ",t1, type(t1)

produces
t1: 2009-12-05 22:11:00 <type 'datetime.datetime'>

Where did the hyphens and colons come from?

print some_object

first converts some_object to a string invoking str(some_object) which in
turn calls the some_object.__str__() method. The resulting string is then
written to stdout. Quoting the documentation:

datetime.__str__()
For a datetime instance d, str(d) is equivalent to d.isoformat(' ').

datetime.isoformat([sep])
Return a string representing the date and time in ISO 8601 format,
YYYY-MM-DDTHH:MM:SS.mmmmmm or, if microsecond is 0, YYYY-MM-DDTHH:MM:SS

Peter
 
W

W. eWatson

Peter said:
W. eWatson said:
This is quirky.

datetime.datetime(2009, 12, 5, 22, 11)

t1: 2009-12-05 22:11:00 <type 'datetime.datetime'>

but in the program:
import datetime

t1=datetime.datetime.strptime("20091205_221100","%Y%m%d_%H%M%S")
print "t1: ",t1, type(t1)

produces
t1: 2009-12-05 22:11:00 <type 'datetime.datetime'>

Where did the hyphens and colons come from?

print some_object

first converts some_object to a string invoking str(some_object) which in
turn calls the some_object.__str__() method. The resulting string is then
written to stdout. Quoting the documentation:

datetime.__str__()
For a datetime instance d, str(d) is equivalent to d.isoformat(' ').

datetime.isoformat([sep])
Return a string representing the date and time in ISO 8601 format,
YYYY-MM-DDTHH:MM:SS.mmmmmm or, if microsecond is 0, YYYY-MM-DDTHH:MM:SS

Peter
So as long as I don't print it, it's datetime.datetime and I can make
calculations or perform operations on it as though it is not a string,
but a datetime object?
 
S

Steven D'Aprano

It's customary to consult the documentation for questions like that
<URL:http://docs.python.org/library/datetime.html#datetime.timedelta>.

No no no, it's customary to annoy everyone on the list by asking the
question *without* consulting the documentation, and then to be told to
Read The Fine Manual.

To be serious for a moment, if you're in the interactive interpreter, you
can get some useful information by calling help(datetime.timedelta).
 
P

Peter Otten

W. eWatson said:
Peter said:
W. eWatson said:
This is quirky.

t1=datetime.datetime.strptime("20091205_221100","%Y%m%d_%H%M%S")
t1
datetime.datetime(2009, 12, 5, 22, 11)
type(t1)
<type 'datetime.datetime'>

t1: 2009-12-05 22:11:00 <type 'datetime.datetime'>

but in the program:
import datetime

t1=datetime.datetime.strptime("20091205_221100","%Y%m%d_%H%M%S")
print "t1: ",t1, type(t1)

produces
t1: 2009-12-05 22:11:00 <type 'datetime.datetime'>

Where did the hyphens and colons come from?

print some_object

first converts some_object to a string invoking str(some_object) which in
turn calls the some_object.__str__() method. The resulting string is then
written to stdout. Quoting the documentation:

datetime.__str__()
For a datetime instance d, str(d) is equivalent to d.isoformat(' ').

datetime.isoformat([sep])
Return a string representing the date and time in ISO 8601 format,
YYYY-MM-DDTHH:MM:SS.mmmmmm or, if microsecond is 0,
YYYY-MM-DDTHH:MM:SS

Peter
So as long as I don't print it, it's datetime.datetime and I can make
calculations or perform operations on it as though it is not a string,
but a datetime object?

Not "as though", it *is* a datetime object. And it knows how to show as
something meaningful to the user when printed

These are very basic concepts that apply to all Python objects. I suggest
that you take a moment to go through the tutorial before you continue with
your efforts.

Peter
 
S

Steven D'Aprano

[...]
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:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top