Discuss / Python / 第一次交作业

第一次交作业

Topic source

liupzone

#1 Created at ... [Delete] [Delete and Lock User]
# _*_coding:utf-8_*_

from datetime import datetime,timedelta,timezone
import re



def to_timestamp(dt_str,tz_str):
    re_utctz = re.compile(r'(^\w{3})(\+|\-)(\d+):(\d\d)$')
    re_result = re_utctz.match(tz_str)
    if re_result:
        re_utc = re_result.group(1)
        re_v = re_result.group(2)
        re_hh = re_result.group(3)
        re_mm = re_result.group(4)
    else:
        return


    if len(re_hh) == 2:
        str_tz = '%s%s%s' % (re_v,re_hh,re_mm)
    if len(re_hh) == 1:
        str_tz = '%s0%s%s' % (re_v,re_hh,re_mm)

    dt_str1 = dt_str+' '+str_tz

    tm = datetime.strptime(dt_str1,'%Y-%m-%d %H:%M:%S %z')

    print(tm.timestamp())


if __name__ == '__main__':
    to_timestamp('2015-5-31 16:10:30', 'UTC-09:00')
    to_timestamp('2015-6-1 08:10:30', 'UTC+7:00')

  • 1

Reply