详解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实现按任意键继续执行程序

在windows下写bat的时候,通过pause命令,可以暂停程序运行,例如经常见的程序会在终端提示”按任意键继续……”,用户在终端回车后程序可以接着运行,这个功能有多大用途今天暂且不说...

Python实现输出程序执行进度百分比的方法

Python实现输出程序执行进度百分比的方法

本文实例讲述了Python实现输出程序执行进度百分比的方法。分享给大家供大家参考,具体如下: 对于一些大型的Python程序,我们需要在命令行输出其百分比,显得更加友好,以免被人误会程序...

Django框架中间件定义与使用方法案例分析

Django框架中间件定义与使用方法案例分析

本文实例讲述了Django框架中间件定义与使用方法。分享给大家供大家参考,具体如下: 1. 定义 Django中的中间件是一个轻量级、底层的插件系统,可以介入Django的请求和响应处理...

Python中bisect的用法

本文实例讲述了Python中bisect的用法,是一个比较常见的实用技巧。分享给大家供大家参考。具体分析如下: 一般来说,Python中的bisect用于操作排序的数组,比如你可以在向一...

树莓派使用python-librtmp实现rtmp推流h264的方法

目的是能使用Python进行rtmp推流,方便在h264帧里加入弹幕等操作。 librtmp使用的是0.3.0,使用树莓派noir官方摄像头适配的。 通过wireshark抓ffmpeg...