Discuss / Python / 出

状态空间

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

def to_timestamp(dt_str, tz_str):

    cday = datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S')

    re_utc = re.compile(r'^UTC(\+|\-)(0[0-9]|1[0-9]2[0-3]|[0-9])\:[0-5][0-9]$')

    p_or_n, h = re_utc.match(tz_str).group(1), re_utc.match(tz_str).group(2)

    tz = timezone(timedelta(hours = int(h) * (1*(p_or_n == '+')+(-1)*(p_or_n == '-'))))

    dt = cday.replace(tzinfo=tz)

    ts = dt.timestamp()

    return ts


  • 1

Reply