Ejemplo n.º 1
0
def timestamps_to_dict(time_lists):

    isList = 0
    if type(time_lists) is list:
        isList = 1

        old_list = time_lists
        time_lists = mh.AutoVivification()

        key = 'key'
        time_lists[key] = list()

        for i in range(len(old_list)):
            time_lists[key].append(old_list[i])

    return [time_lists, isList]
Ejemplo n.º 2
0
def normalize_timestamps(time_lists, count_back, time_unit):
    """ Convert timestamps if they are strings """
    if isinstance(time_lists, list):
        time_lists = timestamp_list_to_obj(time_lists)
    elif isinstance(time_lists, dict):
        time_lists = timestamp_dict_to_obj(time_lists)
    else:
        logging.error(
            'TimestampProcessor::normalize_timestamps -- Timestamps must be contained in a list or dictionary.'
        )
        return dict()

    time_lists, isList = timestamps_to_dict(time_lists)
    """ Depending on args set the start date """
    if count_back:
        start_date_obj = find_latest_date_in_list(time_lists)
    else:
        start_date_obj = find_earliest_date_in_list(time_lists)

    start_month = start_date_obj.month
    start_day = start_date_obj.day
    start_hr = start_date_obj.hour
    start_mte = start_date_obj.minute

    length_of_month = cal.mdays[start_month]

    # Normalize dates
    time_norm = mh.AutoVivification()
    for key in time_lists.keys():
        for date_obj in time_lists[key]:

            month = date_obj.month
            day = date_obj.day
            hr = date_obj.hour
            mte = date_obj.minute

            if time_unit == 0:
                elem = (date_obj - start_date_obj
                        ).days + (date_obj - start_date_obj).seconds / (
                            60 * 60 * 24)  # Time difference in days
            elif time_unit == 1:
                elem = (date_obj - start_date_obj).days * 24 + (
                    date_obj - start_date_obj).seconds / (
                        60 * 60)  # Time difference in hours
            elif time_unit == 2:
                elem = (day - start_day) * 24 * 60 + (hr - start_hr) * 60 + (
                    mte - start_mte)
            elif time_unit == 3:
                elem = (month - start_month) * 24 * 60 * length_of_month + (
                    day - start_day) * 24 * 60 + (hr - start_hr) * 60 + (
                        mte - start_mte)
            try:
                time_norm[key].append(elem)
            except:
                time_norm[key] = list()
                time_norm[key].append(elem)
    """ If the original argument was a list put it back in that form """
    if isList:
        time_norm = time_norm[key]

    return time_norm