[Tutor] stfftime question

Kent Johnson kent37 at tds.net
Tue Jul 4 13:13:35 CEST 2006


Tom Tucker wrote:
> I found a temporary solution.  The goal in the end was to compare two
> dates/times and  retrieve the millisecond delta between the two.
> 
> Work around
> #############
> import datetime
> import time
> t1 = datetime.datetime(1973,9,4,04,3,25,453)
> t2 = datetime.datetime(1973,9,4,04,3,25,553)
> t1tuple = time.mktime(t1.timetuple())+(t1.microsecond/1000.)
> t2tuple = time.mktime(t2.timetuple())+(t2.microsecond/1000.)
> delta  = (t2tuple - t1tuple) * 1000
> print delta

You could also subtract the datetimes directly to get a timedelta:
In [13]: t1 = datetime.datetime(1973,9,4,04,3,25,453)

In [14]: t2 = datetime.datetime(1973,9,4,04,3,25,553)

In [15]: diff = t2-t1

In [16]: diff
Out[16]: datetime.timedelta(0, 0, 100)

In [17]: diff.microseconds
Out[17]: 100

or if the diff can be bigger use
((diff.days * 24*60*60) * diff.seconds) * 1000 + diff.microseconds

> On 7/4/06, Tom Tucker <tktucker at gmail.com> wrote:
>> Below is an example of me converting a datetime to milliseconds on a
>> Mac running Pythong 2.3.5.  The same working code on a Solaris system
>> with Python 2.3.2 fails.  Any thoughts? What arguments am I missing?
>>
>>
>>
>> From my Mac
>> #############
>> Python 2.3.5 (#1, Oct  5 2005, 11:07:27)
>> [GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> import datetime
>>>>> dtstr = datetime.datetime(1973,9,4,04,3,25,453)
>>>>> output = dtstr.strftime('%s.%%03d') % (dtstr.microsecond)
>>>>> print output
>> 115977805.453

I think you want '%S.%%03d' as the format string (uppercase S). %s is 
not a standard format and it is probably handled differently on Mac OS 
and Solaris. What is the result of dtstr.strftime('%s.%%03d') on each 
machine? On Windows I get
In [11]: dtstr.strftime('%s.%%03d')
Out[11]: '.%03d'

Perhaps Solaris just passes the unknown format to output, that would 
give the error you see.

Kent

>>
>>
>> From Work (Solaris)
>> ################
>> Python 2.3.2 (#1, Nov 17 2003, 22:32:28)
>> [GCC 2.95.3 20010315 (release)] on sunos5
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> import datetime
>>>>> dtstr = datetime.datetime(1973,9,4,04,3,25,453)
>>>>> output = dtstr.strftime('%s.%%03d') % (dtstr.microsecond)
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in ?
>> TypeError: not enough arguments for format string
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 




More information about the Tutor mailing list