0

When I run my py script I receive the following error:

Traceback (most recent call last):
File "report_monitor.py", line 17, in <module>
timedelta(hours=7).time())
AttributeError: 'datetime.timedelta' object has no attribute 'time'

The code is below:

#!/usr/bin/python

import time
import MySQLdb
import datetime 
from datetime import timedelta

db =      
MySQLdb.connect("localhost","root","password","DB")
cursor = db.cursor() 
cursor.execute("SELECT * FROM reportPref WHERE lastDate LIKE '2015-11-    
18%'")
records = cursor.fetchall()
for record in records:
rec = record[14]     #index 14 is a datetime object.  there is no problem here.
schedLogTime =  
(datetime.datetime.combine(datetime.date(1,1,1),rec.time()) -
timedelta(hours=7).time()) 

print schedLogTime
db.close()

What am I missing? This is driving me bonkers...!

Dimpermanence
  • 47
  • 1
  • 9

2 Answers2

1

Python is trying to do this:

timedelta(hours=7).time()

...which is invalid because as the error indicates, there's no such thing as a time method on a timedelta object. What you probably meant to do was:

(datetime.datetime.combine(datetime.date(1,1,1),rec.time()) -
 timedelta(hours=7)).time()

...which will first subtract a timedelta from the datetime, then get the time of the resulting datetime object. (Note the move of the close parenthesis compared to your code.)

glibdud
  • 7,550
  • 4
  • 27
  • 37
  • this code returns schedLogTime = (datetime.datetime.combine(datetime.date(1,1,1),rec.time()) - datetime.timedelta(hours=6)).time() OverflowError: date value out of range – Dimpermanence Nov 18 '15 at 18:17
  • What if you change it to `datetime.date(1900,1,1)`? – glibdud Nov 18 '15 at 18:18
  • Wow...that seems to be working. What does changing the 1900 do? – Dimpermanence Nov 18 '15 at 18:20
  • @Dimpermanence The lowest year that can be represented in a datetime is 1 (see [the `datetime` docs](https://docs.python.org/2.7/library/datetime.html)). You're starting there and then attempting to subtract time from it, resulting in Python trying to build a `datetime` for year 0, which is invalid. – glibdud Nov 18 '15 at 18:22
  • So this isn't performing like a "since current epoch" calculation or something like that? – Dimpermanence Nov 18 '15 at 18:23
  • @Dimpermanence `date` and `datetime` objects work on actual calendar years. Effectively, their epoch is midnight January 1, 1AD. – glibdud Nov 18 '15 at 18:24
  • Subtle. It is working. Thanks for untangling me. I vow to get better at this. I always do, except I'm working with a bunch of DBs that have different timestamps so that clock arithmetic is meticulous and hairy for me as a bit of a noob. – Dimpermanence Nov 18 '15 at 18:28
0

Aside from the obvious, that timedelta objects don't have that method, timedelta isn't a time, it's a vector. It gives you differences in time. That's why it's timedelta (delta meaning something akin to 'change in').

It makes no sense to say "what time is a 1 hour duration"

If you want to subtract 7 hours, just do:

timedeltaobj = datetime.timedelta(hours=7)
var = datetimeobj - timedeltaobj

Here's a handy little chart from the datetime module docs

enter image description here

Glibud hit the nail on the head in terms of what you were probably trying to do.

NotAnAmbiTurner
  • 2,553
  • 2
  • 21
  • 44
  • Is there a better way to do clock arithmetic with a datetime time object? – Dimpermanence Nov 18 '15 at 18:06
  • Not that I'm aware of. It's pretty easy once you get the hang of it... I actually use it all the time. It's also awesome because you can actually use dates as iterators, it handles leap years and such things automatically, etc.. Just take a look at the chart I put into my answer. You were trying to call `.time()` on a `timedelta` object, but I think glibud is right in that what you really wanted was to call `.time()` on a `datetime` object. – NotAnAmbiTurner Nov 18 '15 at 18:15
  • For me your code returns: TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.timedelta' – Dimpermanence Nov 18 '15 at 18:16
  • Sorry, I meant to write `datetimeobj - timedeltaobj`. [That should work.](https://repl.it/B1Tg/1) – NotAnAmbiTurner Nov 18 '15 at 18:18