You are currently viewing a single post of this guestblog.

Click here to go back the complete page. I would be glad to hear your comments, so don’t hesitate to leave me your feedback below. It will appear on the main guestblog page.

in categories  linux  opensource  permalink:  228   posted on 17.03.2013 3:50
From Yorik

You spend too much time in front of the computer

and you want to know how much, or maybe you simply want to know how long you have been working on something today. For either cases, I made this little script for linux, that you must launch at startup (put it in your startup apps list, or launch it manually). It will then record the time passed since you turned on your computer, and subtract the time your desktop was in screensaver mode, resulting in the time you were actually sitting doing something with the computer.

Of course it wont help you to know how long you have been working and how long you spent staring at facebook. But there are much better time-logging apps available on the net to solve that problem...

The script will store the current activity time in a ".activitytime" file inside your home folder, so you can just do "cat ~/.activitytime" from a terminal or from any other script (launcher, panel widget, etc...) to display your current activity time.

import sys,os,time

r = os.system("xprintidle")
if r != 0:
    print "Error: xprintidle was not found, and must be installed for this program to run"

total_idle = 0

def uptime():
    "returns uptime in minutes"
    with open("/proc/uptime", "r") as f:
        uptime_seconds = float(f.readline().split()[0])
        return int(uptime_seconds)/60
    return 0

def idletime():
    "returns idle time in minutes"
    global total_idle
    r = os.popen("xprintidle")
    idle_minutes = int(r.read())/60000
    if idle_minutes > 1:
        total_idle += 1
    return total_idle
def formattime(minutes):
    "formats atime in minutes to HH:MM"
    hours = minutes/60
    restminutes = minutes%60
    return str(hours).zfill(2)+":"+str(restminutes).zfill(2)
def writetime():
    "writes current activity time to a file"
    strtime = formattime(uptime() - idletime())
    print ("active time: "+strtime)
    fil = open(os.path.expanduser("~") + os.sep + ".activitytime","wb")
def readtime():
    "reads a temp file and adjusts elapsed idle"
    global total_idle
    fpath = os.path.expanduser("~") + os.sep + ".activitytime"
    if os.path.exists(fpath):
        fil = open(fpath,"r")
        r = fil.read()
        t = r.split(":")
        minutes = int(t[0])*60 + int(t[1])
        print "logged activity time: ",formattime(minutes)
        print "current uptime: ",formattime(uptime())
        total_idle = uptime() - minutes
        print "logged idle time: ",formattime(total_idle)

if __name__ == "__main__":
    if len(sys.argv) == 1:
        # if no argument is given, start from zero
        # with an argument, for ex --append, restart from currently saved time

    while True:

First and foremost, your name:

And your message:

To publish it, just press this ...