Discuss / Python / 复杂了点,不过能匹配正确的时间和计算分钟

复杂了点,不过能匹配正确的时间和计算分钟

Topic source

天命破凰

#1 Created at ... [Delete] [Delete and Lock User]

import re from datetime import datetime, timezone, timedelta

def to_timestamp(dt_str, tz_str): oldtime = datetime.strptime(dt_str,'%Y-%m-%d %H:%M:%S') muban = re.compile(r'([a-zA-Z]*)([+,-])([0-9]|0[1-9]|1[0-9]|2[0-4])[:]([0-5][0-9])') jihe = muban.match(tz_str).groups() xiaoshi = int(jihe[1]+jihe[2]) fenzhong = int(jihe[1]+jihe[3]) tz_utc = timezone(timedelta(hours=xiaoshi,minutes=fenzhong)) dt = oldtime.replace(tzinfo=tz_utc) return dt.timestamp()


  • 1

Reply