Ejemplo n.º 1
0
    def test_round_time_to_nearest_quarter_hour(self):

        time = pytz.utc.localize(datetime.datetime(2014, 1, 1, 0, 0, 0))
        assert round_time(time) == pytz.utc.localize(datetime.datetime(2014, 1, 1, 0, 0, 0))

        time = pytz.utc.localize(datetime.datetime(2014, 1, 1, 0, 6, 0))
        assert round_time(time) == pytz.utc.localize(datetime.datetime(2014, 1, 1, 0, 0, 0))

        time = pytz.utc.localize(datetime.datetime(2014, 1, 1, 0, 7, 0))
        assert round_time(time) == pytz.utc.localize(datetime.datetime(2014, 1, 1, 0, 0, 0))

        time = pytz.utc.localize(datetime.datetime(2014, 1, 1, 0, 8, 0))
        assert round_time(time) == pytz.utc.localize(datetime.datetime(2014, 1, 1, 0, 15, 0))

        time = pytz.utc.localize(datetime.datetime(2014, 1, 1, 0, 59, 0))
        assert round_time(time) == pytz.utc.localize(datetime.datetime(2014, 1, 1, 1, 0, 0))

        time = pytz.utc.localize(datetime.datetime(2014, 1, 31, 23, 59, 0))
        assert round_time(time) == pytz.utc.localize(datetime.datetime(2014, 2, 1, 0, 0, 0))

        time = pytz.utc.localize(datetime.datetime(2014, 12, 31, 23, 59, 0))
        assert round_time(time) == pytz.utc.localize(datetime.datetime(2015, 1, 1, 0, 0, 0))

file_names = os.listdir(fieldserver_folder)

for i, file_name in enumerate(file_names):
    try:
        print("Processing file " + str(i + 1) + "/" + str(len(file_names)))
        file_path = os.path.join(fieldserver_folder, file_name)

        file_name_date = file_name.split("_")[0].split("-")[1:4]
        file_name_time = file_name.split("_")[1:4]

        timestamp = pytz.utc.localize(datetime.datetime(int(file_name_date[0]), int(file_name_date[1]),
                                                        int(file_name_date[2]), int(file_name_time[0]),
                                                        int(file_name_time[1]), int(file_name_time[2])))
        timestamp = round_time(timestamp)

        file_name_date_str = "-".join(file_name_date)
        write_file_path = os.path.join(fieldserver_folder, file_name_date_str)

        with open(write_file_path, "a") as write_file:
            with open(file_path, "r") as f:
                data = json.loads(f.read())

                for (key, values) in data.iteritems():
                    key_split = key.split("_")
                    if key_split[1] == "Offsets":
                        site_id = key_split[0]
                        values_array = [float(r) for r in values.split(",")]
                        starting_offset = int(key_split[2].split("-")[0])