Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import sys
- from datetime import datetime, date, timedelta
- from collections import defaultdict
- dic = defaultdict(timedelta)
- with open(sys.argv[1]) as d:
- in_zero = True
- zero_time = None
- in_one = False
- one_time = None
- for line in d:
- e = line.rstrip().split()
- time = datetime.strptime(' '.join(e[0:2]), '%Y/%m/%d %H:%M')
- if (zero_time is None):
- zero_time = time
- if (in_zero and e[2] == '0'):
- pass
- elif (in_zero and e[2] == '1'):
- in_zero = False
- in_one = True
- one_time = time
- elif (in_one and e[2] == '0'):
- if (zero_time.day != one_time.day):
- midnight = datetime(*(one_time.date().timetuple()[:6]))
- dic[zero_time.date()] += midnight - zero_time
- dic[one_time.date()] += one_time - midnight
- else:
- dic[zero_time.date()] += one_time - zero_time
- in_one = False
- in_zero = True
- zero_time = time
- elif (in_one and e[2] == '1'):
- one_time = time
- else:
- print('error')
- if (in_one):
- if (zero_time.day != one_time.day):
- midnight = datetime(*(one_time.date().timetuple()[:6]))
- dic[zero_time.date()] += midnight - zero_time
- dic[one_time.date()] += one_time - midnight
- else:
- dic[zero_time.date()] += one_time - zero_time
- for k, v in sorted(dic.items()):
- print('{0},{1}'.format(k, int(v.total_seconds() / 60)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement