Question: I need to update the code bolded to do what the specification says but I'm unsure of what needs to be done. from dateutil import
I need to update the code bolded to do what the specification says but I'm unsure of what needs to be done.
from dateutil import parser
def str_to_time(timestamp,tz=None): """ Returns the datetime object for the given timestamp (or None if stamp is invalid) This function should just use the parse function in dateutil.parser to convert the timestamp to a datetime object. If it is not a valid date (so the parser crashes), this function should return None. If the timestamp has a timezone, then it should keep that timezone even if the value for tz is not None. Otherwise, if timestamp has no timezone and tz if not None, this this function will assign that timezone to the datetime object. The value for tz can either be a string or a time OFFSET. If it is a string, it will be the name of a timezone, and it should localize the timestamp. If it is an offset, that offset should be assigned to the datetime object. Parameter timestamp: The time stamp to convert Precondition: timestamp is a string Parameter tz: The timezone to use (OPTIONAL) Precondition: tz is either None, a string naming a valid time zone, or a time zone OFFSET. """ # HINT: Use the code from the previous exercise and update the timezone # Use localize if timezone is a string; otherwise replace the timezone if not None try: timestamp = parser.parse(timestamp) return timestamp except: return None
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
