Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Rewrote the parse_date utility function to allow arbitrary specificat…

…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...
commit 92b2d5c473b097512b5ab73e13b0b78b1ac788be 1 parent e2d3793
Thomas Welfley authored September 06, 2010
1  pyposterous/models.py
@@ -135,5 +135,6 @@ class User(PosterousData):
135 135
     ('views', 'filesize', 'height', 'width', 'commentscount', 'num_posts', 'size', ):int,
136 136
     ('private', 'commentsenabled', 'primary'):lambda x: x.upper() == 'TRUE',
137 137
     ('body',):lambda x: x.strip(), # Hopefully whitespace will not be significant. 
  138
+    ('timestamp',):lambda x: parse_date(x, '%a %b %d %H:%M:%S %Y'),
138 139
     ('date',):lambda x: parse_date(x),} 
139 140
     
43  pyposterous/utils.py
@@ -30,23 +30,40 @@ def docstring_trim(docstring):
30 30
         trimmed.pop(0)
31 31
     # Return a single string:
32 32
     return '\n'.join(trimmed)
  33
+
  34
+def parse_date(time_string, time_format="%a, %d %b %Y %H:%M:%S"):
  35
+    """Converts a string representation of a time stamp to a UTC datetime object.
  36
+    A utcoffset of -HHMM is expected in the time_string, but should not be
  37
+    represented in the time_format string by %z. Just leave it out. This
  38
+    function will find it, and do the necessary calculation to convert the
  39
+    output to UTC.
  40
+    
  41
+    time_string -- a string to be converted to a UTC datetime object. Must contain a utcoffset string +HHMM or -HHMM
  42
+    time_format -- a string representing the format of time_string. See strptime documentation for an example. Leave out %z.    
  43
+    """
  44
+    from re import findall
  45
+    from datetime import datetime
33 46
     
34  
-def parse_date(time_string):
35  
-    """Returns a UTC datetime object based on a string representation of time
36  
-    in this format: %a, %d %b %Y %H:%M:%S %z"""
37  
-    from datetime import datetime, timedelta
  47
+    utc_offset_str = findall(r'\+[0-9]{4}|-[0-9]{4}', time_string)[0]
  48
+    time_string = time_string.replace(utc_offset_str, "", 1)
38 49
     
39  
-    utc_offset_str = time_string[-6:].strip()
40  
-    sign = 1
41  
-    # UTC offset is negative. Trim that -!
42  
-    if utc_offset_str[0] == '-':
43  
-        sign = -1
44  
-        utc_offset_str = utc_offset_str[1:5]
  50
+    # the substitution above may have resulted in unnecessary whitespace
  51
+    time_string = time_string.strip().replace('  ', ' ')
  52
+        
  53
+    return convert_to_utc(datetime.strptime(time_string, time_format), utc_offset_str)
45 54
     
46  
-    utcoffset = sign * timedelta(hours=int(utc_offset_str[0:2]), minutes=int(utc_offset_str[2:4]))
  55
+def convert_to_utc(date, offset):
  56
+    from datetime import timedelta
  57
+    
  58
+    sign = offset[0]
  59
+    hour = int(offset[1:3])
  60
+    minute = int(offset[3:5])
  61
+    
  62
+    if sign == '-': sign = 1
  63
+    if sign == '+': sign = -1
  64
+    
  65
+    return date + sign * timedelta(hours=hour, minutes=minute)
47 66
     
48  
-    return datetime.strptime(time_string[:-6], '%a, %d %b %Y %H:%M:%S') - utcoffset
49  
-
50 67
 def try_parse_int(number):
51 68
     try:
52 69
         return int(number)

0 notes on commit 92b2d5c

Please sign in to comment.
Something went wrong with that request. Please try again.