Question: I keep running into the error : The call daytime ( datetime . datetime ( 2 0 1 5 , 6 , 5 , 7
I keep running into the error :
The call daytimedatetimedatetimedaycycle returns None, not True.
Implement daytime
Read the specification of the function daytime in the module funcs. Implement this function according to its specification.
This function is very similar to sunset from the previous exercise, except that it now asks you to determine whether a datetime object is between sunrise and sunset eg happened during the day That means you will need to extract both the sunrise and sunset this time and compare them to given time value.
Note that the time 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 timezonethat value is a string If time does not have a time zone, you can assume it has the same time zone as sunrise and sunset.
def daytimetimedaycycle:
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 mmdd 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 hour time format.
For example, here is what part of a daycycle dictionary might look like:
:
:
"sunrise": :
"sunset": :
:
"sunrise": :
"sunset": :
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
try:
tzcycle daycycletimezone
if time.tzinfo is not None:
year time.strftimeY
date time.strftimemd
sunrisetime daycycleyeardatesunrise
sunsettime daycycleyeardatesunset
y year date
y datetime.datetime.strptimeyYmd
y datetime.datetime.strptimesunrisetimeH:Mtime
y datetime.datetime.combiney y
x yisoformat
y datetime.datetime.strptimesunsettimeH:Mtime
y datetime.datetime.combiney y
x yisoformat
backsunrise strtotimex tzcycle
backsunset strtotimex tzcycle
if backsunrise time backsunset:
return True
else:
return False
except KeyError:
return None
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
