Python循环语句介绍
659 2023-04-03 03:47:59
前篇我们稍微学习了Python中时间的获取,这次继续学习日期的时区转换,格式化等等。
我们经常会用到,比如全球化的业务根据不同客户显示不同时间(格式等)
在python 主要有下面两个模块涵盖了常用日期处理
?12import
time
import
calender
Python中创建一个时间,具体来说创建一个struct_time 需要一个9个元素的元组来构造。
asctime 函数帮我们把这种类型的时间格式化为字符串。
?12345678910111213141516171819202122#!/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(近似)
指定输入来构造时间:
?12345678910111213#!/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).
?123456789101112#!/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 日期处理挺多把戏的,换个格式打印/转换,结果就不一样了。
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!
您可能感兴趣的文章: