Difference Between Two datetimes

S

Steven D'Aprano

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?

No, it remains a datetime object regardless of whether you print it or
not. Printing doesn't turn the object into a string, it leaves the object
as-is and produces an additional string suitable for printing.

This is no different from any other object: if you print a dict, or a
int, or a list, the object doesn't turn into a string. When you say
"print x", Python has no idea what information is appropriate to display
for some arbitrary object x. So it asks x what is appropriate, by calling
the __str__ method. That way Python only needs to know how to print one
data type: strings.
 
S

Steven D'Aprano

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.

In fairness to the OP, that's a misleading way of describing it. Python
doesn't convert objects in the standard senses of "convert lead into
gold" or "convert to <insert name of religion here>". some_object.__str__
returns an independent string object while leaving some_object alone.

But I'm sure you knew that already -- it's only the OP who was confused.
 
W

W. eWatson

Steven said:
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).
Yes, thanks. I'm starting to catch on to the idea there are tools like
dir, help, doc sources, and ___dcc__ that can help. It doesn't seem to
be standard practice to more or less teach the environment that Python
is in. If they do, it's jumbled around. Most books start with Python
itself and skirt the issues of the environment and interaction. Oddly,
today I found a source that gets right into these concepts. It may have
something to do with MIT. Here's a link to one of the three section of
the reference
<http://docs.google.com/leaf?id=0B2o...ZmY5YS00ZWQxLThkNWMtZmJkMmU1MWM1OTcx&cindex=2>.


BTW, I had looked at some Python doc that seems to be apart from the
reference above. So I'm not entirely remiss on this. I do look first.
However, on the other hand, regarding the reference, 29 pages is a bit
steep for any document.
 
S

Steve Holden

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?

It's nothing to do with whether you print it or not. When you print it,
an equivalent string is produced, and the string is what gets printed.
This is necessary, because humans only understand character-based output.
<type 'float'>

The type of something is unchanged by printing it. The print statement
merely calls methods of the object to produce strings that it can print.

People talk loosely about "converting an object to a string", but that
doesn't mean transmogrifying the object to something else, it means
producing an equivalent value of some other type which can be used
instead of the original object for some specific purpose. In this case,
printing.

regards
Steve
 
S

Steve Ferg

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?

Pyfdate http://www.ferg.org/pyfdate/index.html
has a numsplit function that should do the trick:
http://www.ferg.org/pyfdate/tutorial.html#contents_item_14

It splits a string into its numeric parts and return a list containing
the numeric parts converted to ints.
from pyfdate import *
numsplit("2007_10_09") [2007, 10, 9]
numsplit("2007-10-09T23:45:59") [2007, 10, 9, 23, 45, 59]
numsplit("2007/10/09 23.45.59")
[2007, 10, 9, 23, 45, 59]
 

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

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,511
Latest member
svareza

Latest Threads

Top