yuliji
9/26/2017 - 6:39 AM

convert time to utc time

import pytz
from pytz import timezone

def localtime_to_utctime(py_datetime):
    if not hasattr(settings, 'TIME_ZONE'):
        logger.error('Missing config TIMEZONE')
        return py_datetime

    localzone = timezone(settings.TIME_ZONE)
    localized_time = localzone.localize(py_datetime)
    utc_time = localized_time.astimezone(pytz.utc)
    return utc_time