Skip to content

Commit

Permalink
Move Item.__get_formatted_date to webqueue2api.parser.utils and renam…
Browse files Browse the repository at this point in the history
…e to format_date_string
  • Loading branch information
benne238 committed Jun 28, 2021
1 parent 425b41a commit dc11658
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/webqueue2api/parser/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Shared utilities for the parser package"""

from dateutil import parser, tz
from datetime import datetime

def format_date_string(date: str) -> str:
"""Returns the date/time formatted as RFC 8601 YYYY-MM-DDTHH:MM:SS+00:00.
Returns empty string if the string argument passed to the function is not a datetime.
See: https://en.wikipedia.org/wiki/ISO_8601
Returns:
str: Properly formatted date/time recieved or empty string.
"""
try:
# This date is never meant to be used. The default attribute is just to set timezone.
parsed_date = parser.parse(date, default=datetime(
1970, 1, 1, tzinfo=tz.gettz('EDT')))
except:
return ""

parsed_date_string = parsed_date.strftime("%Y-%m-%dT%H:%M:%S%z")

return parsed_date_string

if __name__ == "__main__":
print(format_date_string("Tue, 23 Jun 2020 13:30:45 -0400"))

0 comments on commit dc11658

Please sign in to comment.