Code Comprehension Exercise
Read the following code carefully before answering the questions.
unsigned long normalise(unsigned long input_time)
{
bool finished;
// This produces a formatted time string like:
// Thu_Nov_24_18:22:48_1986
string str_time = format_time( input_time );
while( str_time.substr(1,3) != "Sun")
{
input_time -= 24*60*60;
str_time = format_time( input_time );
}
while( str_time.substr(11,2) != "00" )
{
input_time -= 60*60;
str_time = format_time( input_time );
}
while( str_time.substr(14,2) != "00")
{
str_time = format_time( input_time );
input_time -= 60;
}
while( str_time.substr(17,2) != "00")
{
input_time -= 1;
str_time = format_time( input_time );
}
return input_time;
}
1) Briefly explain what you think the overall purpose of this function is (do not describe each line of code).
2) There are two bugs in this code that prevent it from functioning correctly; circle them.
3) What would actually happen when this code is run?
(a) as originally written?
(b) with the first bug fixed?
4) Discuss the efficiency of the code assuming the bugs have been fixed.
5) Given that input_time actually represents an integer value in seconds since the epoch – and has the value 0 at precisely "Thu Jan 1 00:00:00 1970", write