python -- time datetime小结

[TOC]

python — time datetime小结

time

基本

import time
time.time()
Out[3]: 1561107970.0175698 # 时间戳

time.localtime()
Out[4]: time.struct_time(tm_year=2019, tm_mon=6, tm_mday=21, tm_hour=17, tm_min=6, tm_sec=20, tm_wday=4, tm_yday=172, tm_isdst=0) # 本地时间带格式 time.struct_time

time.gmtime()
Out[5]: time.struct_time(tm_year=2019, tm_mon=6, tm_mday=21, tm_hour=9, tm_min=6, tm_sec=35, tm_wday=4, tm_yday=172, tm_isdst=0)# UTC时间带格式 time.struct_time

time.localtime().tm_yday
Out[6]: 172 # time.struct_time可以获取你想要的东西

转换 — 时间戳和struct time

time_stamp = time.time()
local_struct_time = time.localtime(time_stamp) # 时间戳转struct time
time.mktime(local_struct_time)
Out[13]: 1561108315.0 # struct time 转时间戳

转换 — 字符串和struct time

time.strftime("%Y-%m-%d, %H:%M:%S, %w", local_struct_time)
Out[16]: '2019-06-21, 17:11:55, 5'
==================================================================================
time.strptime('2019-06-21, 17:11:55, 5', "%Y-%m-%d, %H:%M:%S, %w")
Out[17]: time.struct_time(tm_year=2019, tm_mon=6, tm_mday=21, tm_hour=17, tm_min=11, tm_sec=55, tm_wday=4, tm_yday=172, tm_isdst=-1)

转换 — 格式化字符串和时间戳 — 不可以

time.strftime("%Y-%m-%d, %H:%M:%S, %w", time.time())
Traceback (most recent call last):
  File "C:\Users\zhicfeng\Envs\etisalat\lib\site-packages\IPython\core\interactiveshell.py", line 2910, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-15-fa1c7b40ca19>", line 1, in <module>
    time.strftime("%Y-%m-%d, %H:%M:%S, %w", time.time())
TypeError: Tuple or struct_time argument required

datetime

The time module is written in C, and is mostly interface to low-level operating system functions. The datetime module is written in Python.

基本

类关系图

object
    timedelta # 1. 日期和时间差值计算 2. timezone的第一个参数
    tzinfo # 抽象类
        timezone # datetime会用到
    time # 没啥大用
    date # 没啥大用
        datetime # 常用

timedelta 和 timezone

class timedelta:
    def __new__(cls, days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0):
        pass
=======================================================================
dongbaqu = timedelta(hours=8) # 东八区
timezone(dongbaqu)
datetime.datetime.now(timezone.utc) # utc时间
Out[39]: datetime.datetime(2019, 6, 21, 9, 46, 30, 593920, tzinfo=datetime.timezone.utc)
datetime.datetime.now(timezone(dongbaqu)) # 东八区时间
Out[40]: datetime.datetime(2019, 6, 21, 17, 46, 34, 817637, tzinfo=datetime.timezone(datetime.timedelta(0, 28800)))

datetime

datetime.datetime.now(timezone(dongbaqu)).year
Out[41]: 2019
=======================================================================
datetime.datetime(2018,12,12,12,12,12)
Out[51]: datetime.datetime(2018, 12, 12, 12, 12, 12)

转换 — datetime和字符串

datetime.datetime.now(timezone(dongbaqu)).strftime('%a, %d %b %Y %H:%M:%S +0000')
Out[42]: 'Fri, 21 Jun 2019 17:49:35 +0000'

datetime.datetime.strptime('Fri, 21 Jun 2019 17:49:35 +0000', '%a, %d %b %Y %H:%M:%S +0000')
Out[44]: datetime.datetime(2019, 6, 21, 17, 49, 35)

转换 — datetime和时间戳

datetime.datetime.now().timestamp()
Out[49]: 1561110815.027726

datetime.datetime.fromtimestamp(1561110815.027726)
Out[52]: datetime.datetime(2019, 6, 21, 17, 53, 35, 27726)

时间计算

datetime.datetime(2019, 6, 21, 17, 53, 35, 27726) + datetime.timedelta(hours=25)
Out[54]: datetime.datetime(2019, 6, 22, 18, 53, 35, 27726)

django 中的timezone

from django.utils import timezone

django 中提供了 timezone

django settings 中提供了 USE_TZ, TIMEZONE等参数.

个人不建议使用django中的timezone — 会让代码变乱. 这些信息是给框架使用的. — 譬如 serializers.DateTimeField

最佳实践

后端和数据库统一使用UTC计时,并将时区传给前端.由前端负责显示.

datetime.datetime.now(timezone.utc) # utc时间
Out[39]: datetime.datetime(2019, 6, 21, 9, 46, 30, 593920, tzinfo=datetime.timezone.utc)
    原文作者:chaleaoch
    原文地址: https://segmentfault.com/a/1190000019641574
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞