Skip to content

Instantly share code, notes, and snippets.

@danzek
Last active January 2, 2018 21:43
Show Gist options
  • Save danzek/8f272f8c72b0aa3701bf to your computer and use it in GitHub Desktop.
Save danzek/8f272f8c72b0aa3701bf to your computer and use it in GitHub Desktop.
Parse Droid Time - Utility to parse Android Unix timestamps in CSV files
#!/usr/bin/python
#
# pdtime = parse Droid time - Utility to parse Android Unix timestamps in csv files
#
# Given a csv file or list of csv files containing Android timestamps, create a "parsed" directory and
# output new csv files with timestamps parsed in human-readable format, with no timezone adjustments.
# Android timestamps should be stored in UTC/GMT, and are parsed as such.
#
# Sample timestamp: 1311341729264, Android has three extra numbers than regular UNIX timestamps, because
# it stores UNIX epoch in milliseconds. It must be divided by 1000 to make it a normal UNIX timestamp.
"""
Copyright (c) 2011 Dan O'Day. All rights reserved.
This software distributed under the Eclipse Public License 1.0 (EPL-1.0)
http://www.opensource.org/licenses/EPL-1.0
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import sys
import os
import datetime
import csv
# Given an Android Unix timestamp (ts), return a human-readable date and time (hrdt)
def parseAndroidTS(ts):
hrdt = datetime.datetime.fromtimestamp(int(ts)//1000)
return hrdt
# Check if value is a valid Android timestamp, return true or false
def isAndroidTS(value):
if len(value.strip()) == 13:
try:
float(value)
return True
except ValueError:
return False
else:
return False
# Create directory after seeing if it exists
def createPath(path):
if not os.path.exists(path):
os.makedirs(path)
# Handles read/write operations in input and output csv files
def read_write_csvfile(reader, writer):
for row in reader:
for field_index, field in enumerate(row):
if isAndroidTS(field):
row[field_index] = parseAndroidTS(field)
writer.writerow(row)
# Receives csv file, write to new csv file with parsed timestamps
def load_save_csvfile(infilename, outfilename):
with open(infilename, 'rb') as infile:
with open(outfilename, 'w+b') as outfile:
reader = csv.reader(infile, lineterminator='\n')
writer = csv.writer(outfile, lineterminator='\n')
read_write_csvfile(reader, writer)
def main():
args = sys.argv[1:]
if not args:
print 'usage: ./pdtime.py csvfile {addtl csvfiles}\n'
sys.exit(1)
for csvfile in args:
createPath("parsed")
output_file = 'parsed/' + csvfile
load_save_csvfile(csvfile, output_file)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment