Permalink
Browse files

Merged time_utils from pypercube

  • Loading branch information...
tsileo committed Oct 1, 2013
1 parent d460731 commit 272eb3663641aecc309b49e6733023d4e0a30ed6
Showing with 113 additions and 0 deletions.
  1. +43 −0 cube/tests/test_time_utils.py
  2. +70 −0 cube/time_utils.py
@@ -0,0 +1,43 @@
+# -*- encoding: utf-8 -*-
+
+"""
+Copyright (c) 2012 Steven Buss
+Originally from:
+https://github.com/sbuss/pypercube/blob/master/tests/test_time_utils.py
+"""
+
+from datetime import datetime
+import unittest
+
+from cube import time_utils
+
+
+class TestMetric(unittest.TestCase):
+ def setUp(self):
+ self.now = datetime(2012, 7, 6, 20, 33, 16, 573225)
+
+ def test_yesterday(self):
+ self.assertEqual(time_utils.yesterday(self.now),
+ datetime(2012, 7, 5, 20, 33, 16, 573225))
+
+ def test_last_week(self):
+ self.assertEqual(time_utils.last_week(self.now),
+ datetime(2012, 6, 29, 20, 33, 16, 573225))
+
+ def test_start_of_month(self):
+ self.assertEqual(time_utils.start_of_month(self.now),
+ datetime(2012, 7, 1))
+
+ def test_floor(self):
+ self.assertEqual(time_utils.floor(self.now, time_utils.STEP_10_SEC),
+ datetime(2012, 7, 6, 20, 33, 10))
+ self.assertEqual(time_utils.floor(self.now, time_utils.STEP_1_MIN),
+ datetime(2012, 7, 6, 20, 33))
+ self.assertEqual(time_utils.floor(self.now, time_utils.STEP_5_MIN),
+ datetime(2012, 7, 6, 20, 30))
+ self.assertEqual(time_utils.floor(self.now, time_utils.STEP_1_HOUR),
+ datetime(2012, 7, 6, 20))
+ self.assertEqual(time_utils.floor(self.now, time_utils.STEP_1_DAY),
+ datetime(2012, 7, 6))
+ self.assertRaisesRegexp(ValueError, "is not a valid resolution",
+ time_utils.floor, self.now, 12345)
View
@@ -0,0 +1,70 @@
+# -*- encoding: utf-8 -*-
+
+"""
+Copyright (c) 2012 Steven Buss
+Originally from:
+https://github.com/sbuss/pypercube/blob/master/pypercube/time_utils.py
+"""
+
+from datetime import datetime
+from datetime import timedelta
+
+STEP_10_SEC = long(1e4)
+STEP_1_MIN = long(6e4)
+STEP_5_MIN = long(3e5)
+STEP_1_HOUR = long(36e5)
+STEP_1_DAY = long(864e5)
+
+STEP_CHOICES = ((STEP_10_SEC, "10 seconds"),
+ (STEP_1_MIN, "1 minute"),
+ (STEP_5_MIN, "5 minutes"),
+ (STEP_1_HOUR, "1 hour"),
+ (STEP_1_DAY, "1 day"))
+
+
+def now():
+ return datetime.utcnow()
+
+
+def yesterday(start=None):
+ if start is None:
+ start = now()
+ return start - timedelta(days=1)
+
+
+def last_week(start=None):
+ if start is None:
+ start = now()
+ return start - timedelta(days=7)
+
+
+def start_of_month(timestamp=None):
+ if not timestamp:
+ timestamp = now()
+ return datetime(year=timestamp.year, month=timestamp.month, day=1)
+
+
+def floor(start, resolution):
+ """Floor a datetime by a resolution.
+
+ >>> now = datetime(2012, 7, 6, 20, 33, 16, 573225)
+ >>> floor(now, STEP_1_HOUR)
+ datetime.datetime(2012, 7, 6, 20, 0)
+ """
+ if resolution == STEP_10_SEC:
+ return datetime(start.year, start.month, start.day, start.hour,
+ start.minute, start.second - (start.second % 10))
+ elif resolution == STEP_1_MIN:
+ return datetime(start.year, start.month, start.day, start.hour,
+ start.minute)
+ elif resolution == STEP_5_MIN:
+ return datetime(start.year, start.month, start.day, start.hour,
+ start.minute - (start.minute % 5))
+ elif resolution == STEP_1_HOUR:
+ return datetime(start.year, start.month, start.day, start.hour)
+ elif resolution == STEP_1_DAY:
+ return datetime(start.year, start.month, start.day)
+
+ raise ValueError("{resolution} is not a valid resolution. Valid choices "
+ "are {choices}".format(
+ resolution=resolution, choices=STEP_CHOICES))

0 comments on commit 272eb36

Please sign in to comment.