详解Django将秒转换为xx天xx时xx分

yipeiwu_com5年前Python基础

Django将秒转换为xx天xx时xx分,具体代码如下所示:

from django.utils.translation import ngettext_lazy as _n

def humanize_seconds(secs):
  a_day = 86400
  an_hour = 3600
  a_minute = 60
  timetot = ''
  total_secs = secs
  if secs > a_day: # 60sec * 60min * 24hrs
    days = int(secs // a_day)
    # timetot += "{} {}".format(int(days), _('days'))
    timetot += _n('%(num)s day', '%(num)s days', days) % {'num': days}
    secs = secs - days * a_day

  if secs > an_hour:
    hrs = int(secs // an_hour)
    # timetot += " {} {}".format(int(hrs), _('hours'))
    timetot += ' '
    timetot += _n('%(num)s hour', '%(num)s hours', hrs) % {'num': hrs}
    secs = secs - hrs * an_hour

  if secs > a_minute and total_secs < a_day:
    mins = int(secs // a_minute)
    timetot += ' '
    timetot += _n('%(num)s minute', '%(num)s minutes', mins) % {'num': mins}
    secs = secs - mins * a_minute

  if secs > 0 and total_secs < an_hour:
    secs = int(secs)
    timetot += ' '
    timetot += _n('%(num)s second', '%(num)s seconds', secs) % {'num': secs}
  return timetot

if __name__ == "__main__":
  print(humanize_seconds(360200))

知识点扩展:django 将model转换为字典

from django.forms.models import model_to_dict
from projects.models import ProjectInformation

site = ProjectInformation.objects.get(id=6)
dict = model_to_dict(site)
dict
{'CRFmethod': '',
 'EDCprovider': '',
 'acceptancenum': '',
 'add_time': datetime.datetime(2017, 4, 20, 8, 4, 42, 751202, tzinfo=<UTC>),
 'begindate': None,
 'clinicalassis': '',
 'clinicalnum': '',
 'created_by': '',
 'created_date': None,
 'enddate': None,
 'ethicsreviewdate': None,
 'ethicsreviewpers': '',
 'ethicsreviewres': '',
 'ethicsreviewunit': '',
 'id': 6,
 'isimport': None,
 'leaderunit': None,
 'localcases': None,
 'medicalequipment': '',
 'mequipmenttype': '',
 'multicenter': '',
 'plannum': '',
 'proenname': '爱上地方',
 'proname': '打士大夫',
 'prostatus': '',
 'prosummary': '',
 'protype': '打是否',
 'regstudy': '是',
 'reportdate': None,
 'reportnum': '',
 'reportversion': '',
 'researchdesign': '',
 'researchtype': '',
 'responsible': '',
 'studytype': '器械类',
 'telephonenum': None,
 'totalcases': None,
 'treatmenttype': None,
 'unitnum': None}

总结

以上所述是小编给大家介绍的Django将秒转换为xx天xx时xx分,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

跟老齐学Python之玩转字符串(3)

字符串就是一个话题中心。 给字符串编号 在很多很多情况下,我们都要对字符串中的每个字符进行操作(具体看后面的内容),要准确进行操作,必须做的一个工作就是把字符进行编号。比如一个班里面有5...

使用python对多个txt文件中的数据进行筛选的方法

使用python对多个txt文件中的数据进行筛选的方法

一、问题描述 筛选出多个txt文件中需要的数据 二、数据准备 这是我自己建立的要处理的文件,里面是随意写的一些数字和字母 三、程序编写 import os def eachFi...

Python3+OpenCV2实现图像的几何变换(平移、镜像、缩放、旋转、仿射)

Python3+OpenCV2实现图像的几何变换(平移、镜像、缩放、旋转、仿射)

前言 总结一下最近看的关于opencv图像几何变换的一些笔记. 这是原图: 1.平移 import cv2 import numpy as np img = cv2.im...

python实现在IDLE中输入多行的方法

在python命令行模式下,在IDLE中输入多行,例如if  else 使用tab的方式,控制缩进 在最后,连续两个回车,表示结束 >>> if stat...

python数据封装json格式数据

最简单的使用方法是: >>> import simplejson as json >>> json.dumps(['foo', {'bar':...