详解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设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

django数据模型(Model)的字段类型解析

字段类型(Field types) 1、AutoField 它是一个根据 ID 自增长的 IntegerField 字段。通常,你不必直接使用该字段。如果你没在别的字段上指定主 键,Dj...

Python实现的rsa加密算法详解

Python实现的rsa加密算法详解

本文实例讲述了Python实现的rsa加密算法。分享给大家供大家参考,具体如下: 算法过程 1. 随意选择两个大的质数p和q,p不等于q,计算N=pq。 2. 根据欧拉函数,不大于N且与...

Python字典实现简单的三级菜单(实例讲解)

如下所示: data = { "北京":{ "昌平":{"沙河":["oldboy","test"],"天通苑":["链接地产","我爱我家"]}, "朝阳":{"望京":...

python利用itertools生成密码字典并多线程撞库破解rar密码

脚本功能: 利用itertools生成密码字典(迭代器形式) 多线程并发从密码字典中取出密码进行验证 验证成功后把密码写入文件中保存 #!/usr/bin/env...

python 实现图片旋转 上下左右 180度旋转的示例

如下所示: #首先建好一个数据_ud文件夹 import PIL.Image as img import os path_old = "C:/Users/49691/Desktop/...