rewrite time_window() function