Question: Having this error: The call daytime ( datetime . datetime ( 2 0 1 5 , 1 0 , 3 1 , 6 , 0

Having this error:
The call daytime(datetime.datetime(2015,10,31,6,0, tzinfo=),daycycle) returns False, not True.
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)
year = time.year
month_day = time.strftime('%m-%d')
day_info = daycycle[str(year)][month_day]
sunrise_str = day_info['sunrise']
sunset_str = day_info['sunset']
if time.tzinfo is None:
tz = pytz.timezone(daycycle['timezone'])
time = tz.localize(time)
else:
tz = time.tzinfo
sunrise = datetime.datetime.combine(time.date(), datetime.datetime.strptime(sunrise_str,'%H:%M').time(), tzinfo=tz)
sunset = datetime.datetime.combine(time.date(), datetime.datetime.strptime(sunset_str,'%H:%M').time(), tzinfo=tz)
return sunrise <= time < sunset
these are the test cases
def test_daytime():
"""
Tests the function utils.daytime
"""
fcn = 'utils.daytime'
parent = os.path.split(__file__)[0]
fpath = os.path.join(parent,'daycycle.json')
cycle = utils.read_json(fpath)
times =[('2015-06-05T07:00:00',True,True),('2015-06-05T17:00:00',True,True),
('2015-10-31T06:00:00',False,True),('2015-10-31T17:00:00',True,False),
('2015-11-17T07:00:00',True,True),('2015-11-17T17:00:00',False,False),
('2015-12-11T07:00:00',False,True),('2015-06-05T17:00:00',True,True),
('2016-11-01T07:00:00',True,True),('2016-11-01T17:00:00',False,False),
('2017-11-17T07:00:00',False,True),('2017-11-17T17:00:00',False,False),
('2018-06-05T07:00:00',True,True),('2018-06-05T17:00:00',True,True),
('2018-11-15T07:00:00',True,True),('2018-11-15T17:00:00',False,False),
('2019-11-15T07:00:00',True,True),('2019-11-15T17:00:00',False,False)]
# CHECK THE TEST CASES
for time in times:
act = utils.str_to_time(time[0],"America/New_York")
day = utils.daytime(act,cycle)
data =(fcn,repr(act),'daycycle',repr(day),repr(time[1]))
assert_equals(time[1], day,'%s(%s,%s) returned %s, but should have returned %s'% data)
act = utils.str_to_time(time[0],"America/Chicago")
day = utils.daytime(act,cycle)
data =(fcn,repr(act),'daycycle',repr(day),repr(time[2]))
assert_equals(time[2], day,'%s(%s,%s) returned %s, but should have returned %s'% data)
# Test the case of no time zone (Same result as first test above)(Edited by STT)
act = utils.str_to_time(time[0])
day = utils.daytime(act,cycle)
data =(fcn,repr(act),'daycycle',repr(day),repr(time[1]))
assert_equals(time[1], day,'%s(%s,%s) returned %s, but should have returned %s'% data)
print('%s passed all tests' % fcn)

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!