python的datetime模块

基础知识见:

datetime

注意timestamp是一个浮点数,它没有时区的概念,而datetime是有时区的。

timestamp的值与时区毫无关系,因为timestamp一旦确定,其UTC时间就确定了,转换到任意时区的时间也是完全确定的,这就是为什么计算机存储的当前时间是以timestamp表示的,因为全球各地的计算机在任意时刻的timestamp都是完全相同的(假定时间已校准)。

# 拿到UTC时间,并强制设置时区为UTC+0:00:
utc_d = datetime.utcnow().replace(tzinfo=timezone.utc)
print(utc_d)
# astimezone()将转换时区为北京时间:
utc_dt = utc_d.astimezone(timezone(timedelta(hours=8)))
print(utc_dt)
# 东8区,也就是北京的时间戳
print(utc_dt.timestamp())
# astimezone()将转换时区为东京时间:
utc_dt = utc_d.astimezone(timezone(timedelta(hours=9)))
print(utc_dt)
# 东9区,也就是东的时间戳
print(utc_dt.timestamp())

结果:

2017-07-27 16:28:19.287400+00:00
2017-07-28 00:28:19.287400+08:00
1501172899.2874
2017-07-28 01:28:19.287400+09:00
1501172899.2874

由此看出,北京和东京差一个时区,东京早北京一个小时,但同一时刻的时间戳是一样的

实践学习

假设你获取了用户输入的日期和时间如2015-1-21 9:01:30,以及一个时区信息如UTC+5:00,均是str,请编写一个函数将其转换为timestamp:

  • 第一种形式
import re
from datetime import datetime, timezone, timedelta

def to_timestamp(dt_str, tz_str):    
    # dt_str转换为datetime格式
    dt=datetime.strptime(dt_str,'%Y-%m-%d %H:%M:%S')
    print('To datetime:',dt)
    # tz_str转换为timezone,用?表示0个或1个字符
    m = re.match(r'^(UTC)([+-]?)([0-9]|0[0-9]|1[0-2]):(00)$', tz_str)
    print('get the timezone:',m.group(3))
    sign = -1 if m.group(2) is '-' else 1
    #print(sign)
    tz = sign * int(m.group(3))
    #  创建时区
    tz_utc = timezone(timedelta(hours=tz))
    # 时间转换为UTC时间
    dt_utc = dt.replace(tzinfo=tz_utc)
    print('datetime of UTC:',dt_utc)    
    # datetime转换为timestamp
    return dt_utc.timestamp()


t1 = to_timestamp('2015-6-1 08:10:30', 'UTC+7:00')
print('the timestamp of t1:',t1)
print('...............................................')

t2 = to_timestamp('2015-5-31 16:10:30', 'UTC-09:00')
print('the timestamp of t2:',t2)

print('end')

运行结果:

To datetime: 2015-06-01 08:10:30
get the timezone: 7
datetime of UTC: 2015-06-01 08:10:30+07:00
the timestamp of t1: 1433121030.0
...............................................
To datetime: 2015-05-31 16:10:30
get the timezone: 09
datetime of UTC: 2015-05-31 16:10:30-09:00
the timestamp of t2: 1433121030.0
end
  • 第二种形式
def to_timestamp(dt_str, tz_str):
    # dt_str转换为datetime
    dt=datetime.strptime(dt_str,'%Y-%m-%d %H:%M:%S')
    # 主要是正则表达式的不同,很强大!
    UTC=int(re.match(r'^UTC([-+]*\d{1,2}):00$',tz_str).group(1))
    tz_utc=timezone(timedelta(hours=UTC))
    lastdt=dt.replace(tzinfo=tz_utc)
    timestamp=lastdt.timestamp()
    return timestamp
t1 = to_timestamp('2015-6-1 08:10:30', 'UTC+7:00')
print('the timestamp of t1:',t1)
print('...............................................')
t2 = to_timestamp('2015-5-31 16:10:30', 'UTC-09:00')
print('the timestamp of t2:',t2)
print('end')

结果:

>>> 
the timestamp of t1: 1433121030.0
...............................................
the timestamp of t2: 1433121030.0
end
>>> 
  • 第三种形式
def to_timestamp(dt_str, tz_str):
    dt = datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S')
    # 采用分割,也是正则表达式的不同
    tz_info = re.split(r'[UTC\:]+',tz_str)
    print(tz_info)
    tz_hours = int(tz_info[1])
    tz_minutes = int(tz_info[2])
    # 细分到分钟
    dt = dt.replace(tzinfo = timezone(timedelta(hours=tz_hours, minutes=tz_minutes)))
    return dt.timestamp()

t1 = to_timestamp('2015-6-1 08:10:30', 'UTC+7:00')
print('the timestamp of t1:',t1)
print('...............................................')
t2 = to_timestamp('2015-5-31 16:10:30', 'UTC-09:00')
print('the timestamp of t2:',t2)
print('end')

结果:

>>> 
['', '+7', '00']
the timestamp of t1: 1433121030.0
...............................................
['', '-09', '00']
the timestamp of t2: 1433121030.0
end
>>>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值