VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > python入门教程 >
  • Python日期带时区转换工具类总结

1.背景

最近项目是国际项目,所以需要经常需要用到UTC时间和local时间的转换。
所以整理了一下时间戳工具类,方便使用。
这里主要用到的包就是datatime、time、pytz。

2. 遇到的坑

直接看测试案例

tzinfo=pytz.timezone("Asia/Shanghai")
dtStr = "2023-05-28 00:00:00"
dt1 = datetime.datetime.strptime(dtStr,"%Y-%m-%d %H:%M:%S").replace(tzinfo=tzinfo)
dt2 = dt_tz = tzinfo.localize(datetime.datetime.strptime(dtStr, '%Y-%m-%d %H:%M:%S'))
print(dt1)
print(dt2)

输出结果:

2023-05-28 00:00:00+08:06
2023-05-28 00:00:00+08:00

这里第一种方法dt1中的输出时区带有06分的差异。

3. 一些小案例

3.1 当前日期、日期时间、UTC日期时间

print(DateUtil.currDate())
print(DateUtil.currDateTime())
print(DateUtil.currDateTime("UTC"))

输出结果:

2023-05-29
2023-05-29 10:44:27
2023-05-29 02:44:27

3.2 昨天、昨天UTC日期、昨天现在这个时间点的时间戳

print(DateUtil.yesterday())
print(DateUtil.yesterdayUTC()) 
print(DateUtil.yesterday_ts())

输出结果:

2023-05-28
2023-05-28  #这里如果是早上8点以前运行的就不一样
1685242026391

3.3 日期转时间戳

print(DateUtil.date2ts("2023-05-28"))
print(DateUtil.datetime2ts("2023-05-28 10:01:01"))
print(DateUtil.datetime2ts("2023-05-28 10:01:01","UTC"))
print(DateUtil.datetime2ts("2023-05-28 10:01:01","Asia/Shanghai"))

输出结果:

1685203200000
1685239261000
1685268061000 #如果使用的是UTC格式,则时间戳不一样
1685239261000

3.4 时间戳转日期

print(DateUtil.timestamp2Date("1685239261000"))
print(DateUtil.timestamp2Date("1685239261")) # 这里自动判断是毫秒还是秒
print(DateUtil.timestamp2Date("1685239261","UTC")) # 这里指定输出的日期的时区是UTC格式
print(DateUtil.timestamp2Date(1685239261)) #int类型或者字符串都可以转行
print(DateUtil.timestamp2Date(1685239261000,"UTC")) #int类型或者字符串都可以转行

输出结果:

2023-05-28 10:01:01
2023-05-28 10:01:01
2023-05-28 02:01:01
2023-05-28 10:01:01
2023-05-28 02:01:01

3.5 日期加减、小时的加减

print(DateUtil.dateAdd("2023-05-28",1)) #加
print(DateUtil.dateAdd("2023-05-28",-1)) #减
print(DateUtil.datetimeAdd("2023-05-28 10:01:01",-1)) #日期时间的加减
print(DateUtil.hourAdd("2023-05-28 10:01:01",1)) # 小时的加减
print(DateUtil.hourAdd("2023-05-28 10:01:01",-1)) # 小时的加减

输出结果:

2023-05-29
2023-05-27
2023-05-27 10:01:01
2023-05-28 11:01:01
2023-05-28 09:01:01

4. 总结

刚开始思绪有点乱,刚好趁机会好好的整理了一遍思路。
注意: 当使用日期进行格式转换时,需要确定这个日期对应的时区。

  • 时区的设置
    python的日期中时区对应的有个属性timezone。
    我这里主要是通过pytz.timezone("时区字符串")来设置时区的。
    比如,要将日期转为时间戳,首先要指定这个日期的时区属性。
tzinfo = pytz.timezone("Asia/Shanghai")
  • 日期设置
    然后通过localize方法配置这个日期的时区。
dt_tz = tzinfo.localize(dt)

再进行其他的转换。
但是千万要注意,不要使用datetime中的.replace方法来设置时区。

# 这样设置会导致会导致输出结果`2023-05-28 00:00:00+08:06` 带有一个6分钟的差异
dt_tz =datetime.datetime.strptime("2023-05-28 00:00:00","%Y-%m-%d %H:%M:%S").replace(tzinfo=tzinfo)  

5. 完整的编码

# -*- coding: utf-8 -*-
# @Time    : 2023/3/28  10:04
# @Author  : King
# @Software: PyCharm
# @Describe: 
# -*- encoding:utf-8 -*-
import datetime
import time,pytz

_timezone_utc = "UTC"
_timezone_cn = "Asia/Shanghai"

"""
DateTime日期转时间戳(日期时区为上海)
"""
def datetime2ts(date, timezone=_timezone_cn):
    if isinstance(date, datetime.datetime):
        return int(date.timestamp() * 1000)
    else:
        date = date + " 00:00:00" if len(date) <= 10 else date
        dt = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
        dt_tz = pytz.timezone(timezone).localize(dt)
    return int(dt_tz.timestamp()*1000)

"""
DateTime日期转时间戳(日期时区为UTC)
"""
def utcDatetime2ts(date):
    return datetime2ts(date,_timezone_utc)

"""
Date日期转时间戳
"""
def date2ts(date,timezone=_timezone_cn):
    if isinstance(date,datetime.datetime):
        return int(date.timestamp()*1000)
    else:
        dt = datetime.datetime.strptime(date, '%Y-%m-%d')
        dt_tz = pytz.timezone(timezone).localize(dt)
        return int(dt_tz.timestamp()*1000)

"""
时间戳转日期
"""
def _ts2Date(timestamp,timezone,date_format="%Y-%m-%d %H:%M:%S"):
    tz = pytz.timezone(timezone)
    timestamp_s = int(timestamp) if len(str(timestamp)) <= 10 else int(timestamp)/1000
    dt_tz = datetime.datetime.fromtimestamp(timestamp_s,tz)
    return dt_tz.strftime(date_format)

"""
时间戳转日期
"""
def timestamp2Date(timestamp,timezone=_timezone_cn):
    return _ts2Date(timestamp,timezone)

"""
获取当前日期 2023-03-28
"""
def currDate(timezone=_timezone_cn):
    return datetime.datetime.now(pytz.timezone(_timezone_utc)).astimezone(pytz.timezone(timezone)).strftime('%Y-%m-%d')
"""
当前UTC日期
"""
def currUTCDate():
    return datetime.datetime.now(pytz.timezone(_timezone_utc)).strftime('%Y-%m-%d')

"""
昨日
"""
def yesterday(timezone=_timezone_cn):
    return dateAdd(currDate(timezone),-1)
"""
UTC昨日
"""
def yesterdayUTC():
    return dateAdd(currUTCDate(),-1)
"""
当前日期时间
"""
def currDateTime(timezone=_timezone_cn):
    return datetime.datetime.now(pytz.timezone(timezone)).strftime('%Y-%m-%d %H:%M:%S')

"""
当前时间戳
"""
def currTimestamp():
    return int(time.time()*1000)

"""
昨天时间戳
"""
def yesterday_ts():
    return currTimestamp() - 24*3600*1000

"""
日期的增减
"""
def dateAdd(date, n):
    dt = datetime.datetime.strptime(date, "%Y-%m-%d")
    return (dt + datetime.timedelta(days=n)).strftime("%Y-%m-%d")
"""
日期时间的增减
"""
def datetimeAdd(date,n):
    dt = datetime.datetime.strptime(date,'%Y-%m-%d %H:%M:%S')
    return (dt + datetime.timedelta(days=n)).strftime('%Y-%m-%d %H:%M:%S')

"""
小时的增减
"""
def hourAdd(date,n):
    dt = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
    return  (dt + datetime.timedelta(hours=n)).strftime('%Y-%m-%d %H:%M:%S')


"""
timezone:默认是Aisa/Shanghai时区
指定日期的上一个小时的时间戳
"""
def lastHourTimestamp(date,timezone=_timezone_cn):
    if(len(date) == 10):
        ts = date2ts(date,timezone)
    else:
        ts = datetime2ts(date,timezone)
    return ts - ts % 3600000 - 3600*1000



相关教程