Skip to content

Instantly share code, notes, and snippets.

@hyrsky
Created March 13, 2016 13:03
Show Gist options
  • Save hyrsky/c365ae4ac40034fd15d7 to your computer and use it in GitHub Desktop.
Save hyrsky/c365ae4ac40034fd15d7 to your computer and use it in GitHub Desktop.
Fix timestamps
#!/usr/bin/env python
import os.path, time
import argparse
from dateutil import parser
import datetime
argparser = argparse.ArgumentParser(description='fix timestamps for files. oldest timestamp will be set to epoch and other timestamps are calculated based on that')
argparser.add_argument('file', nargs='+', help='list of file to touch')
argparser.add_argument('--epoch', default='2000-01-01', metavar='epoch', help='set custom epoch')
args = argparser.parse_args()
epoch = int(time.mktime(parser.parse(args.epoch).timetuple()))
files = []
for file in args.file:
files.append((file, os.path.getmtime(file)))
files = sorted(files, key=lambda x: x[1])
for file in files:
difference = file[1] - files[0][1]
print(datetime.datetime.fromtimestamp(int(epoch + difference)).strftime('%Y-%m-%d %H:%M:%S') + "\t " + file[0])
os.utime(file[0], (epoch + difference, epoch + difference))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment