Skip to content

Commit

Permalink
Rewrote the parse_date utility function to allow arbitrary specificat…
Browse files Browse the repository at this point in the history
…ion of time formats. Added timestamp to the attribute map with the twitter api time format specified. This change should be backwards compatible because the parse_date function defaults to the previously hardcoded timeestamp format.
  • Loading branch information
thomasw committed Sep 6, 2010
1 parent e2d3793 commit 92b2d5c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions pyposterous/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -135,5 +135,6 @@ class User(PosterousData):
('views', 'filesize', 'height', 'width', 'commentscount', 'num_posts', 'size', ):int, ('views', 'filesize', 'height', 'width', 'commentscount', 'num_posts', 'size', ):int,
('private', 'commentsenabled', 'primary'):lambda x: x.upper() == 'TRUE', ('private', 'commentsenabled', 'primary'):lambda x: x.upper() == 'TRUE',
('body',):lambda x: x.strip(), # Hopefully whitespace will not be significant. ('body',):lambda x: x.strip(), # Hopefully whitespace will not be significant.
('timestamp',):lambda x: parse_date(x, '%a %b %d %H:%M:%S %Y'),
('date',):lambda x: parse_date(x),} ('date',):lambda x: parse_date(x),}


43 changes: 30 additions & 13 deletions pyposterous/utils.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,23 +30,40 @@ def docstring_trim(docstring):
trimmed.pop(0) trimmed.pop(0)
# Return a single string: # Return a single string:
return '\n'.join(trimmed) return '\n'.join(trimmed)

def parse_date(time_string, time_format="%a, %d %b %Y %H:%M:%S"):
"""Converts a string representation of a time stamp to a UTC datetime object.
A utcoffset of -HHMM is expected in the time_string, but should not be
represented in the time_format string by %z. Just leave it out. This
function will find it, and do the necessary calculation to convert the
output to UTC.
time_string -- a string to be converted to a UTC datetime object. Must contain a utcoffset string +HHMM or -HHMM
time_format -- a string representing the format of time_string. See strptime documentation for an example. Leave out %z.
"""
from re import findall
from datetime import datetime


def parse_date(time_string): utc_offset_str = findall(r'\+[0-9]{4}|-[0-9]{4}', time_string)[0]
"""Returns a UTC datetime object based on a string representation of time time_string = time_string.replace(utc_offset_str, "", 1)
in this format: %a, %d %b %Y %H:%M:%S %z"""
from datetime import datetime, timedelta


utc_offset_str = time_string[-6:].strip() # the substitution above may have resulted in unnecessary whitespace
sign = 1 time_string = time_string.strip().replace(' ', ' ')
# UTC offset is negative. Trim that -!
if utc_offset_str[0] == '-': return convert_to_utc(datetime.strptime(time_string, time_format), utc_offset_str)
sign = -1
utc_offset_str = utc_offset_str[1:5]


utcoffset = sign * timedelta(hours=int(utc_offset_str[0:2]), minutes=int(utc_offset_str[2:4])) def convert_to_utc(date, offset):
from datetime import timedelta

sign = offset[0]
hour = int(offset[1:3])
minute = int(offset[3:5])

if sign == '-': sign = 1
if sign == '+': sign = -1

return date + sign * timedelta(hours=hour, minutes=minute)


return datetime.strptime(time_string[:-6], '%a, %d %b %Y %H:%M:%S') - utcoffset

def try_parse_int(number): def try_parse_int(number):
try: try:
return int(number) return int(number)
Expand Down

0 comments on commit 92b2d5c

Please sign in to comment.