简体   繁体   English

将datetime.datetime与datetime.time相结合 - TypeError:需要一个整数

[英]combining datetime.datetime with datetime.time - TypeError: an integer is required

I am currently attempting to iterate through some data contained in an SQL request cursor, alter the type of some of the data into "datetime.time" and then combine that with another variable into a new variable named "datetime_db". 我目前正在尝试迭代SQL请求游标中包含的一些数据,将某些数据的类型更改为“datetime.time”,然后将其与另一个变量组合成一个名为“datetime_db”的新变量。

I have two variables named "date" and "nextDay" which have been previously defined earlier in my code. 我有两个名为“date”和“nextDay”的变量,这些变量之前已在我的代码中定义过。 The previously mentioned "datetime.time" will be combined with either "date" or "nextDay" depending on certain conditions. 前面提到的“datetime.time”将与“date”或“nextDay”组合,具体取决于某些条件。

My code is as follows: 我的代码如下:

for (date_db,time_db,price) in cursor:
    time_db = datetime.datetime.strptime(time_db,"%H:%M:%S").time()
    price = float(price)

    if (date_db == date):
        datetime_db = datetime.datetime.combine(datetime.date(date), datetime.time(time_db))

    else:
        datetime_db = datetime.datetime.combine(datetime.date(nextDay), datetime.time(time_db))

This throws up the following error: 这会引发以下错误:

File "C:/Users/Stuart/PycharmProjects/untitled/Apache - Copy.py", line 82, in <module>
datetime_db = datetime.datetime.combine(datetime.date(date), datetime.time(time_db))
TypeError: an integer is required

When I print out the "type()" for the 3 variables involved I get the following: 当我为所涉及的3个变量打印出“type()”时,我得到以下内容:

time_db = <type 'datetime.time'> date = <type 'datetime.datetime'> nextDay = <type 'datetime.datetime'>

Is there any obvious reason why this is not working? 有什么明显的原因导致这种情况无效吗? I have tried changing the type of "date" and "nextDay" to a "datetime.date" but that makes no difference. 我尝试将“date”和“nextDay”的类型更改为“datetime.date”,但这没有任何区别。

Could someone suggest how I may combine these two variables successfully? 有人可以建议我如何成功地将这两个变量结合起来吗?

You cannot convert a datetime to date by the constructor: 您无法通过构造函数将datetime date转换为datetime

>>> datetime.date(datetime.datetime.now())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required

Same goes for datetime.time constructor; datetime.time构造函数也是如此; it would not accept another time object as a parameter. 它不会接受另一个time对象作为参数。

Now, you need to have a date and a time to use the datetime.combine ; 现在,你需要有一个datetime来使用datetime.combine ; time_db is an instance of class time already, but your date (unfortunate name there for this explanation) is of type datetime . time_db已经是类time的实例,但是你的date (这个解释的不幸名称)是datetime类型。 Well, that date part of date can be extracted with datetime.date method: 那么, date部分date可以用datetime.date方法提取:

>>> dt = datetime.datetime.now()
>>> dt
datetime.datetime(2015, 2, 20, 22, 21, 22, 806109)
>>> dt.date()
datetime.date(2015, 2, 20)

thus you can do: 因此你可以这样做:

datetime.datetime.combine(date.date(), time_db)

From the documentation for combine : combine文件

datetime_db = datetime.datetime.combine(date.date(), time_db.timetz())

This assumes that both date and time_db are datetime objects, which they appear to be. 这假设datetime_db都是datetime对象,它们看起来是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM