| #!//usr/bin/env python
# Converts Nikola-style single-file bundled metadata
# timestamps from whatever time zone they were before and
# replaces them in-file with UTC.
#
# Can easily be modified to work with other meta or file
# types, or perform other time zone shifts.
#
# Possibly destructive. Backup your data before running.
# Provided as-is with no support. This program is licened
# in the pubic domain.
import sys
import datetime
import dateutil.tz
import dateutil.parser
infile = sys.argv[1]
lines = open(infile, 'r').readlines()
outlines = []
inheader = True
for l in lines:
if inheader and '-->' in l: inheader = False
if inheader:
if 'updated: ' in l or 'date: ' in l:
lineparts = l.split(': ')
predate = lineparts[1]
ctrldate = dateutil.parser.parse(predate)
date = dateutil.parser.parse(predate)
date = date.astimezone(dateutil.tz.gettz('UTC')).isoformat().replace('+00:00', 'Z')
outlines.append(lineparts[0] + ': ' + date +'\n')
else:
outlines.append(l)
else:
outlines.append(l)
open(infile, 'w').write(''.join(outlines))
|