目录
  • 开发中常用的日期操作还有哪些?
  • 我们看看这两个模块。
    • 时间处理中的类型转换:struct_time vs str
    • 时间与字符串转换
  • 总结

    前篇我们稍微学习了python中时间的获取,这次继续学习日期的时区转换,格式化等等。

    开发中常用的日期操作还有哪些?

    • 时区转换显示
    • 日期格式化
    • 秒数 与 日期 与 字符串的转换

    我们经常会用到,比如全球化的业务根据不同客户显示不同时间(格式等)

    在python 主要有下面两个模块涵盖了常用日期处理

    import time
    import calender
    

    我们看看这两个模块。

    时间处理中的类型转换:struct_time vs str

    python中创建一个时间,具体来说创建一个struct_time 需要一个9个元素的元组来构造。

    asctime 函数帮我们把这种类型的时间格式化为字符串。

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @time : 2021/11/10 22:49 上午
    # @author : leixuewei
    # @csdn/juejin/wechat: 雷学委
    # @xueweitag: codingdemo
    # @file : createtime.py
    # @project : hello
    
    import time
    # fixed time: time.struct_time(tm_year=2021, tm_mon=11, tm_mday=10, tm_hour=22, tm_min=55, tm_sec=11, tm_wday=16, tm_yday=16, tm_isdst=16)
    the9fields = (2021, 11, 10, 22, 55, 11, 16, 16, 16)
    fixed = time.struct_time(the9fields)
    print("fixed time:", fixed)
    print("type:", type(fixed))
    result = time.asctime(the9fields)  # 类似struct_time,需要9个元素构成的元组参数。
    print("asc time:", result)
    print("type:", type(result))
    localtime = time.localtime()
    print("local time:", localtime)
    print("type:", type(localtime))
    print("asc time:", time.asctime(localtime))
    

    运行效果如下:

    这个ticks就是从0时刻计算,至今的秒数累计。

    可以隔一秒运行这个程序,每次ticks值加上1(近似)

    指定输入来构造时间:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @time : 2021/11/10 22:49 上午
    # @author : leixuewei
    # @csdn/juejin/wechat: 雷学委
    # @xueweitag: codingdemo
    # @file : createtime.py
    # @project : hello
    
    import time
    #fixed time: time.struct_time(tm_year=2021, tm_mon=11, tm_mday=10, tm_hour=22, tm_min=55, tm_sec=11, tm_wday=16, tm_yday=16, tm_isdst=16)
    fixed = time.struct_time((2021, 11, 10, 22, 55, 11, 16, 16, 16))
    print("fixed time:", fixed)
    

    运行效果如下:

    时间与字符串转换

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @time : 2021/11/10 22:49 上午
    # @author : leixuewei
    # @csdn/juejin/wechat: 雷学委
    # @xueweitag: codingdemo
    # @file : createtime2.py
    # @project : hello
    
    import time
    sec = 3600  # 纪元开始后的一个小时(gmt 19700101凌晨)
    #
    gmtime = time.gmtime(sec)
    print("gmtime:", gmtime)  # gmt
    print("type:", type(gmtime))
    print(time.strftime("%b %d %y %h:%m:%s", gmtime))
    print(time.strftime("%y-%m-%d %h:%m:%s %z", gmtime))  # 打印日期加上时区
    print("*" * 16)
    localtime = time.localtime(sec)
    print("localtime:", localtime)  # 本地时间
    print("type:", type(localtime))
    print(time.strftime("%b %d %y %h:%m:%s", localtime))
    print(time.strftime("%y-%m-%d %h:%m:%s %z", localtime))  # 打印日期加上时区
    #试试其他格式
    print(time.strftime("%d", localtime))
    print(time.strftime("%t", localtime))
    

    下面是运行结果:

    对于时间格式化函数(strftime) 它并不理会你传入的时间(struct_time)是哪个时区的,照样给你输出,也是正确的。

    但是我们写程序拿数据的时候,必须把时区信息原样返回到用户端,或者是ui端,最后由客户端本地时区设置进行调整显示。

    最后看看,日期文本转换为日期(struct_time).

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @time : 2021/11/10 7:49 上午
    # @author : leixuewei
    # @csdn/juejin/wechat: 雷学委
    # @xueweitag: codingdemo
    # @file : createtime4.py
    # @project : hello
    import time
    print("strptime1:", time.strptime("jan 01 1970 09:00:00", "%b %d %y %h:%m:%s"))
    print("strptime2:", time.strptime("1970-01-01 09:00:00", "%y-%m-%d %h:%m:%s"))
    print("strptime3:", time.strptime("1970-01-01 09:00:00 cst", "%y-%m-%d %h:%m:%s %z"))
    

    下面是运行结果:

    总结

    python 日期处理挺多把戏的,换个格式打印/转换,结果就不一样了。

    本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注www.887551.com的更多内容!