from datetime import datetime def length_of_storage_time_in_seconds(now): """ calculate how long, in seconds, is left before 20mins past the current, or next hour :return: the number of seconds before 20mins past the current, or next, hour """ minute = now.minute if 0 < minute <= 20: return (20 - minute) * 60 mins_to_hour = 60 - minute return (mins_to_hour + 20) * 60