In [1]:
import os
In [48]:
os.chdir("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)))
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]:
In [45]:
datetime.datetime.fromtimestamp(get_fixed_ts(to_ts(datetime.datetime(2015,11,21))))
Out[45]:
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]))
In [ ]: