#!/usr/bin/env python # arbeitszeit.py # 2010-09-03 # by Gernot WALZL # Adds up durations between log-in and log-out time (see "man last"). # It gives an overview for each day. import os import sys display = ":0" username = os.getenv("USER") if (len(sys.argv) > 1): username = sys.argv[1] def strdur_to_mins(strdur): result = 0 try: [strhours, strminutes] = strduration.split(":") result = int(strhours)*60 + int(strminutes) except ValueError: pass return result def mins_to_strdur(mins): hours = mins/60 minutes = mins%60 return "{h:02d}:{m:02d}".format(h=hours,m=minutes) cmd = "last -R | grep \""+username+"\" | grep \" "+display+" \"" lastdate = "" sum_duration = 0 for line in os.popen(cmd).readlines(): strdate = line[22:32] strtime = line[33:38] strduration = line[49:54] if (lastdate == ""): lastdate = strdate minutes = strdur_to_mins(strduration) if (lastdate == strdate): sum_duration += minutes else: if (sum_duration > 0): print(lastdate + " | " + mins_to_strdur(sum_duration)) lastdate = strdate sum_duration = minutes if (sum_duration > 0): print(lastdate + " | " + mins_to_strdur(sum_duration))