Datetimes are now returned as timezone-aware.
Timezone is hardcoded as UTC for now, as Pillar always returns UTC times.
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import json
|
||||
import re
|
||||
import sys
|
||||
from datetime import datetime
|
||||
import datetime
|
||||
import time
|
||||
from contextlib import closing
|
||||
|
||||
import requests
|
||||
|
||||
try:
|
||||
@@ -27,7 +29,7 @@ class PillarJSONEncoder(json.JSONEncoder):
|
||||
# Late import to prevent circular references.
|
||||
from .resource import Resource
|
||||
|
||||
if isinstance(obj, datetime):
|
||||
if isinstance(obj, datetime.datetime):
|
||||
return obj.isoformat(' ')
|
||||
if isinstance(obj, Resource):
|
||||
return obj.to_dict()
|
||||
@@ -104,6 +106,24 @@ def merge_dict(data, *override):
|
||||
return result
|
||||
|
||||
|
||||
ZERO = datetime.timedelta(0)
|
||||
|
||||
|
||||
class UTC(datetime.tzinfo):
|
||||
"""UTC"""
|
||||
|
||||
def utcoffset(self, dt):
|
||||
return ZERO
|
||||
|
||||
def tzname(self, dt):
|
||||
return "UTC"
|
||||
|
||||
def dst(self, dt):
|
||||
return ZERO
|
||||
|
||||
utc = UTC()
|
||||
|
||||
|
||||
def convert_datetime(item):
|
||||
"""Starting from a JSON object, find and replace the _create and _updated
|
||||
keys with actual datetime objects.
|
||||
@@ -113,7 +133,9 @@ def convert_datetime(item):
|
||||
for k in keys:
|
||||
# Check if the key is in the document
|
||||
if k in item:
|
||||
item[k] = datetime.strptime(item[k], "%a, %d %b %Y %H:%M:%S %Z")
|
||||
parsed = time.strptime(item[k], "%a, %d %b %Y %H:%M:%S %Z")
|
||||
as_utc = datetime.datetime(*parsed[0:6], tzinfo=utc)
|
||||
item[k] = as_utc
|
||||
|
||||
return item
|
||||
|
||||
|
||||
Reference in New Issue
Block a user