2014-07-17 68 views
1

我正在使用下面的代碼從mysql獲取名爲fajr_begins的時間實體。我能夠打印fajr_begins它的格式是:5:27:00,我如何將它轉換爲Python中的時間格式,以便我可以操縱時間並添加15分鐘?使用python從mysql獲取時間

cnx = mysql.connector.connect(host=mysql_remote_host, user=mysql_remote_host_user, password=mysql_remote_host_password, database=mysql_remote_host_database) 
cursor = cnx.cursor() 
cursor.execute("select * from prayertimes where DATE(date) = DATE(NOW())") 
results = cursor.fetchall() 
id, date, fajr_begins, fajr_jamaat, sunrise, zuhr_begins, zuhr_jamaat, asr_begins, asr_jamaat, maghrib_jamaat, isha_begins, isha_jamaat = results[0] 
cursor.close() 
print fajr_begins 

回答

0

您必須使用strptime將其轉換。 timedelta將用於添加分鐘。

>>> from datetime import datetime, timedelta 
>>> a = "05:27:00" 
>>> b = datetime.strptime(a, "%H:%M:%S") 
>>> print b 
1900-01-01 05:27:00 
>>> c = timedelta(minutes=15) 
>>> print c 
0:15:00 
>>> print b+c 
1900-01-01 05:42:00 
+0

有Python中的時間實例,而不日期 – Ossama

+0

是,Python有'time'模塊,但ü不能做到像'add'分鐘,小時,天等.... timedelta不能添加timetuple直接操作。 – Nilesh

+0

謝謝,所以我想我需要轉換回時間一次我加15分鐘 – Ossama