本文整理汇总了Python中pendulum.from_format方法的典型用法代码示例。如果您正苦于以下问题:Python pendulum.from_format方法的具体用法?Python pendulum.from_format怎么用?Python pendulum.from_format使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pendulum
的用法示例。
在下文中一共展示了pendulum.from_format方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _parseDateInput
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import from_format [as 别名]
def _parseDateInput(cls, date):
"""Verify that the given string is a valid date formatted as
YYYY-MM-DD. Also, the API seems to go back until 2014-10-04,
so we will check that the input is not a date earlier than that.
In case of failure, throws a ValueError exception.
"""
date = date.lower()
if date in cls._FUZZY_DAYS:
date = cls._EnglishDateToDate(date)
elif date.replace("-", "").isdigit():
try:
parsed_date = pendulum.from_format(date, "YYYY-MM-DD")
except:
raise ValueError("Incorrect date format, should be YYYY-MM-DD")
# The current API goes back until 2014-10-04. Is it in range?
if parsed_date < pendulum.datetime(2014, 10, 4):
raise ValueError("I can only go back until 2014-10-04")
else:
raise ValueError("Date is not valid")
return cls._stripDateSeparators(date)
示例2: test_from_format
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import from_format [as 别名]
def test_from_format(text, fmt, expected, now):
if now is None:
now = pendulum.datetime(2015, 11, 12)
else:
now = pendulum.parse(now)
# Python 2.7 loses precision for x timestamps
# so we don't test
if fmt == "x" and PY2:
return
with pendulum.test(now):
assert pendulum.from_format(text, fmt).isoformat() == expected
示例3: main
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import from_format [as 别名]
def main(command, datetime=None):
ctx = Context()
if datetime is None:
datetime = pendulum.now(ctx.config["TIMEZONE"])
else:
try:
datetime = pendulum.from_format(datetime, 'YYYY-MM-DD', tz=ctx.config["TIMEZONE"])
ctx.logger(f"Running in date override mode with date {datetime.format('YYYY-MM-DD')}.")
ctx.logger("This is an experimental feature and may generate incorrect data.")
except:
ctx.logger("Failed to parse custom datetime argument")
return 1
try:
outcome = ACTIONS[command](ctx, datetime)
except Exception as exc:
ctx.logger(exc)
traceback.print_tb(exc.__traceback__)
ctx.logger(f"{command} failed")
return 1
if outcome is 0:
ctx.logger(f"{command} completed successfully")
return 0
else:
ctx.logger(f"{command} failed")
return 1
示例4: extract_datetime
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import from_format [as 别名]
def extract_datetime(path_to_dt, full_path, tz="UTC"):
string_to_match = f"(?:{path_to_dt}/)([0-9]+-[0-9]+-[0-9]+)/([0-9]+:[0-9]+:[0-9]+)"
dateinfo = re.match(string_to_match, full_path).groups()
return pendulum.from_format(
dateinfo[0] + "T" + dateinfo[1], "YYYY-MM-DDTHH:mm:ss", tz=tz
)
示例5: extract_date
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import from_format [as 别名]
def extract_date(path_to_dt, full_path, tz="UTC"):
string_to_match = f"(?:{path_to_dt}/)([0-9]+-[0-9]+-[0-9]+)"
dateinfo = re.match(string_to_match, full_path).groups()
return pendulum.from_format(dateinfo[0], "YYYY-MM-DD", tz=tz)
示例6: extract_time
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import from_format [as 别名]
def extract_time(path_to_dt, full_path, tz="UTC"):
string_to_match = f"(?:{path_to_dt}/)([0-9]+:[0-9]+:[0-9]+)"
dateinfo = re.match(string_to_match, full_path).groups()
return pendulum.from_format(dateinfo[0], "HH:mm:ss", tz=tz)
示例7: services_running_on
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import from_format [as 别名]
def services_running_on(self, datestring):
within_range = self.on_date(datestring)
date = pendulum.from_format(datestring, "YYYY-MM-DD")
day = date.format("dddd").lower()
services = within_range[within_range[day] == 1]
if len(services.index) is 0:
# This can apply on public holidays
# The day flag can be incorrect so we
# instead just look for services with a
# start and end time exactly on the date
services = self.exactly_on_date(datestring)
return services
示例8: test_from_format_returns_datetime
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import from_format [as 别名]
def test_from_format_returns_datetime():
d = pendulum.from_format("1975-05-21 22:32:11", "YYYY-MM-DD HH:mm:ss")
assert_datetime(d, 1975, 5, 21, 22, 32, 11)
assert isinstance(d, pendulum.DateTime)
assert "UTC" == d.timezone_name
示例9: test_from_format_rejects_extra_text
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import from_format [as 别名]
def test_from_format_rejects_extra_text():
with pytest.raises(ValueError):
pendulum.from_format("1975-05-21 22:32:11 extra text", "YYYY-MM-DD HH:mm:ss")
示例10: test_from_format_with_timezone_string
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import from_format [as 别名]
def test_from_format_with_timezone_string():
d = pendulum.from_format(
"1975-05-21 22:32:11", "YYYY-MM-DD HH:mm:ss", tz="Europe/London"
)
assert_datetime(d, 1975, 5, 21, 22, 32, 11)
assert "Europe/London" == d.timezone_name
示例11: test_from_format_with_timezone
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import from_format [as 别名]
def test_from_format_with_timezone():
d = pendulum.from_format(
"1975-05-21 22:32:11",
"YYYY-MM-DD HH:mm:ss",
tz=pendulum.timezone("Europe/London"),
)
assert_datetime(d, 1975, 5, 21, 22, 32, 11)
assert "Europe/London" == d.timezone_name
示例12: test_from_format_with_square_bracket_in_timezone
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import from_format [as 别名]
def test_from_format_with_square_bracket_in_timezone():
with pytest.raises(ValueError, match="^String does not match format"):
pendulum.from_format(
"1975-05-21 22:32:11 Eu[rope/London", "YYYY-MM-DD HH:mm:ss z",
)
示例13: test_from_format_with_escaped_elements_valid_tokens
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import from_format [as 别名]
def test_from_format_with_escaped_elements_valid_tokens():
d = pendulum.from_format("1975-05-21T22:32:11.123Z", "YYYY-MM-DD[T]HH:mm:ss.SSS[Z]")
assert_datetime(d, 1975, 5, 21, 22, 32, 11)
assert "UTC" == d.timezone_name
示例14: test_from_format_with_millis
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import from_format [as 别名]
def test_from_format_with_millis():
d = pendulum.from_format("1975-05-21 22:32:11.123456", "YYYY-MM-DD HH:mm:ss.SSSSSS")
assert_datetime(d, 1975, 5, 21, 22, 32, 11, 123456)
示例15: test_from_format_with_padded_day
# 需要导入模块: import pendulum [as 别名]
# 或者: from pendulum import from_format [as 别名]
def test_from_format_with_padded_day():
d = pendulum.from_format("Apr 2 12:00:00 2020 GMT", "MMM DD HH:mm:ss YYYY z")
assert_datetime(d, 2020, 4, 2, 12)