-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimeconverter.py
executable file
·31 lines (27 loc) · 1012 Bytes
/
timeconverter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import time
import datetime
class TimeConverter:
# cassandra accepts yyyy-mm-dd format
def __init__(self):
self.dateformat = '%Y-%m-%d'
def toDate(self, x):
return time.strftime(self.dateformat, time.gmtime(int(x)))
def toDatetime(self, s):
y, m, d = tuple([int(s) for s in s.split('-')])
return datetime.date(y, m, d)
def toTimebucket(self, time_string):
parts = time_string.split('-')
return '-'.join(parts[:1] + ['01', '01'])
import unittest
class TestTimeConverterMethods(unittest.TestCase):
def test_date(self):
c = TimeConverter()
self.assertEqual(c.toDate(1462238456), '2016-05-03')
def test_timebucket(self):
c = TimeConverter()
self.assertEqual(c.toTimebucket('2016-05-03'), '2016-01-01')
def test_datetime(self):
c = TimeConverter()
self.assertEqual(c.toDatetime('2016-05-03').month, 5)
if __name__ == '__main__':
unittest.main()