Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Haing this error : The call str _ to _ time ( ' 2 0 1 6 - 0 5 - 1 2 T 1

Haing this error :
The call str_to_time('2016-05-12T16:23-4:00', datetime.datetime(2016,5,12,16,23, tzinfo=)) returns datetime.datetime(2016,5,12,16,23, tzinfo=), not datetime.datetime(2016,5,12,16,23, tzinfo=).
On this python def
def str_to_time(timestamp,tzsource=None):
"""
Returns the datetime object for the given timestamp (or None if timestamp 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 time zone, then it should keep that time zone even if
the value for tzsource is not None. Otherwise, if timestamp has no time zone
and tzsource is not None, then this function will use tzsource to assign
a time zone to the new datetime object.
The value for tzsource can be None, a string, or a datetime object. If it
is a string, it will be the name of a time zone, and it should localize the
timestamp. If it is another datetime, then the datetime object created from
timestamp should get the same time zone as tzsource.
Parameter timestamp: The time stamp to convert
Precondition: timestamp is a string
Parameter tzsource: The time zone to use (OPTIONAL)
Precondition: tzsource is either None, a string naming a valid time zone,
or a datetime object.
"""
# HINT: Use the code from the previous exercise and add time zone handling.
# Use localize if tzsource is a string; otherwise replace the time zone if not None
try:
dt = parser.parse(timestamp)
except ValueError:
return None
if tzsource is None:
return dt
if hasattr(tzsource, 'tzinfo') and tzsource.tzinfo is not None:
return dt.replace(tzinfo=tzsource.tzinfo)
elif isinstance(tzsource, str):
time_zone = pytz.timezone(tzsource)
return time_zone.localize(dt)
else:
return dt

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analytics And Quality Management Fundamental Tools

Authors: Joseph Nguyen

1st Edition

B0CNGG3Y2W, 979-8862833232

More Books

Students also viewed these Databases questions