[Tutor] deltatime difficulty

greg whittier greg at thewhittiers.com
Fri Sep 19 02:49:33 CEST 2008


On Thu, Sep 18, 2008 at 8:38 PM, Wayne Watson
<sierra_mtnview at sbcglobal.net>wrote:

>  What's the problem here. It seems right to me. line 9 is diff =...
>
> import time
> from datetime import datetime
>
> You've imported the datetime class from the datetime module.


>
> def adjust_ftime(afilename, sec):
>     # Vyyyymmdd_hhmmss+tag, seconds in, new yyyymmdd_hhmmss out
>     ts = afilename[1:-7]      # use time stamp portion
>     format = '%Y%m%d_%H%M%S'
>     d = datetime(*(time.strptime(ts, format)[0:6]))
>     print "sec: ", sec, type(d)
>     diff = datetime.timedelta(seconds = sec)
>
> As the output below tells you, the datetime class doesn't have a
"timedelta" method.  This is a variation on the same confusion between the
datetime module and the datetime.datetime class you've posted before.

You could fix this above by doing "from datetime import datetime, timedelta"
and then doing

diff = timedelta(seconds = sec)

or be more explicit as use "import datetime" and then reference
datetime.datetime and datetime.timedelta.  (Another alternative is "import
datetime as dt" and then dt.datetime and dt.timedelta.)



...

>
> Results:
> sec:  33 <type 'datetime.datetime'>
> Traceback (most recent call last):
>   File
> "C:/Sandia_Meteors/Improved_Sentinel/Sentinel_Playground/Utility_Dev/junk.py",
> line 14, in ?
>     adjust_ftime('v20080120_000020.xx.dat', 33)
>   File
> "C:/Sandia_Meteors/Improved_Sentinel/Sentinel_Playground/Utility_Dev/junk.py",
> line 9, in adjust_ftime
>     diff = datetime.timedelta(seconds = sec)
> AttributeError: type object 'datetime.datetime' has no attribute
> 'timedelta'
>

Hope this helps,
Greg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080918/92b62010/attachment.htm>


More information about the Tutor mailing list