Skip to content

Instantly share code, notes, and snippets.

@pridkett
Created March 20, 2017 01:58
Show Gist options
  • Save pridkett/c54d2660fa51d42a83888180524cca92 to your computer and use it in GitHub Desktop.
Save pridkett/c54d2660fa51d42a83888180524cca92 to your computer and use it in GitHub Desktop.
A handy little script to shift the date on a collection of files by a set amount
"""
date_shift.py - timezone sensitive file date modifications
Patrick Wagstrom <patrick@wagstrom.net>
March 19, 2017
this was necessitated after a recent dive trip where I forgot to set the time
on my gopro camera before the first dive. Thus, the timestamp of over video was
off by roughly some fixed amount. This script allows me to calculate the delta
required to shift all the files by the same fixed amount. Once the dates are
all shifted, I can easily reconcile the times against my dive log and figure out
what dive site we were at when each video was recorded.
Yes, I realize I could have done this without all the datetime and timezone
conversion, but it's a short script and I opted for clarity vs efficiency here.
"""
import os
import datetime
import pytz
FILE_DIR = './'
LOCAL_TZ = pytz.timezone('America/New_York')
base_delta = (datetime.datetime(2017,3,16,9,19,30, 0, LOCAL_TZ) -
pytz.UTC.localize(datetime.datetime.fromtimestamp(1325824722.0)))
files = [x for x in os.listdir(FILE_DIR) if os.path.splitext(x)[1].lower() == '.mp4']
for fn in files:
filename = os.path.join(FILE_DIR, fn)
statinfo = os.stat(filename)
file_mtime_datetime = pytz.UTC.localize(datetime.datetime.fromtimestamp(statinfo.st_mtime))
new_mtime_datetime = file_mtime_datetime + base_delta
os.utime(filename, (new_mtime_datetime.timestamp(), new_mtime_datetime.timestamp()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment