import pytz

import datetime

import calendar


s = '2012/01/10 23:00'

def get_timezone_offset_ms(timezone_name):

    try:

        tz = pytz.timezone(timezone_name)

    except Exception as e:

        return 0

    timezone_offset = datetime.datetime.now(tz).strftime('%z')

    offset_sign = timezone_offset[0:1]

    offset_hour = int(offset_sign+timezone_offset[1:3])

    offset_min = int(offset_sign+timezone_offset[3:])

    offset_ms = offset_hour*60*60*1000 + offset_min*60*1000

    return offset_ms


def get_pst_timestamp_from_time_string(time_string, time_format):

    pst_timezone_name = "America/Los_Angeles"

    tz_offset_ms = get_timezone_offset_ms(pst_timezone_name)

    t = datetime.datetime.strptime(time_string, time_format)

    return long(calendar.timegm(t.timetuple())*1000) - tz_offset_ms


get_pst_timestamp_from_time_string(s, '%Y/%m/%d %H:%M')

'python' 카테고리의 다른 글

gunicorn vs uwsgi  (0) 2017.01.20
flask async response  (0) 2017.01.04
functools.wraps에 대해  (0) 2015.04.15
Apple Push Notification Service(APNs) python modules  (0) 2015.04.07
Shallow copy VS Deep copy  (0) 2014.08.27
블로그 이미지

시간을 거스르는자

,