Created
March 22, 2014 22:04
-
-
Save tupton/9715119 to your computer and use it in GitHub Desktop.
Formatter for Reporter App dates
This file contains hidden or 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
import datetime | |
import pytz | |
def format_report_date(date_or_nsdate): | |
formatted = None | |
if type(date_or_nsdate) is float: | |
try: | |
reference_date = datetime.datetime(2001, 1, 1, 0, 0, 0, 0, pytz.utc).strftime('%s') | |
formatted = datetime.datetime.fromtimestamp(long(reference_date) + date_or_nsdate) | |
except e: | |
pass | |
else: | |
offset = date_or_nsdate[-5:] | |
offset = map(int, [offset[:3], offset[-2:]]) | |
offset_delta = datetime.timedelta(hours=offset[0], minutes=offset[1]) | |
formatted = datetime.datetime.strptime(date_or_nsdate[:-5], '%Y-%m-%dT%H:%M:%S') + offset_delta | |
return formatted |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment