Repository: kissmetrics/py-KISSmetrics

Issue:
Support for timestamps submitted as floats
From Dylan:
"
Currently, if someone using the Python library (and potentially other libraries) tries to send in a float as a timestamp, it gets converted to something like 1426982400.0. This doesn't generate any errors, and it shows up in the live tab just fine, but any events recorded with this timestamp won't be properly processed and won't show up in a user's account.
"


Discussion:

Patch:
diff --git a/KISSmetrics/query_string.py b/KISSmetrics/query_string.py
index 4765b2c..4013d3a 100644
--- a/KISSmetrics/query_string.py
+++ b/KISSmetrics/query_string.py
@@ -43,7 +43,7 @@ def create_query(key, person, event=None, timestamp=None,
     query_dict = {KEY_KEY: key, PERSON_KEY: person}
     if timestamp:
         query_dict[TIME_FLAG_KEY] = 1
-        query_dict[TIME_KEY] = timestamp
+        query_dict[TIME_KEY] = int(timestamp)
     if event:
         query_dict[EVENT_NAME_KEY] = event
     if identity:


Test patch:
diff --git a/KISSmetrics/tests/test_kissmetrics.py b/KISSmetrics/tests/test_kissmetrics.py
index bfc5734..a15ba99 100644
--- a/KISSmetrics/tests/test_kissmetrics.py
+++ b/KISSmetrics/tests/test_kissmetrics.py
@@ -113,7 +113,7 @@ class TestKISSmetricsRequestFunctionsTestCase(unittest.TestCase):
         assert parse_qs(query_string)['_p'] == ['bar']
         assert parse_qs(query_string)['_n'] == ['fizzed']
 
-    def test_record_with_timestamp(self):
+    def test_record_with_integer_timestamp(self):
         query_string = KISSmetrics.request.record(key='foo', person='bar', event='fizzed', timestamp=1381849312)
         assert urlparse(query_string).path == '/e'
         query_string = urlparse(query_string).query
@@ -122,6 +122,24 @@ class TestKISSmetricsRequestFunctionsTestCase(unittest.TestCase):
         assert parse_qs(query_string)['_d'] == ['1']
         assert parse_qs(query_string)['_t'] == ['1381849312']
 
+    def test_record_with_whole_float_timestamp(self):
+        query_string = KISSmetrics.request.record(key='foo', person='bar', event='fizzed', timestamp=1381849312.0)
+        assert urlparse(query_string).path == '/e'
+        query_string = urlparse(query_string).query
+        assert parse_qs(query_string)['_k'] == ['foo']
+        assert parse_qs(query_string)['_p'] == ['bar']
+        assert parse_qs(query_string)['_d'] == ['1']
+        assert parse_qs(query_string)['_t'] == ['1381849312']
+
+    def test_record_with_non_whole_float_timestamp(self):
+        query_string = KISSmetrics.request.record(key='foo', person='bar', event='fizzed', timestamp=1381849312.5)
+        assert urlparse(query_string).path == '/e'
+        query_string = urlparse(query_string).query
+        assert parse_qs(query_string)['_k'] == ['foo']
+        assert parse_qs(query_string)['_p'] == ['bar']
+        assert parse_qs(query_string)['_d'] == ['1']
+        assert parse_qs(query_string)['_t'] == ['1381849312']
+
     def test_record_custom_path(self):
         query_string = KISSmetrics.request.record(key='foo', person='bar', event='fizzed', path='get')
         assert urlparse(query_string).path == '/get'
@@ -150,6 +168,28 @@ class TestKISSmetricsRequestFunctionsTestCase(unittest.TestCase):
         assert parse_qs(query_string)['_t'] == ['1381849312']
         assert parse_qs(query_string)['cool'] == ['1']
 
+    def test_set_with_whole_float_timestamp(self):
+        properties = {'cool': '1'}
+        query_string = KISSmetrics.request.set(key='foo', person='bar', properties=properties, timestamp=1381849312.0)
+        assert urlparse(query_string).path == '/s'
+        query_string = urlparse(query_string).query
+        assert parse_qs(query_string)['_k'] == ['foo']
+        assert parse_qs(query_string)['_p'] == ['bar']
+        assert parse_qs(query_string)['_d'] == ['1']
+        assert parse_qs(query_string)['_t'] == ['1381849312']
+        assert parse_qs(query_string)['cool'] == ['1']
+
+    def test_set_with_non_whole_float_timestamp(self):
+        properties = {'cool': '1'}
+        query_string = KISSmetrics.request.set(key='foo', person='bar', properties=properties, timestamp=1381849312.5)
+        assert urlparse(query_string).path == '/s'
+        query_string = urlparse(query_string).query
+        assert parse_qs(query_string)['_k'] == ['foo']
+        assert parse_qs(query_string)['_p'] == ['bar']
+        assert parse_qs(query_string)['_d'] == ['1']
+        assert parse_qs(query_string)['_t'] == ['1381849312']
+        assert parse_qs(query_string)['cool'] == ['1']
+
     def test_set_custom_path(self):
         properties = {'cool': '1'}
         query_string = KISSmetrics.request.set(key='foo', person='bar', properties=properties, path='get')