Discuss / Python / 顺利运行,好开心,大伙来瞧瞧我的作业~

顺利运行,好开心,大伙来瞧瞧我的作业~

Topic source

#usr/bin/env python

#-*- coding:utf-8 -*-

__author__='teddy.xiong'

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')

    H=re.match(r'^([a-zA-Z]{3})(\+|\-)(0[0-9]|[0-9]|1[0-9]|2[0-4])\:(\d{2})$',tz_str)

    hour=int(H.group(3))

    if H.group(2)=='+':

        date_time=dt-timedelta(hours=hour)

        time_stamp=date_time.timestamp()

    elif H.group(2)=='-':

        date_time=dt+timedelta(hours=hour)

        time_stamp=date_time.timestamp()

    return time_stamp

t1 = to_timestamp('2015-6-1 08:10:30', 'UTC+7:00')

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

print(t1)

print(t2)


  • 1

Reply