关于作者

Discuss / Python / 请问^ 和$是必须的嚒?

请问^ 和$是必须的嚒?

问题来源

中大-雪山

#1 Created at ...

#!/usr/bin/env python3 #-*- coding: utf-8 -*-

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') zone = re.match(r'^UTC([+-]\d{1,2}):(\d{2})$', tz_str).group(1) tz_utc = timezone(timedelta(hours=int(zone))) utc_dt = dt.replace(tzinfo=tz_utc) return utc_dt.timestamp()

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

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

print('Pass')

廖雪峰

#2 Created at ...

整个字符串匹配是必须的,如果是部分匹配或者搜索不能加


  • 1

Reply

WARNING: You are using an old browser that does not support HTML5. Please choose a modern browser (Chrome / Microsoft Edge / Firefox / Sarafi) to get a good experience.