本文整理汇总了Python中oslo_utils.timeutils.parse_strtime方法的典型用法代码示例。如果您正苦于以下问题:Python timeutils.parse_strtime方法的具体用法?Python timeutils.parse_strtime怎么用?Python timeutils.parse_strtime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类oslo_utils.timeutils
的用法示例。
在下文中一共展示了timeutils.parse_strtime方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_service_status
# 需要导入模块: from oslo_utils import timeutils [as 别名]
# 或者: from oslo_utils.timeutils import parse_strtime [as 别名]
def get_service_status(self, context, service_id):
service = objects.Service.get(context, service_id)
last_heartbeat = (service.last_seen_up or service.updated_at or
service.created_at)
if isinstance(last_heartbeat, str):
# NOTE(russellb) If this service came in over rpc via
# conductor, then the timestamp will be a string and needs to be
# converted back to a datetime.
last_heartbeat = timeutils.parse_strtime(last_heartbeat)
else:
# Objects have proper UTC timezones, but the timeutils comparison
# below does not (and will fail)
last_heartbeat = last_heartbeat.replace(tzinfo=None)
elapsed = timeutils.delta_seconds(last_heartbeat, timeutils.utcnow())
is_up = abs(elapsed) <= CONF.service_down_time
if not is_up:
LOG.warning('Seems service %(name)s on host %(host)s is down. '
'Last heartbeat was %(lhb)s. Elapsed time is %(el)s',
{'name': service.name,
'host': service.host,
'lhb': str(last_heartbeat), 'el': str(elapsed)})
return objects.service.ServiceStatus.FAILED
return objects.service.ServiceStatus.ACTIVE
示例2: _set_status
# 需要导入模块: from oslo_utils import timeutils [as 别名]
# 或者: from oslo_utils.timeutils import parse_strtime [as 别名]
def _set_status(self, id):
service = objects.Service.get(pecan.request.context, id)
last_heartbeat = (service.last_seen_up or service.updated_at or
service.created_at)
if isinstance(last_heartbeat, six.string_types):
# NOTE(russellb) If this service came in over rpc via
# conductor, then the timestamp will be a string and needs to be
# converted back to a datetime.
last_heartbeat = timeutils.parse_strtime(last_heartbeat)
else:
# Objects have proper UTC timezones, but the timeutils comparison
# below does not (and will fail)
last_heartbeat = last_heartbeat.replace(tzinfo=None)
elapsed = timeutils.delta_seconds(last_heartbeat, timeutils.utcnow())
is_up = abs(elapsed) <= CONF.service_down_time
if not is_up:
LOG.warning('Seems service %(name)s on host %(host)s is down. '
'Last heartbeat was %(lhb)s.'
'Elapsed time is %(el)s',
{'name': service.name,
'host': service.host,
'lhb': str(last_heartbeat), 'el': str(elapsed)})
self._status = objects.service.ServiceStatus.FAILED
else:
self._status = objects.service.ServiceStatus.ACTIVE
示例3: test_event_create_get
# 需要导入模块: from oslo_utils import timeutils [as 别名]
# 或者: from oslo_utils.timeutils import parse_strtime [as 别名]
def test_event_create_get(self):
event = self.create_event(self.ctx)
ret_event = db_api.event_get(self.ctx, event.id)
self.assertIsNotNone(ret_event)
tst_timestamp = tu.parse_strtime('2014-12-19 11:51:54.670244',
'%Y-%m-%d %H:%M:%S.%f')
self.assertEqual(common_utils.isotime(tst_timestamp),
common_utils.isotime(ret_event.timestamp))
self.assertEqual('20', ret_event.level)
self.assertEqual('', ret_event.oid)
self.assertEqual('', ret_event.otype)
self.assertEqual('', ret_event.oname)
self.assertEqual('', ret_event.action)
self.assertEqual('', ret_event.status)
self.assertEqual('', ret_event.status_reason)
self.assertEqual(self.ctx.user_id, ret_event.user)
self.assertEqual(self.ctx.project_id, ret_event.project)
示例4: check_and_get_datetime
# 需要导入模块: from oslo_utils import timeutils [as 别名]
# 或者: from oslo_utils.timeutils import parse_strtime [as 别名]
def check_and_get_datetime(time, time_name):
if not time:
return None
if isinstance(time, datetime):
return time
if not isinstance(time, six.string_types):
msg = (_("The trigger %(name)s(type = %(vtype)s) is "
"not an instance of string") %
{"name": time_name, "vtype": type(time)})
raise exception.InvalidInput(msg)
try:
time = timeutils.parse_strtime(time, fmt='%Y-%m-%d %H:%M:%S')
except Exception:
msg = (_("The format of trigger %s is not correct") % time_name)
raise exception.InvalidInput(msg)
return time
示例5: test_return_valid_isoformat
# 需要导入模块: from oslo_utils import timeutils [as 别名]
# 或者: from oslo_utils.timeutils import parse_strtime [as 别名]
def test_return_valid_isoformat(self):
"""Ensure that the ec2 api returns datetime in xs:dateTime
(which apparently isn't datetime.isoformat())
NOTE(ken-pepple): https://bugs.launchpad.net/nova/+bug/721297
"""
conv = apirequest._database_to_isoformat
# sqlite database representation with microseconds
time_to_convert = timeutils.parse_strtime("2011-02-21 20:14:10.634276",
"%Y-%m-%d %H:%M:%S.%f")
self.assertEqual(conv(time_to_convert), '2011-02-21T20:14:10.634Z')
# mysqlite database representation
time_to_convert = timeutils.parse_strtime("2011-02-21 19:56:18",
"%Y-%m-%d %H:%M:%S")
self.assertEqual(conv(time_to_convert), '2011-02-21T19:56:18.000Z')
示例6: _get_dummy_event_obj
# 需要导入模块: from oslo_utils import timeutils [as 别名]
# 或者: from oslo_utils.timeutils import parse_strtime [as 别名]
def _get_dummy_event_obj(self):
return {
'resource_id': '6261579e-d6f3-49ad-8bc3-a9cb974778ff',
'resource_state': 'ACTIVE',
'resource_type': 'VNF',
'event_details': '',
'event_type': 'scale_up',
'timestamp': timeutils.parse_strtime('2016-07-20T05:43:52.765172')
}
示例7: test_to_json_with_date_format_value
# 需要导入模块: from oslo_utils import timeutils [as 别名]
# 或者: from oslo_utils.timeutils import parse_strtime [as 别名]
def test_to_json_with_date_format_value(self):
test_date = tu.parse_strtime("0001-03-08T02:00:00",
'%Y-%m-%dT%H:%M:%S')
fixture = {"date": test_date}
expected = '{"date": "0001-03-08T02:00:00"}'
actual = serializers.JSONResponseSerializer().to_json(fixture)
self.assertEqual(expected, actual)
示例8: create_event
# 需要导入模块: from oslo_utils import timeutils [as 别名]
# 或者: from oslo_utils.timeutils import parse_strtime [as 别名]
def create_event(self, ctx, timestamp=None, level=logging.INFO,
entity=None, action=None, status=None,
status_reason=None):
fake_timestamp = tu.parse_strtime(
'2014-12-19 11:51:54.670244', '%Y-%m-%d %H:%M:%S.%f')
if entity:
e_name = reflection.get_class_name(entity, fully_qualified=False)
type_name = e_name.upper()
if type_name == 'CLUSTER':
cluster_id = entity.id
elif type_name == 'NODE':
cluster_id = entity.cluster_id
else:
cluster_id = ''
else:
type_name = ''
cluster_id = ''
values = {
'timestamp': timestamp or fake_timestamp,
'level': level,
'oid': entity.id if entity else '',
'oname': entity.name if entity else '',
'otype': type_name,
'cluster_id': cluster_id,
'action': action or '',
'status': status or '',
'status_reason': status_reason or '',
'user': ctx.user_id,
'project': ctx.project_id,
}
# Make sure all fields can be customized
return db_api.event_create(ctx, values)
示例9: parse_input_params
# 需要导入模块: from oslo_utils import timeutils [as 别名]
# 或者: from oslo_utils.timeutils import parse_strtime [as 别名]
def parse_input_params(expected_input_params):
"""Parse input parameters from request.
:param expected_input_params: (array) Expected input
params specified in constants.
"""
raw_filters = _get_input_params_from_request(expected_input_params)
filters = copy.deepcopy(raw_filters)
date_fmt = CONF.api.input_date_format
for key, value in filters.items():
if key == const.START_DATE or key == const.END_DATE:
try:
filters[key] = timeutils.parse_strtime(value, date_fmt)
except (ValueError, TypeError) as exc:
raise api_exc.ParseInputsError(
'Invalid date format: %(exc)s' % {'exc': exc})
start_date = filters.get(const.START_DATE)
end_date = filters.get(const.END_DATE)
if start_date and end_date:
if start_date > end_date:
raise api_exc.ParseInputsError(
'Invalid dates: %(start)s more than %(end)s'
'' % {'start': const.START_DATE, 'end': const.END_DATE})
if const.SIGNED in filters:
if is_authenticated():
filters[const.OPENID] = get_user_id()
else:
raise api_exc.ParseInputsError(
'To see signed test results you need to authenticate')
return filters
示例10: test_parse_input_params_success
# 需要导入模块: from oslo_utils import timeutils [as 别名]
# 或者: from oslo_utils.timeutils import parse_strtime [as 别名]
def test_parse_input_params_success(self,
mock_get_user_id,
mock_is_authenticated,
mock_get_input):
fmt = '%Y-%m-%d %H:%M:%S'
self.CONF.set_override('input_date_format',
fmt,
'api')
raw_filters = {
const.START_DATE: '2015-03-26 15:04:40',
const.END_DATE: '2015-03-26 15:04:50',
const.CPID: '12345',
const.SIGNED: True
}
expected_params = mock.Mock()
mock_get_input.return_value = raw_filters
parsed_start_date = timeutils.parse_strtime(
raw_filters[const.START_DATE],
fmt
)
parsed_end_date = timeutils.parse_strtime(
raw_filters[const.END_DATE],
fmt
)
expected_result = {
const.START_DATE: parsed_start_date,
const.END_DATE: parsed_end_date,
const.CPID: '12345',
const.SIGNED: True,
const.OPENID: 'fake_id',
}
result = api_utils.parse_input_params(expected_params)
self.assertEqual(expected_result, result)
mock_get_input.assert_called_once_with(expected_params)
示例11: test_parse_strtime
# 需要导入模块: from oslo_utils import timeutils [as 别名]
# 或者: from oslo_utils.timeutils import parse_strtime [as 别名]
def test_parse_strtime(self):
perfect_time_format = self.skynet_self_aware_time_perfect_str
expect = timeutils.parse_strtime(perfect_time_format)
self.assertEqual(self.skynet_self_aware_time_perfect, expect)
示例12: test_strtime_and_back
# 需要导入模块: from oslo_utils import timeutils [as 别名]
# 或者: from oslo_utils.timeutils import parse_strtime [as 别名]
def test_strtime_and_back(self):
orig_t = datetime.datetime(1997, 8, 29, 6, 14, 0)
s = timeutils.strtime(orig_t)
t = timeutils.parse_strtime(s)
self.assertEqual(orig_t, t)
示例13: __init__
# 需要导入模块: from oslo_utils import timeutils [as 别名]
# 或者: from oslo_utils.timeutils import parse_strtime [as 别名]
def __init__(self, auth_token=None, domain_id=None,
domain_name=None, user_name=None, user_id=None,
user_domain_name=None, user_domain_id=None,
project_name=None, project_id=None, roles=None,
is_admin=None, read_only=False, show_deleted=False,
request_id=None, trust_id=None, auth_token_info=None,
all_projects=False, password=None, timestamp=None, **kwargs):
"""Stores several additional request parameters:
:param domain_id: The ID of the domain.
:param domain_name: The name of the domain.
:param user_domain_id: The ID of the domain to
authenticate a user against.
:param user_domain_name: The name of the domain to
authenticate a user against.
"""
super(RequestContext, self).__init__(auth_token=auth_token,
user_id=user_name,
project_id=project_name,
is_admin=is_admin,
read_only=read_only,
show_deleted=show_deleted,
request_id=request_id,
roles=roles)
self.user_name = user_name
self.user_id = user_id
self.project_name = project_name
self.project_id = project_id
self.domain_id = domain_id
self.domain_name = domain_name
self.user_domain_id = user_domain_id
self.user_domain_name = user_domain_name
self.auth_token_info = auth_token_info
self.trust_id = trust_id
self.all_projects = all_projects
self.password = password
if is_admin is None:
self.is_admin = policy.check_is_admin(self)
else:
self.is_admin = is_admin
if not timestamp:
timestamp = timeutils.utcnow()
if isinstance(timestamp, str):
timestamp = timeutils.parse_strtime(timestamp)
self.timestamp = timestamp
示例14: is_ec2_timestamp_expired
# 需要导入模块: from oslo_utils import timeutils [as 别名]
# 或者: from oslo_utils.timeutils import parse_strtime [as 别名]
def is_ec2_timestamp_expired(request, expires=None):
"""Checks the timestamp or expiry time included in an EC2 request
and returns true if the request is expired
"""
query_time = None
timestamp = request.get('Timestamp')
expiry_time = request.get('Expires')
def parse_strtime(strtime):
if _ms_time_regex.match(strtime):
# NOTE(MotoKen): time format for aws-sdk-java contains millisecond
time_format = "%Y-%m-%dT%H:%M:%S.%fZ"
else:
time_format = "%Y-%m-%dT%H:%M:%SZ"
return timeutils.parse_strtime(strtime, time_format)
try:
if timestamp and expiry_time:
msg = _("Request must include either Timestamp or Expires,"
" but cannot contain both")
LOG.error(msg)
raise exception.InvalidRequest(msg)
elif expiry_time:
query_time = parse_strtime(expiry_time)
return timeutils.is_older_than(query_time, -1)
elif timestamp:
query_time = parse_strtime(timestamp)
# Check if the difference between the timestamp in the request
# and the time on our servers is larger than 5 minutes, the
# request is too old (or too new).
if query_time and expires:
return (timeutils.is_older_than(query_time, expires) or
timeutils.is_newer_than(query_time, expires))
return False
except ValueError:
LOG.exception("Timestamp is invalid: ")
return True
# NOTE(ft): extra functions to use in vpc specific code or instead of
# malformed existed functions
示例15: __init__
# 需要导入模块: from oslo_utils import timeutils [as 别名]
# 或者: from oslo_utils.timeutils import parse_strtime [as 别名]
def __init__(self, user_id, project_id, request_id=None,
is_admin=None, remote_address=None,
auth_token=None, user_name=None, project_name=None,
overwrite=True, service_catalog=None, api_version=None,
is_os_admin=None, **kwargs):
"""Parameters
:param overwrite: Set to False to ensure that the greenthread local
copy of the index is not overwritten.
:param kwargs: Extra arguments that might be present, but we ignore
because they possibly came in from older rpc messages.
"""
user = kwargs.pop('user', None)
tenant = kwargs.pop('tenant', None)
super(RequestContext, self).__init__(
auth_token=auth_token,
user=user_id or user,
tenant=project_id or tenant,
is_admin=is_admin,
request_id=request_id,
resource_uuid=kwargs.pop('resource_uuid', None),
overwrite=overwrite)
# oslo_context's RequestContext.to_dict() generates this field, we can
# safely ignore this as we don't use it.
kwargs.pop('user_identity', None)
self.session = kwargs.pop('session', None)
if kwargs:
LOG.warning('Arguments dropped when creating context: %s',
str(kwargs))
self.user_id = user_id
self.project_id = project_id
self.remote_address = remote_address
timestamp = timeutils.utcnow()
if isinstance(timestamp, six.string_types):
timestamp = timeutils.parse_strtime(timestamp)
self.timestamp = timestamp
self.service_catalog = service_catalog
if self.service_catalog is None:
# if list is empty or none
self.service_catalog = []
self.user_name = user_name
self.project_name = project_name
self.is_admin = is_admin
# TODO(ft): call policy.check_is_admin if is_admin is None
self.is_os_admin = is_os_admin
self.api_version = api_version