-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from singingwolfboy/datetime-attrs
Convert timestamp fields to datetime fields
- Loading branch information
Showing
9 changed files
with
222 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from __future__ import division | ||
from datetime import datetime | ||
|
||
|
||
def timestamp_from_dt(dt, epoch=datetime(1970, 1, 1)): | ||
""" | ||
Convert a datetime to a timestamp. | ||
https://stackoverflow.com/a/8778548/141395 | ||
""" | ||
delta = dt - epoch | ||
# return delta.total_seconds() | ||
return delta.seconds + delta.days * 86400 | ||
|
||
|
||
def convert_datetimes_to_timestamps(data, datetime_attrs): | ||
""" | ||
Given a dictionary of data, and a dictionary of datetime attributes, | ||
return a new dictionary that converts any datetime attributes that may | ||
be present to their timestamped equivalent. | ||
""" | ||
if not data: | ||
return data | ||
|
||
new_data = {} | ||
for key, value in data.items(): | ||
if key in datetime_attrs and isinstance(value, datetime): | ||
new_key = datetime_attrs[key] | ||
new_data[new_key] = timestamp_from_dt(value) | ||
else: | ||
new_data[key] = value | ||
|
||
return new_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -264,7 +264,8 @@ def mock_messages(mocked_responses, api_url, account_id): | |
} | ||
], | ||
"starred": False, | ||
"unread": True | ||
"unread": True, | ||
"date": 1265077342, | ||
}, { | ||
"id": "1238", | ||
"subject": "Test Message 2", | ||
|
@@ -278,7 +279,8 @@ def mock_messages(mocked_responses, api_url, account_id): | |
} | ||
], | ||
"starred": False, | ||
"unread": True | ||
"unread": True, | ||
"date": 1265085342, | ||
}, { | ||
"id": "12", | ||
"subject": "Test Message 3", | ||
|
@@ -292,7 +294,8 @@ def mock_messages(mocked_responses, api_url, account_id): | |
} | ||
], | ||
"starred": False, | ||
"unread": False | ||
"unread": False, | ||
"date": 1265093842, | ||
} | ||
]) | ||
endpoint = re.compile(api_url + '/messages') | ||
|
@@ -369,7 +372,11 @@ def mock_threads(mocked_responses, api_url, account_id): | |
"id": "abcd" | ||
}], | ||
"starred": True, | ||
"unread": False | ||
"unread": False, | ||
"first_message_timestamp": 1451703845, | ||
"last_message_timestamp": 1483326245, | ||
"last_message_received_timestamp": 1483326245, | ||
"last_message_sent_timestamp": 1483232461, | ||
} | ||
]) | ||
endpoint = re.compile(api_url + '/threads') | ||
|
@@ -395,7 +402,11 @@ def mock_thread(mocked_responses, api_url, account_id): | |
"id": "abcd" | ||
}], | ||
"starred": True, | ||
"unread": False | ||
"unread": False, | ||
"first_message_timestamp": 1451703845, | ||
"last_message_timestamp": 1483326245, | ||
"last_message_received_timestamp": 1483326245, | ||
"last_message_sent_timestamp": 1483232461, | ||
} | ||
response_body = json.dumps(base_thrd) | ||
|
||
|
@@ -451,7 +462,11 @@ def mock_labelled_thread(mocked_responses, api_url, account_id): | |
"account_id": account_id, | ||
"object": "label" | ||
} | ||
] | ||
], | ||
"first_message_timestamp": 1451703845, | ||
"last_message_timestamp": 1483326245, | ||
"last_message_received_timestamp": 1483326245, | ||
"last_message_sent_timestamp": 1483232461, | ||
} | ||
response_body = json.dumps(base_thread) | ||
|
||
|
@@ -881,10 +896,17 @@ def mock_events(mocked_responses, api_url): | |
"title": "Pool party", | ||
"location": "Local Community Pool", | ||
"participants": [ | ||
"Alice", | ||
"Bob", | ||
"Claire", | ||
"Dot", | ||
{ | ||
"comment": None, | ||
"email": "[email protected]", | ||
"name": "Kelly Nylanaut", | ||
"status": "noreply", | ||
}, { | ||
"comment": None, | ||
"email": "[email protected]", | ||
"name": "Sarah Nylanaut", | ||
"status": "no", | ||
}, | ||
] | ||
} | ||
]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.