Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions awslogs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,16 @@ def parse_datetime(self, datetime_text):
if not datetime_text:
return None

ago_regexp = r'(\d+)\s?(m|minute|minutes|h|hour|hours|d|day|days|w|weeks|weeks)(?: ago)?'
ago_regexp = r'([+-]?([0-9]*[.])?[0-9]+)\s?(s|sec|secs|second|seconds|m|minute|minutes|h|hour|hours|d|day|days|w|weeks|weeks)(?: ago)?'

@Code0x58 Code0x58 Nov 23, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I agree that the +/- is redundant.

The digit pattern may be a bit more efficient (not a practical concern) as ([0-9]+(?:\.[0-9]+)?) - it would also avoid the additional matching group, so you just have (amount, unit) again.

To be consistent the additional time spans would be s|second|seconds, as there's nothing like hr|hrs.

The line is getting long (enough to cause the linter to kick off in Travis), a change to s|seconds?|m|minutes?|… seems reasonable.

p.s. it looks like there was a bug previously, was the pattern ends with weeks|weeks instead of week|weeks. It would also be good to add $ to the end of the pattern.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I agree the +/- is a bit too much.
  • I do agree with @Code0x58 about the regexp and s|second|seconds

ago_match = re.match(ago_regexp, datetime_text)

if ago_match:
amount, unit = ago_match.groups()
amount = int(amount)
unit = {'m': 60, 'h': 3600, 'd': 86400, 'w': 604800}[unit[0]]
amount, whole_part, unit = ago_match.groups()
try:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This try/except block could be replaced with just amount = float(amount) as the precision is good enough for this application regardless.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree

amount = int(amount)
except:
amount = float(amount)
unit = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400, 'w': 604800}[unit[0]]
date = datetime.utcnow() + timedelta(seconds=unit * amount * -1)
else:
try:
Expand Down