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):
    dt = datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S')
    pattern = re.compile(r'^UTC([+-]*\s*\d+):(\d{2})$')
    match =  pattern.match(tz_str)
    tz_hours = match.group(1)
    tz_minutes = match.group(2)
    tz = timezone(timedelta(hours= int(tz_hours), minutes= int(tz_minutes)))
    tz_dt = dt.replace(tzinfo= tz)
    timestamp = tz_dt.timestamp()
    return timestamp

  • 1

Reply