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 calendar
|
||||||
import datetime
|
import datetime
|
||||||
import decimal
|
import decimal
|
||||||
import time
|
|
||||||
|
|
||||||
|
|
||||||
def dt_to_decimal(utc):
|
def dt_to_decimal(utc):
|
||||||
|
@ -5,28 +5,30 @@ from stacktach import datetime_to_decimal as dt
|
|||||||
|
|
||||||
|
|
||||||
def str_time_to_unix(when):
|
def str_time_to_unix(when):
|
||||||
if 'T' in when:
|
if 'Z' in when:
|
||||||
try:
|
when = _try_parse(when, ["%Y-%m-%dT%H:%M:%SZ", "%Y-%m-%dT%H:%M:%S.%fZ"])
|
||||||
# Old way of doing it
|
elif 'T' in when:
|
||||||
when = datetime.datetime.strptime(when, "%Y-%m-%dT%H:%M:%S.%f")
|
when = _try_parse(when, ["%Y-%m-%dT%H:%M:%S.%f", "%Y-%m-%dT%H:%M:%S"])
|
||||||
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
|
|
||||||
else:
|
else:
|
||||||
try:
|
when = _try_parse(when, ["%Y-%m-%d %H:%M:%S.%f", "%Y-%m-%d %H:%M:%S"])
|
||||||
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
|
|
||||||
|
|
||||||
return dt.dt_to_decimal(when)
|
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):
|
def is_uuid_like(val):
|
||||||
try:
|
try:
|
||||||
converted = str(uuid.UUID(val))
|
converted = str(uuid.UUID(val))
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
# IN THE SOFTWARE.
|
# IN THE SOFTWARE.
|
||||||
|
|
||||||
import mox
|
import mox
|
||||||
|
import decimal
|
||||||
|
|
||||||
from stacktach import utils as stacktach_utils
|
from stacktach import utils as stacktach_utils
|
||||||
from utils import INSTANCE_ID_1
|
from utils import INSTANCE_ID_1
|
||||||
@ -61,3 +62,32 @@ class StacktachUtilsTestCase(StacktachBaseTestCase):
|
|||||||
def test_is_message_id_like_invalid(self):
|
def test_is_message_id_like_invalid(self):
|
||||||
uuid = "$-^&#$"
|
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