In [1]:
import os

In [48]:
os.chdir("power_drain/data")


---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-48-448b637d6a42> in <module>()
----> 1 os.chdir("power_drain/data")

OSError: [Errno 2] No such file or directory: 'power_drain/data'

In [4]:
import fnmatch

In [23]:
import datetime
import time

In [24]:
def to_ts(dt):
    return time.mktime(dt.timetuple())

In [28]:
print to_ts(datetime.datetime(2015,10,21,10,11,12))
print datetime.datetime.fromtimestamp(to_ts(datetime.datetime(2015, 10, 21,10,11,12)))


1445447472.0
2015-10-21 10:11:12

In [39]:
def get_fixed_ts(ts):
    dt = datetime.datetime.fromtimestamp(ts)
    if dt.month == 11:
        return to_ts(dt + datetime.timedelta(days=-31))
    else:
        return to_ts(dt)

In [43]:
datetime.datetime.fromtimestamp(get_fixed_ts(to_ts(datetime.datetime(2015,10,21))))


Out[43]:
datetime.datetime(2015, 10, 21, 0, 0)

In [45]:
datetime.datetime.fromtimestamp(get_fixed_ts(to_ts(datetime.datetime(2015,11,21))))


Out[45]:
datetime.datetime(2015, 10, 21, 0, 0)

In [54]:
def rename_if_needed(fn, start_ts, end_ts):
    start_fixed_ts = get_fixed_ts(start_ts)
    end_fixed_ts = get_fixed_ts(end_ts)
    
    fixed_fn = fn
    if start_fixed_ts != start_ts:
        fixed_fn = fixed_fn.replace(str(start_ts), str(start_fixed_ts))
    if end_fixed_ts != end_ts:
        fixed_fn = fixed_fn.replace(str(end_ts), str(end_fixed_ts))
    
    if fn != fixed_fn:
        print("renaming %s -> %s" % (fn, fixed_fn))
        os.rename(fn, fixed_fn)

In [55]:
for path in os.listdir("."):
    # print path
    filename = path
    # print filename
    parts = filename.split('_')
    # print parts
    if fnmatch.fnmatch(filename, "geofence_100m_*"):
        print parts[7], parts[9]
        rename_if_needed(filename, long(parts[7]), long(parts[9]))
    if fnmatch.fnmatch(filename, "no_data_collection*"):
        print parts[3], parts[5]
        rename_if_needed(filename, long(parts[3]), long(parts[5]))
    if fnmatch.fnmatch(filename, "mobility_plus_fused*"):
        print parts[6], parts[8]
        rename_if_needed(filename, long(parts[6]), long(parts[8]))
    if fnmatch.fnmatch(filename, "ongoing_plus*"):
        print parts[6], parts[8]
        rename_if_needed(filename, long(parts[6]), long(parts[8]))


1448128800 1448150400
renaming geofence_100m_plus_fused_balanced_power_1min_1448128800_to_1448150400 -> geofence_100m_plus_fused_balanced_power_1min_1445446800.0_to_1445468400.0
1448128800 1448150400
renaming geofence_100m_plus_fused_balanced_power_1min_1448128800_to_1448150400_checkin -> geofence_100m_plus_fused_balanced_power_1min_1445446800.0_to_1445468400.0_checkin
1448106000 1448127600
renaming geofence_100m_plus_fused_balanced_power_2sec_1448106000_to_1448127600 -> geofence_100m_plus_fused_balanced_power_2sec_1445424000.0_to_1445445600.0
1448106000 1448127600
renaming geofence_100m_plus_fused_balanced_power_2sec_1448106000_to_1448127600_checkin -> geofence_100m_plus_fused_balanced_power_2sec_1445424000.0_to_1445445600.0_checkin
1445896243 1445918025
1445896243 1445918025
1445957053 1445979189
1445957053 1445979189
1448031600 1448053200
renaming geofence_100m_plus_fused_balanced_power_30sec_1448031600_to_1448053200 -> geofence_100m_plus_fused_balanced_power_30sec_1445349600.0_to_1445371200.0
1448031600 1448053200
renaming geofence_100m_plus_fused_balanced_power_30sec_1448031600_to_1448053200_checkin -> geofence_100m_plus_fused_balanced_power_30sec_1445349600.0_to_1445371200.0_checkin
1448556300 1445896011
renaming geofence_100m_plus_fused_balanced_power_30sec_1448556300_to_1445896011 -> geofence_100m_plus_fused_balanced_power_30sec_1445874300.0_to_1445896011
1448556300 1445896011
renaming geofence_100m_plus_fused_balanced_power_30sec_1448556300_to_1445896011_checkin -> geofence_100m_plus_fused_balanced_power_30sec_1445874300.0_to_1445896011_checkin
1448222400 1448245800
renaming geofence_100m_plus_fused_high_accuracy_30sec_1448222400_to_1448245800 -> geofence_100m_plus_fused_high_accuracy_30sec_1445540400.0_to_1445563800.0
1448222400 1448245800
renaming geofence_100m_plus_fused_high_accuracy_30sec_1448222400_to_1448245800_checkin -> geofence_100m_plus_fused_high_accuracy_30sec_1445540400.0_to_1445563800.0_checkin
1448195400 1448215200
renaming mobility_plus_fused_balanced_power_1min_1448195400_to_1448215200 -> mobility_plus_fused_balanced_power_1min_1445513400.0_to_1445533200.0
1448195400 1448215200
renaming mobility_plus_fused_balanced_power_1min_1448195400_to_1448215200_checkin -> mobility_plus_fused_balanced_power_1min_1445513400.0_to_1445533200.0_checkin
1445842956 1445865080
1445842956 1445865080
1445930191 1445953692
1445930191 1445953692
1446023140 1446044728
1446023140 1446044728
1446105740 1446128077
1446105740 1446128077
1446150661 1446174574
1446150661 1446174574
1446186810 1446212637
1446186810 1446212637
1448173800 1448195400
renaming no_data_collection_1448173800_to_1448195400 -> no_data_collection_1445491800.0_to_1445513400.0
1448173800 1448195400
renaming no_data_collection_1448173800_to_1448195400_checkin -> no_data_collection_1445491800.0_to_1445513400.0_checkin
1448265600 1448287200
renaming no_data_collection_1448265600_to_1448287200 -> no_data_collection_1445583600.0_to_1445605200.0
1448265600 1448287200
renaming no_data_collection_1448265600_to_1448287200_checkin -> no_data_collection_1445583600.0_to_1445605200.0_checkin
1448290800 1448312400
renaming no_data_collection_1448290800_to_1448312400 -> no_data_collection_1445608800.0_to_1445630400.0
1448290800 1448312400
renaming no_data_collection_1448290800_to_1448312400_checkin -> no_data_collection_1445608800.0_to_1445630400.0_checkin
1448352000 1448373600
renaming no_data_collection_1448352000_to_1448373600 -> no_data_collection_1445670000.0_to_1445691600.0
1448352000 1448373600
renaming no_data_collection_1448352000_to_1448373600_checkin -> no_data_collection_1445670000.0_to_1445691600.0_checkin
1448150400 1448172000
renaming ongoing_plus_fused_balanced_power_1min_1448150400_to_1448172000 -> ongoing_plus_fused_balanced_power_1min_1445468400.0_to_1445490000.0
1448150400 1448172000
renaming ongoing_plus_fused_balanced_power_1min_1448150400_to_1448172000_checkin -> ongoing_plus_fused_balanced_power_1min_1445468400.0_to_1445490000.0_checkin
1448083800 1448105400
renaming ongoing_plus_fused_balanced_power_2sec_1448083800_to_1448105400 -> ongoing_plus_fused_balanced_power_2sec_1445401800.0_to_1445423400.0
1448083800 1448105400
renaming ongoing_plus_fused_balanced_power_2sec_1448083800_to_1448105400_checkin -> ongoing_plus_fused_balanced_power_2sec_1445401800.0_to_1445423400.0_checkin
1445980311 1446002095
1445980311 1446002095
1446002095 1446023140
1446002095 1446023140
1446045087 1446068393
1446045087 1446068393
1446068421 1446105101
1446068421 1446105101
1446129513 1446150502
1446129513 1446150502
1448055000 1448076600
renaming ongoing_plus_fused_balanced_power_30sec_1448055000_to_1448076600 -> ongoing_plus_fused_balanced_power_30sec_1445373000.0_to_1445394600.0
1448055000 1448076600
renaming ongoing_plus_fused_balanced_power_30sec_1448055000_to_1448076600_checkin -> ongoing_plus_fused_balanced_power_30sec_1445373000.0_to_1445394600.0_checkin
1448245800 1448265600
renaming ongoing_plus_fused_high_accuracy_30sec_1448245800_to_1448265600 -> ongoing_plus_fused_high_accuracy_30sec_1445563800.0_to_1445583600.0
1448245800 1448265600
renaming ongoing_plus_fused_high_accuracy_30sec_1448245800_to_1448265600_checkin -> ongoing_plus_fused_high_accuracy_30sec_1445563800.0_to_1445583600.0_checkin

In [ ]: