cran: Add support for parsing date with milliseconds
This commit is contained in:
parent
2461c97bbb
commit
b4c4c20bb9
2 changed files with 34 additions and 14 deletions
|
@ -108,22 +108,28 @@ def parse_packaged_date(package_info: Dict[str, str]) -> Optional[datetime]:
|
|||
packaged_at_str = package_info.get("Packaged", "")
|
||||
packaged_at = None
|
||||
if packaged_at_str:
|
||||
try:
|
||||
# Packaged field format: "%Y-%m-%d %H:%M:%S UTC; <packager>",
|
||||
packaged_at = datetime.strptime(
|
||||
packaged_at_str.split(" UTC;")[0], "%Y-%m-%d %H:%M:%S",
|
||||
).replace(tzinfo=timezone.utc)
|
||||
except Exception:
|
||||
packaged_at_str = packaged_at_str.replace(" UTC", "")
|
||||
# Packaged field possible formats:
|
||||
# - "%Y-%m-%d %H:%M:%S[.%f] UTC; <packager>",
|
||||
# - "%a %b %d %H:%M:%S %Y; <packager>"
|
||||
for date_format in (
|
||||
"%Y-%m-%d %H:%M:%S",
|
||||
"%Y-%m-%d %H:%M:%S.%f",
|
||||
"%a %b %d %H:%M:%S %Y",
|
||||
):
|
||||
try:
|
||||
# Some old packages have a different date format:
|
||||
# "%a %b %d %H:%M:%S %Y; <packager>"
|
||||
packaged_at = datetime.strptime(
|
||||
packaged_at_str.split(";")[0], "%a %b %d %H:%M:%S %Y",
|
||||
packaged_at_str.split(";")[0], date_format,
|
||||
).replace(tzinfo=timezone.utc)
|
||||
break
|
||||
except Exception:
|
||||
logger.debug(
|
||||
"Could not parse %s package release date: %s",
|
||||
package_info["Package"],
|
||||
packaged_at_str,
|
||||
)
|
||||
continue
|
||||
|
||||
if packaged_at is None:
|
||||
logger.debug(
|
||||
"Could not parse %s package release date: %s",
|
||||
package_info["Package"],
|
||||
packaged_at_str,
|
||||
)
|
||||
|
||||
return packaged_at
|
||||
|
|
|
@ -40,6 +40,20 @@ def test_parse_packaged_date():
|
|||
assert parse_packaged_date(common_date_format) == datetime(
|
||||
year=2017, month=4, day=26, hour=11, minute=36, second=15, tzinfo=timezone.utc
|
||||
)
|
||||
common_date_format = {
|
||||
"Package": "test",
|
||||
"Packaged": "2017-04-26 11:36:15.123456 UTC; Jonathan",
|
||||
}
|
||||
assert parse_packaged_date(common_date_format) == datetime(
|
||||
year=2017,
|
||||
month=4,
|
||||
day=26,
|
||||
hour=11,
|
||||
minute=36,
|
||||
second=15,
|
||||
microsecond=123456,
|
||||
tzinfo=timezone.utc,
|
||||
)
|
||||
old_date_format = {
|
||||
"Package": "test",
|
||||
"Packaged": "Thu Mar 30 10:48:35 2006; hornik",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue