本文整理汇总了Python中isodatetime.parsers.TimePointParser.get_info方法的典型用法代码示例。如果您正苦于以下问题:Python TimePointParser.get_info方法的具体用法?Python TimePointParser.get_info怎么用?Python TimePointParser.get_info使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类isodatetime.parsers.TimePointParser
的用法示例。
在下文中一共展示了TimePointParser.get_info方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_unix_time_from_time_string
# 需要导入模块: from isodatetime.parsers import TimePointParser [as 别名]
# 或者: from isodatetime.parsers.TimePointParser import get_info [as 别名]
def get_unix_time_from_time_string(datetime_string):
"""Convert a datetime string into a unix timestamp.
The datetime_string must match DATE_TIME_FORMAT_EXTENDED above,
which is the extended ISO 8601 year-month-dayThour:minute:second format,
plus a valid ISO 8601 time zone. For example, 2016-09-07T11:21:00+01:00,
2016-12-25T06:00:00Z, or 2016-12-25T06:00:00+13.
isodatetime is not used to do the whole parsing, partly for performance,
but mostly because the calendar may be in non-Gregorian mode.
"""
try:
date_time_utc = datetime.strptime(
datetime_string, DATE_TIME_FORMAT_EXTENDED + "Z")
except ValueError:
global PARSER
if PARSER is None:
from isodatetime.parsers import TimePointParser
PARSER = TimePointParser()
time_zone_info = PARSER.get_info(datetime_string)[1]
time_zone_hour = int(time_zone_info["time_zone_hour"])
time_zone_minute = int(time_zone_info.get("time_zone_minute", 0))
offset_seconds = 3600 * time_zone_hour + 60 * time_zone_minute
if "+" in datetime_string:
datetime_string, time_zone_string = datetime_string.split("+")
else:
datetime_string, time_zone_string = datetime_string.rsplit("-", 1)
date_time = datetime.strptime(
datetime_string, DATE_TIME_FORMAT_EXTENDED)
date_time_utc = date_time - timedelta(seconds=offset_seconds)
return timegm(date_time_utc.timetuple())