def test_date_time_add_sub(): """Test the add and subtract methods for DateTime.""" dt1 = DateTime(6, 21, 12) dt2 = dt1.add_hour(1) dt3 = dt1.sub_hour(1) dt4 = dt1.add_minute(1) dt5 = dt1.sub_minute(1) assert dt2 == DateTime(6, 21, 13) assert dt3 == DateTime(6, 21, 11) assert dt4 == DateTime(6, 21, 12, 1) assert dt5 == DateTime(6, 21, 11, 59)
def _extract_run_period(self, st_time, end_time): """Extract the run period object and frequency from the SQLite file. Args: st_time: Index for the start time of the data. end_time: Index for the end time of the data. Returns: A tuple with run_period, reporting_frequency, and a boolean for whether the data was for a design day. """ conn = sqlite3.connect(self.file_path) try: # extract the start and end times from the Time table c = conn.cursor() c.execute('SELECT * FROM Time WHERE TimeIndex=?', (st_time,)) start = c.fetchone() c.execute('SELECT * FROM Time WHERE TimeIndex=?', (end_time,)) end = c.fetchone() conn.close() # ensure connection is always closed except Exception as e: conn.close() # ensure connection is always closed raise Exception(str(e)) # check whether the data was for a design day dday_period = True if start[10] in ('SummerDesignDay', 'WinterDesignDay') \ else False # set the reporting frequency by the interval type interval_typ = start[8] if interval_typ <= 1: min_per_step = start[7] aper_timestep = int(60 / min_per_step) reporting_frequency = aper_timestep else: reporting_frequency = self._interval_codes[interval_typ] aper_timestep = 1 min_per_step = 60 # convert the extracted data into an AnalysisPeriod object leap_year = True if end[1] % 4 == 0 else False if reporting_frequency == 'Monthly': st_date = DateTime(start[2], 1, 0) else: st_date = DateTime(start[2], start[3], 0) end_date = DateTime(end[2], end[3], 0) end_date = end_date.add_minute(1440 - min_per_step) run_period = AnalysisPeriod( st_date.month, st_date.day, st_date.hour, end_date.month, end_date.day, end_date.hour, aper_timestep, leap_year) return run_period, reporting_frequency, dday_period
def _extract_all_run_period(self, reporting_frequency, timestep, leap_year): """Extract all run period objects the Time table in the SQLite file. Args: reporting_frequency: Text for the reporting frequency of the data. timestep: Integer to note the timestep of the analsis periods. By the time this function is running, this value should have been gotten from the _extract_run_period method end_time: Boolean to note whether the analysis periods are for the whole year. Returns: A list of AnalysisPeriods for all periods that could be obtained from the Time table. """ conn = sqlite3.connect(self.file_path) try: # extract all of the data from the Time table c = conn.cursor() c.execute('SELECT * FROM Time') timeseries = c.fetchall() conn.close() # ensure connection is always closed except Exception as e: conn.close() # ensure connection is always closed raise Exception(str(e)) min_per_step = int(60 / timestep) # extract information about the first run period if reporting_frequency == 'Monthly': st_date = DateTime(timeseries[0][2], 1, 0) else: st_date = DateTime(timeseries[0][2], timeseries[0][3], 0) env_period = timeseries[0][11] # build up the analysis period objects run_periods = [] for i, time_row in enumerate(timeseries): if time_row[11] != env_period: # start of a new run period # create the run period end = timeseries[i - 1] end_date = DateTime(end[2], end[3], 0) end_date = end_date.add_minute(1440 - min_per_step) run_period = AnalysisPeriod( st_date.month, st_date.day, st_date.hour, end_date.month, end_date.day, end_date.hour, timestep, leap_year) run_periods.append(run_period) # reset the tracking variables if reporting_frequency == 'Monthly': st_date = DateTime(time_row[2], 1, 0) else: st_date = DateTime(time_row[2], time_row[3], 0) env_period = time_row[11] # create the last run period object and return all run periods end_date = DateTime(time_row[2], time_row[3], 0) end_date = end_date.add_minute(1440 - min_per_step) run_period = AnalysisPeriod( st_date.month, st_date.day, st_date.hour, end_date.month, end_date.day, end_date.hour, timestep, leap_year) run_periods.append(run_period) return run_periods