将 2 1/2 小时添加到 python 中的时间对象 [英] adding 2 1/2 hours to a time object in python

查看:31
本文介绍了将 2 1/2 小时添加到 python 中的时间对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够将从 sql 数据库接收到的时间对象上的时间转换为 python.这是我使用的当前 python 代码,没有任何转换.我需要给时间加上 2 又 1/2 小时.

I need to be able to convert time on an time object I recieve from a sql database into python. Here is the current python code I am using without any conversions. I need to add 2 and 1/2 hours to the time.

def getLastReport(self, sql):



    self.connectDB()
    cursor.execute(sql)
    lastReport = cursor.fetchall()

    date = lastReport[0][0].strftime('%Y-%m-%d %H:%M UTC')

    dataset_id = int(lastReport[0][1])

    cursor.close()
    DATABASE.close()
    return date, dataset_id  

推荐答案

你看过 datetime 模块了吗?http://docs.python.org/library/datetime.html

Have you looked at the datetime module? http://docs.python.org/library/datetime.html

将您的 SQL 时间转换为日期时间,并创建一个 2.5 小时的 timedelta 对象.然后将两者相加.

Convert your SQL time into a datetime, and make a timedelta object of 2.5 hours. Then add the two.

from datetime import datetime

dt = datetime.strptime( date, '%Y-%m-%d %H:%M' )
dt_plus_25 = dt + datetime.timedelta( 0, 2*60*60 + 30*60 )

这篇关于将 2 1/2 小时添加到 python 中的时间对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆