def _search_log_for_datetime(log_path, dt, word_count, reverse=False):
ONE_KILOBYTE = <NUM_LIT><EOL>with open(log_path, '<STR_LIT:r>') as log:<EOL><INDENT>lbound = <NUM_LIT:0><EOL>ubound = os.path.getsize(log_path)<EOL>search_area = <NUM_LIT:0><EOL>while ubound > lbound:<EOL><INDENT>if ubound - lbound < ONE_KILOBYTE:<EOL><INDENT>return _linear_search_log_for_time(log_path, dt, word_count, lbound, ubound, reverse)<EOL><DEDENT>mid = (lbound + ubound) / <NUM_LIT:2><EOL>log.seek(mid)<EOL>log.readline()  <EOL>is_valid_dt = False<EOL>while not is_valid_dt:<EOL><INDENT>if log.tell() == ubound:<EOL><INDENT>return _linear_search_log_for_time(log_path, dt, word_count, lbound, ubound, reverse)<EOL><DEDENT>line_start = log.tell()<EOL>line = log.readline()<EOL>timestr = '<STR_LIT:U+0020>'.join(line.split()[:word_count])<EOL>try:<EOL><INDENT>log_dt = parser.parse(timestr)  <EOL>if log_dt.time() != dtime(<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>):<EOL><INDENT>is_valid_dt = True<EOL><DEDENT><DEDENT>except:<EOL><INDENT>is_valid_dt = False<EOL><DEDENT><DEDENT>if log_dt > dt:<EOL><INDENT>ubound = log.tell()<EOL><DEDENT>elif log_dt < dt:<EOL><INDENT>lbound = line_start<EOL><DEDENT>else:<EOL><INDENT>if not reverse:<EOL><INDENT>ubound = log.tell()<EOL><DEDENT>else:<EOL><INDENT>lbound = line_start<EOL><DEDENT><DEDENT>if ubound - lbound == search_area:<EOL><INDENT>return _linear_search_log_for_time(log_path, dt, word_count, lbound, ubound, reverse)<EOL><DEDENT>search_area = ubound - lbound<EOL><DEDENT><DEDENT>
Find the start position of the line containing the datetime nearest to dt.
:param log_path: the absolute path to the log file
:param dt: the datetime to be searched
:param word_count: the number of "words" that represents the datetime in the log
:param reverse: if False, finds the line with the largest datetime less than dt
                if True, finds the line with the smallest datetime larger than dt