Discuss / Python / 可以处理半个时区

可以处理半个时区

Topic source

cacaty

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

def to_timestamp(dt_str, tz_str):

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

    match = re.match(r'UTC([\+\-]\d+):(\d+)', tz_str)

    if match:

        h  = int(match.group(1))

        m  = int(match.group(2))

        tz = timezone(timedelta(hours = h , minutes = m))

        dt = dt.replace(tzinfo= tz)

    return dt.timestamp()

cacaty

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

UTC符号当为负时,UTC-1:30

def to_timestamp(dt_str, tz_str):

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

    match = re.match(r'UTC([\+\-])(\d+):(\d+)', tz_str)

    if match:

        h  = int(match.group(1) + match.group(2))

        m = int(match.group(1) + match.group(3))

        tz = timezone(timedelta(hours = h , minutes = m))

        dt = dt.replace(tzinfo= tz)

    return dt.timestamp()


  • 1

Reply