Eric Palakovich Carr avatar Eric Palakovich Carr committed bbb8a63

Updated _parse_datestring to handle new date strings like 2012-03-11T17:01:41.000Z.

Comments (0)

Files changed (2)

storages/backends/s3boto.py

 
 # Dates returned from S3's API look something like this:
 # "Sun, 11 Mar 2012 17:01:41 GMT"
+# -- Edit 12/19/2012
+# The documentation now shows the dates getting returned like so:
+#   2012-03-11T17:01:41.000Z
+# See here: http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGET.html#RESTBucketGET-responses-examples-sample-response
 MONTH_NAMES = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
-DATESTR_RE = re.compile(r"^.+, (?P<day>\d{1,2}) (?P<month_name>%s) (?P<year>\d{4}) (?P<hour>\d{1,2}):(?P<minute>\d{1,2}):(?P<second>\d{1,2}) (GMT|UTC)$" % ("|".join(MONTH_NAMES)))
+OLD_DATESTR_RE = re.compile(r"^.+, (?P<day>\d{1,2}) (?P<month_name>%s) (?P<year>\d{4}) (?P<hour>\d{1,2}):(?P<minute>\d{1,2}):(?P<second>\d{1,2}) (GMT|UTC)$" % ("|".join(MONTH_NAMES)))
+NEW_DATESTR_RE = re.compile(r"^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})T(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})\.\d{3}Z$")
 def _parse_datestring(dstr):
     """
     Parse a simple datestring returned by the S3 API and returns
     """
     # This regular expression and thus this function
     # assumes the date is GMT/UTC
-    m = DATESTR_RE.match(dstr)
+    m = OLD_DATESTR_RE.match(dstr)
+    if not m:
+        m = NEW_DATESTR_RE.match(dstr)
     if m:
         # This code could raise a ValueError if there is some
         # bad data or the date is invalid.
         datedict = m.groupdict()
+        month = datedict.get("month") or int(MONTH_NAMES.index(datedict['month_name'])) + 1
         utc_datetime = datetime(
             int(datedict['year']),
-            int(MONTH_NAMES.index(datedict['month_name'])) + 1,
+            int(month),
             int(datedict['day']),
             int(datedict['hour']),
             int(datedict['minute']),

storages/tests/s3boto.py

 import mock
 from uuid import uuid4
 from urllib2 import urlopen
+import datetime
+import time
 
 from django.test import TestCase
 from django.core.files.base import ContentFile
         self.assertTrue('2.txt' in files, 
                         """ "2.txt" not in files list "%s".""" % (files,))
 
+    def test__parse_datestring_old_type(self):
+        datestring = "Sun, 11 Mar 2012 17:01:41 GMT"
+        # NOTE: dstfix because _parse_datestring will return different results
+        #       if dst in effect or not when test is ran.
+        dstfix = datetime.timedelta(hours=1 if time.daylight else 1)
+        expected_datetime = datetime.datetime(2012, 3, 11, 17, 1, 41) - \
+                              datetime.timedelta(seconds=time.timezone)
+        expected_datetimes = [expected_datetime, expected_datetime + dstfix]
+        actual_datetime = s3boto._parse_datestring(datestring).replace(microsecond=0)
+        self.assertTrue(actual_datetime in expected_datetimes)
+
+    def test__parse_datestring_new_type(self):
+        datestring = "2012-03-11T17:01:41.000Z"
+        # NOTE: dstfix because _parse_datestring will return different results
+        #       if dst in effect or not when test is ran.
+        dstfix = datetime.timedelta(hours=1 if time.daylight else 1)
+        expected_datetime = datetime.datetime(2012, 3, 11, 17, 1, 41) - \
+                              datetime.timedelta(seconds=time.timezone)
+        expected_datetimes = [expected_datetime, expected_datetime + dstfix]
+        actual_datetime = s3boto._parse_datestring(datestring).replace(microsecond=0)
+        self.assertTrue(actual_datetime in expected_datetimes)
+
     #def test_storage_size(self):
     #    name = self.prefix_path('test_storage_size.txt')
     #    content = 'new content'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.