Advertisement
cd62131

timedelta

Sep 4th, 2017
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import sys
  3. from datetime import datetime, date, timedelta
  4. from collections import defaultdict
  5. dic = defaultdict(timedelta)
  6. with open(sys.argv[1]) as d:
  7.   in_zero = True
  8.   zero_time = None
  9.   in_one = False
  10.   one_time = None
  11.   for line in d:
  12.     e = line.rstrip().split()
  13.     time = datetime.strptime(' '.join(e[0:2]), '%Y/%m/%d %H:%M')
  14.     if (zero_time is None):
  15.       zero_time = time
  16.     if (in_zero and e[2] == '0'):
  17.       pass
  18.     elif (in_zero and e[2] == '1'):
  19.       in_zero = False
  20.       in_one = True
  21.       one_time = time
  22.     elif (in_one and e[2] == '0'):
  23.       if (zero_time.day != one_time.day):
  24.         midnight = datetime(*(one_time.date().timetuple()[:6]))
  25.         dic[zero_time.date()] += midnight - zero_time
  26.         dic[one_time.date()] += one_time - midnight
  27.       else:
  28.         dic[zero_time.date()] += one_time - zero_time
  29.       in_one = False
  30.       in_zero = True
  31.       zero_time = time
  32.     elif (in_one and e[2] == '1'):
  33.       one_time = time
  34.     else:
  35.       print('error')
  36.   if (in_one):
  37.     if (zero_time.day != one_time.day):
  38.       midnight = datetime(*(one_time.date().timetuple()[:6]))
  39.       dic[zero_time.date()] += midnight - zero_time
  40.       dic[one_time.date()] += one_time - midnight
  41.     else:
  42.       dic[zero_time.date()] += one_time - zero_time
  43. for k, v in sorted(dic.items()):
  44.   print('{0},{1}'.format(k, int(v.total_seconds() / 60)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement