捋一捋python日期时间处理(下) 开发中常用的日期操作还

「这是我参与11月更文挑战的第9天,活动详情查看:2021最后一次更文挑战

正式的Python专栏第33篇,同学站住,别错过这个从0开始的文章!

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

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

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

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

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

1
2
arduino复制代码import time
import calender

我们看看这两个模块。

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
perl复制代码#!/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))

运行效果如下:

屏幕快照 2021-11-12 上午12.14.14.png

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

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

指定输入来构造时间:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
python复制代码#!/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)

运行效果如下:

屏幕快照 2021-11-12 上午12.14.14.png

时间与字符串转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
perl复制代码#!/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))

下面是运行结果:

屏幕快照 2021-11-12 上午1.10.05.png

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

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

总结

Python 日期处理还是挺充足的,多练习练习吧。

对了,喜欢Python的朋友,请关注学委的 Python基础专栏 or Python入门到精通大专栏

持续学习持续开发,我是雷学委!

编程很有趣,关键是把技术搞透彻讲明白。

欢迎关注微信,点赞支持收藏!

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%