support more date formats in date

datetime.strptime doesn't support time zone like IST, and need specify
time format. Use dateutil.parser is easier.
This commit is contained in:
Chi Song 2020-12-09 19:06:34 +08:00
Родитель 562b48a240
Коммит 4fb6a8b88f
3 изменённых файлов: 10 добавлений и 12 удалений

Просмотреть файл

@ -1,5 +1,7 @@
from datetime import datetime
from dateutil.parser import parser
from lisa.executable import Tool
from lisa.util import LisaException
@ -18,6 +20,4 @@ class Date(Tool):
raise LisaException(
f"'Date' return non-zero exit code: {command_result.stderr}"
)
# Mon Nov 23 00:21:02 UTC 2020
result = datetime.strptime(command_result.stdout, "%a %b %d %H:%M:%S %Z %Y")
return result
return parser().parse(command_result.stdout)

Просмотреть файл

@ -34,14 +34,16 @@ class Reboot(Tool):
# The reboot will wait forever.
# in this case, verify the time is wait enough to prevent this problem.
date = self.node.tools[Date]
current_delta = date.current() - current_boot_time
# boot time has no tzinfo, so remove from date result to avoid below error.
# TypeError: can't subtract offset-naive and offset-aware datetimes
current_delta = date.current().replace(tzinfo=None) - current_boot_time
self._log.debug(f"delta time since last boot: {current_delta}")
while current_delta < timedelta(minutes=1):
# wait until one minute
wait_seconds = 60 - current_delta.seconds + 1
self._log.debug(f"waiting {wait_seconds} seconds before rebooting")
sleep(wait_seconds)
current_delta = date.current() - current_boot_time
current_delta = date.current().replace(tzinfo=None) - current_boot_time
self._log.debug(f"rebooting with boot time: {last_boot_time}")
try:

Просмотреть файл

@ -1,6 +1,8 @@
import re
from datetime import datetime
from dateutil.parser import parser
from lisa.executable import Tool
from lisa.util import LisaException, get_matched_str
@ -23,11 +25,5 @@ class Who(Tool):
f"'last' return non-zero exit code: {command_result.stderr}"
)
datetime_output = get_matched_str(command_result.stdout, self.last_time_pattern)
try:
result = datetime.fromisoformat(datetime_output)
except ValueError:
# ValueError: Invalid isoformat string: 'Nov 10 20:54'
datetime_with_year = f"{datetime_output} {datetime.utcnow().year}"
result = datetime.strptime(datetime_with_year, "%b %d %H:%M %Y")
return result
return parser().parse(datetime_output)