Advertisement
Guest User

Fixing Android SMS timestamps

a guest
Jan 22nd, 2011
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from xml.dom.minidom import parse
  4.  
  5. import os
  6. import sys
  7. import time
  8.  
  9. START_TIMESTAMP_SECS = 1294290000000
  10. HOUR_TO_MILLISECS = 60 * 60 * 1000
  11. RECEIVED, SENT = (1, 2)
  12.  
  13. def format_time(timestamp_in_ms):
  14.     return time.strftime("%a, %d %b %Y %H:%M:%S",
  15.             time.gmtime(timestamp_in_ms / 1000))
  16.  
  17. def convert_est_to_utc(timestamp_in_ms):
  18.     return timestamp_in_ms + (5 * HOUR_TO_MILLISECS)
  19.  
  20. def replace_timestamps(document):
  21.     smses = document.getElementsByTagName('sms')
  22.     for sms in smses:
  23.         received = int(sms.getAttribute('type')) == RECEIVED
  24.         date = int(sms.getAttribute('date'))
  25.  
  26.         if received and date > START_TIMESTAMP_SECS:
  27.             sms.setAttribute('date', unicode(convert_est_to_utc(date)))
  28.  
  29. def main():
  30.     if (len(sys.argv) < 2):
  31.         sys.exit('Usage: %s sms_file' % os.path.basename(__file__))
  32.  
  33.     sms_file = sys.argv[1]
  34.  
  35.     document = parse(sms_file)
  36.     replace_timestamps(document)
  37.     print(document.toprettyxml(encoding='utf-8'))
  38.  
  39.  
  40. if __name__ == '__main__':
  41.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement