Commits

Anonymous committed a8be5d9

Optionally handle timestamps in milliseconds

Comments (0)

Files changed (2)

         if len(parts) != 5:
             raise ValueError('The given "__utmz" cookie value is invalid.')
 
-        self.creation_time = datetime.utcfromtimestamp(float(parts[1]))
+        self.creation_time = utils.convert_ga_timestamp(parts[1])
         self.response_count = int(parts[3])
         params = parts[4].split(Campaign.CAMPAIGN_DELIMITER)
 
             raise ValueError('The given "__utmb" cookie value is invalid.')
 
         self.track_count = int(parts[1])
-        self.start_time = datetime.utcfromtimestamp(float(parts[3]))
+        self.start_time = utils.convert_ga_timestamp(parts[3])
 
         return self
 
             raise ValueError('The given "__utma" cookie value is invalid.')
 
         self.unique_id = int(parts[1])
-        self.first_visit_time = datetime.utcfromtimestamp(float(parts[2]))
-        self.previous_visit_time = datetime.utcfromtimestamp(float(parts[3]))
-        self.current_visit_time = datetime.utcfromtimestamp(float(parts[4]))
+        self.first_visit_time = utils.convert_ga_timestamp(parts[2])
+        self.previous_visit_time = utils.convert_ga_timestamp(parts[3])
+        self.current_visit_time = utils.convert_ga_timestamp(parts[4])
         self.visit_count = int(parts[5])
 
         return self
 import re
 import urllib
 import os
+from datetime import datetime
 
 __author__ = "Arun KR (kra3) <the1.arun@gmail.com>"
 __license__ = "Simplified BSD"
 RE_GA_ACCOUNT_ID = re.compile(r'^(UA|MO)-[0-9]*-[0-9]*$')
 RE_FIRST_THREE_OCTETS_OF_IP = re.compile(r'^((\d{1,3}\.){3})\d{1,3}$')
 
+def convert_ga_timestamp(timestamp_string):
+    timestamp = float(timestamp_string)
+    if timestamp > ((2 ** 31) - 1):
+        timestamp /= 1000
+    return datetime.utcfromtimestamp(timestamp)
+
 def get_32bit_random_num():
     return randint(0, 0x7fffffff)