[FIX] pingen: convert datetimes to UTC before storing them

This commit is contained in:
Guewen Baconnier @ Camptocamp
2012-12-10 12:14:01 +01:00
parent d3c4c760e8
commit 8f14db9103
2 changed files with 29 additions and 4 deletions

View File

@@ -23,7 +23,9 @@ import requests
import logging
import urlparse
import json
import pytz
from datetime import datetime
from requests.packages.urllib3.filepost import encode_multipart_formdata
_logger = logging.getLogger(__name__)
@@ -37,6 +39,23 @@ POST_SENDING_STATUS = {
400: 'Sending cancelled',
}
DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S' # this is the format used by pingen API
TZ = pytz.timezone('Europe/Zurich') # this is the timezone of the pingen API
def pingen_datetime_to_utc(dt):
""" Convert a date/time used by pingen.com to UTC timezone
:param dt: pingen date/time as string (as received from the API)
to convert to UTC
:return: datetime in the UTC timezone
"""
utc = pytz.utc
dt = datetime.strptime(dt, DATETIME_FORMAT)
localized_dt = TZ.localize(dt, is_dst=True)
return localized_dt.astimezone(utc)
class PingenException(RuntimeError):
"""There was an ambiguous exception that occurred while handling your