Question
I keep getting this error when trying to implement the function daytime. Can someone help me, this is Python. Functions for parsing time values
I keep getting this error when trying to implement the function daytime. Can someone help me, this is Python.
"""
Functions for parsing time values and determining daylight hours.
Both of these functions will be used in the main project. You should hold on to them.
"""
import pytz
from dateutil.parser import *
import datetime
from pytz import *
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
pass # Implement this function
try:
result = parse(timestamp)
except ValueError:
return None
else:
result = parse(timestamp)
if result.tzinfo == None:
if type(tzsource) == str:
tz = pytz.timezone(tzsource)
result = tz.localize(result)
return result
elif tzsource != None:
result = result.replace(tzinfo=tzsource.tzinfo)
return result
return result
def daytime(time,daycycle):
"""
Returns True if the time takes place during the day, False otherwise (and
returns None if a key does not exist in the dictionary).
A time is during the day if it is after sunrise but before sunset, as
indicated by the daycycle dictionary.
A daycycle dictionary has keys for several years (as strings). The value for
each year is also a dictionary, taking strings of the form 'mm-dd'. The
value for that key is a THIRD dictionary, with two keys "sunrise" and
"sunset". The value for each of those two keys is a string in 24-hour
time format.
For example, here is what part of a daycycle dictionary might look like:
"2015": {
"01-01": {
"sunrise": "07:35",
"sunset": "16:44"
},
"01-02": {
"sunrise": "07:36",
"sunset": "16:45"
},
...
}
In addition, the daycycle dictionary has a key 'timezone' that expresses the
timezone as a string. This function uses that timezone when constructing
datetime objects using data from the daycycle dictionary. Also, if the time
parameter does not have a timezone, we assume that it is in the same timezone
as the daycycle dictionary.
Parameter time: The time to check
Precondition: time is a datetime object
Parameter daycycle: The daycycle dictionary
Precondition: daycycle is a valid daycycle dictionary, as described above
"""
# HINT: Use the code from the previous exercise to get sunset AND sunrise
# Add a timezone to time if one is missing (the one from the daycycle)
# Implement this function
try:
tz_cycle = daycycle['timezone']
if time.tzinfo is not None:
year1 = time.strftime('%Y')
date1 = time.strftime('%m-%d')
sunrisetime = daycycle[year1][date1]['sunrise']
sunsettime = daycycle[year1][date1]['sunset']
y = year1 + '-' + date1
y1 = datetime.datetime.strptime(y, '%Y-%m-%d')
y3 = datetime.datetime.strptime(sunrisetime, '%H:%M').time()
y2 = datetime.datetime.combine(y1, y3)
x = y2.isoformat()
y4 = datetime.datetime.strptime(sunsettime, '%H:%M').time()
y5 = datetime.datetime.combine(y1, y4)
x1 = y5.isoformat()
back_sunrise = str_to_time(x, tz_cycle)
back_sunset = str_to_time(x1, tz_cycle)
if back_sunrise
return True
else:
return False
except KeyError:
return None
18. Implement daytime Read the specification of the function in Implement this function according to its specification. This function is very similar to from the previous exercise, except that it now asks you to determine whether a object is between sunrise and sunset (e.g. happened during the day). That means you will need to extract both the sunrise and sunset this time and compare them to given value. Note that the value may or may not have a time zone attached to it. If it has a time zone attached, you must give both sunrise and sunset a time zone in order to be able to compare them. The time zone is stored in the daycycle dictionary using the key 'timezone' (that value is a string). If does not have a time zone, you can assume it has the same time zone as sunrise and sunset. When you have implemented the function, test your answer with the test script. You should now pass all tests. Check the Function You may run this test multiple times. LAST RUN on 1/2/2023, 12:57:54 PM The call daytime(datetime. datetime(2015, 6, 5, 7, 0), daycycle) returns None, not True
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started