Question: I need help implementing def str_to_time(timestamp, tzsource = none) Returns the datetime object for the given timestamp (or None if timestamp is invalid). This function

I need help implementing 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.

THIS IS MY CODE FROM THE PREVIOUS EXAMPLE

I need help implementing def str_to_time(timestamp, tzsource = none) Returns the datetime

object for the given timestamp (or None if timestamp is invalid). ThisSo basically, I need my other code to do this problem and I am having issues implementing it.

try: timestamp = parser.parse(timestamp) except: return None if timestamp : = None: return timestamp else: \#timezone = pytz.timezone (timestamp) try: timestamp = parser.parse(timestamp) return timestamp except: return None

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!