Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中将double转换为time_Python_Django_Datetime_Django Aggregation_Timefield - Fatal编程技术网

在python中将double转换为time

在python中将double转换为time,python,django,datetime,django-aggregation,timefield,Python,Django,Datetime,Django Aggregation,Timefield,我的django模型中有一个TimeField()。我想把这个记录的总和转换成小时 以下是我认为的实际代码: hours_week_decimal = Timesheet.objects.filter(owner = request.user.pk, week = datetime.datetime.now().isocalendar()[1]).aggregate(total=Sum('working_hour')) # this method return a dict of decimal

我的django模型中有一个TimeField()。我想把这个记录的总和转换成小时

以下是我认为的实际代码:

hours_week_decimal = Timesheet.objects.filter(owner = request.user.pk, week = datetime.datetime.now().isocalendar()[1]).aggregate(total=Sum('working_hour')) # this method return a dict of decimal
total_hours_week = convertDecimalToHours(hours_week_decimal)
以及相关的功能:

def convertDecimalToHours(times):
    total_time = 0
    for k, v in times.items():
        total_time += int(v)
    print("type: {} - value: {}".format(type(total_time), total_time))
这让我想起:

 type: int - value: 166000
我有两个小时:

 Monday (08:30) and Tuesday(08:30)
它一定返回了我
“17:00”


希望你能帮我解决这个问题:)

谢谢你的回答

在我的数据库中,我有一个类型为“Time”的列。值的存储方式如下:08:30:00.000000

在我前面的代码中,时间变量(传入参数)如下所示:


{'total':Decimal('166000.000000')}

问题是30m+30m=60m是的,是1h,但您希望计算器能够理解您想要30+30=1h 因此,在您的情况下,您必须显式地将8:30转换为8.5

用几行代码扩展示例的快速但不优雅的方法可以是:

  • 将整数转换为字符串
  • 删去小时数(位置0和1)并乘以60得到分钟数
  • 将该结果和分钟数相加(位置2和3)
在对每个时间域执行此操作后,将所有转换的时间相加,以分钟为单位,然后以小时为单位重新转换

在您的示例中:

def convertDecimalToHours(times):
total_time = 0
for k, v in times.items():
    tmp_time = str(int(v))
    minutes = int(tmp_time[2:4]) + int(tmp_time[0:2])*60
    total_time += minutes / 60 # Now your 8:30 is 8.5
print("type: {} - value: {}".format(type(total_time), total_time))
这里的输出是17。
我建议你用这个例子来理解这个概念,而不是把它当作你问题的打包解决方案。

如果你能分享这个有问题的
dict
的例子,那就容易多了。你用谷歌搜索过吗?它与此相关吗?我不知道为什么您希望像
17:00
这样的输出,而您的函数输出是
print(“type:{}-value:{}”。format(type(total_time),total_time))
,所以函数
convertDecimalToHours(times)
无法生成您想要的内容。@因此,dict的值看起来像是083000和083000,其总和为166000,但他希望输入时间为08:30和08:30,所以输出应该是170000而不是1660000是的,您所说的有意义,但从他刚才的要求来看还不太清楚。