Created
April 9, 2010 01:23
-
-
Save epicserve/360759 to your computer and use it in GitHub Desktop.
Simple script to figure plain text time rows to output a time sheet with time totals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# figure-time.py | |
# | |
# Author: Brent O'Connor on 2010-04-08. | |
# Copyright 2010 Epicserve. All rights reserved. | |
# Version: 0.1.1 | |
from datetime import datetime | |
import sys | |
DATETIME_FORMAT="%m/%d/%Y %I:%M %p" | |
TIME_FORMAT="%I:%M %p" | |
def str_to_datetime(s, datetime_format=DATETIME_FORMAT): | |
return datetime.strptime(s, datetime_format) | |
def seconds_split(seconds): | |
"""Split seconds into a tuple of (hours, minutes, seconds)""" | |
hours = seconds / 3600 | |
seconds -= 3600*hours | |
minutes = seconds / 60 | |
seconds -= 60*minutes | |
return (hours, minutes, seconds) | |
def human_seconds(seconds, return_seconds=False): | |
"""Split seconds into a tuple of hours, minutes and seconds""" | |
hrs, mins, secs = seconds_split(seconds) | |
r = "%02d:%02d" % (hrs, mins) | |
if return_seconds: | |
r = "%02d:%02d:%02d" % (hrs, mins, secs) | |
return r | |
def figure_time(time_rows): | |
total_seconds = 0 | |
for l in time_rows: | |
csv = [x.strip() for x in l.split(",")] | |
date = csv[0] | |
start_time = str_to_datetime("%s %s" % (date, csv[1])) | |
end_time = str_to_datetime("%s %s" % (date, csv[2])) | |
total = (end_time-start_time) | |
total_seconds = total_seconds+total.seconds | |
time_row = { | |
'date': date, | |
'start_time': start_time.strftime(TIME_FORMAT), | |
'end_time': end_time.strftime(TIME_FORMAT), | |
'total': human_seconds(total.seconds) | |
} | |
print "%(date)s, %(start_time)s, %(end_time)s, %(total)s" % time_row | |
print "Total Time: %s" % human_seconds(total_seconds) | |
def main(): | |
if not sys.stdin.isatty(): | |
lines = sys.stdin.readlines() | |
figure_time(lines) | |
else: | |
help_msg = """ | |
Figure time requires that you pipe a text file containing time rows using the | |
example time row format. | |
Time Row Format: %m/%d/%Y, %I:%M %p, %I:%M %p | |
Example Time Row: 04/07/2010, 8:00 AM, 9:00 PM | |
Example: cat my_timesheet.txt | ./figure-time.py | |
""" | |
print help_msg | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment