Added support for glance time format in str_time_to_unix()
This commit is contained in:
parent
221237366b
commit
ef9868e4d9
@ -1,7 +1,6 @@
|
||||
import calendar
|
||||
import datetime
|
||||
import decimal
|
||||
import time
|
||||
|
||||
|
||||
def dt_to_decimal(utc):
|
||||
|
@ -5,28 +5,30 @@ from stacktach import datetime_to_decimal as dt
|
||||
|
||||
|
||||
def str_time_to_unix(when):
|
||||
if 'T' in when:
|
||||
try:
|
||||
# Old way of doing it
|
||||
when = datetime.datetime.strptime(when, "%Y-%m-%dT%H:%M:%S.%f")
|
||||
except ValueError:
|
||||
try:
|
||||
# Old way of doing it, no millis
|
||||
when = datetime.datetime.strptime(when, "%Y-%m-%dT%H:%M:%S")
|
||||
except Exception, e:
|
||||
print "BAD DATE: ", e
|
||||
if 'Z' in when:
|
||||
when = _try_parse(when, ["%Y-%m-%dT%H:%M:%SZ", "%Y-%m-%dT%H:%M:%S.%fZ"])
|
||||
elif 'T' in when:
|
||||
when = _try_parse(when, ["%Y-%m-%dT%H:%M:%S.%f", "%Y-%m-%dT%H:%M:%S"])
|
||||
else:
|
||||
try:
|
||||
when = datetime.datetime.strptime(when, "%Y-%m-%d %H:%M:%S.%f")
|
||||
except ValueError:
|
||||
try:
|
||||
when = datetime.datetime.strptime(when, "%Y-%m-%d %H:%M:%S")
|
||||
except Exception, e:
|
||||
print "BAD DATE: ", e
|
||||
when = _try_parse(when, ["%Y-%m-%d %H:%M:%S.%f", "%Y-%m-%d %H:%M:%S"])
|
||||
|
||||
return dt.dt_to_decimal(when)
|
||||
|
||||
|
||||
def _try_parse(when, formats):
|
||||
last_exception = None
|
||||
for format in formats:
|
||||
try:
|
||||
when = datetime.datetime.strptime(when, format)
|
||||
parsed = True
|
||||
except Exception, e:
|
||||
parsed = False
|
||||
last_exception = e
|
||||
if parsed:
|
||||
return when
|
||||
print "Bad DATE ", last_exception
|
||||
|
||||
|
||||
def is_uuid_like(val):
|
||||
try:
|
||||
converted = str(uuid.UUID(val))
|
||||
|
@ -19,6 +19,7 @@
|
||||
# IN THE SOFTWARE.
|
||||
|
||||
import mox
|
||||
import decimal
|
||||
|
||||
from stacktach import utils as stacktach_utils
|
||||
from utils import INSTANCE_ID_1
|
||||
@ -60,4 +61,33 @@ class StacktachUtilsTestCase(StacktachBaseTestCase):
|
||||
|
||||
def test_is_message_id_like_invalid(self):
|
||||
uuid = "$-^&#$"
|
||||
self.assertFalse(stacktach_utils.is_request_id_like(uuid))
|
||||
self.assertFalse(stacktach_utils.is_request_id_like(uuid))
|
||||
|
||||
def test_str_time_to_unix(self):
|
||||
self.assertEqual(
|
||||
stacktach_utils.str_time_to_unix("2013-05-15T11:51:11Z"),
|
||||
decimal.Decimal('1368618671'))
|
||||
|
||||
self.assertEqual(
|
||||
stacktach_utils.str_time_to_unix("2013-05-15T11:51:11.123Z"),
|
||||
decimal.Decimal('1368618671.123'))
|
||||
|
||||
self.assertEqual(
|
||||
stacktach_utils.str_time_to_unix("2013-05-15T11:51:11"),
|
||||
decimal.Decimal('1368618671'))
|
||||
|
||||
self.assertEqual(
|
||||
stacktach_utils.str_time_to_unix("2013-05-15T11:51:11.123"),
|
||||
decimal.Decimal('1368618671.123'))
|
||||
|
||||
self.assertEqual(
|
||||
stacktach_utils.str_time_to_unix("2013-05-15 11:51:11"),
|
||||
decimal.Decimal('1368618671'))
|
||||
|
||||
self.assertEqual(
|
||||
stacktach_utils.str_time_to_unix("2013-05-15 11:51:11.123"),
|
||||
decimal.Decimal('1368618671.123'))
|
||||
|
||||
with self.assertRaises(Exception):
|
||||
stacktach_utils.str_time_to_unix("invalid date"),
|
||||
decimal.Decimal('1368618671')
|
||||
|
Loading…
x
Reference in New Issue
Block a user