I'd like to know how much time I've spent usefully and how much time I've wasted in a day/week/month. This could be done with a program, that one triggers when a task starts and when a task ends, recording the amount of time in between the triggers and then keeping stats in a database. Does some program, that allows this, exist for Windows or Linux?
Time Doctor seems to be a perfect fit for the requirements you mentioned.
It tracks your time with the simple trigger of a go button. And, you can click break when you are not working.
And yes, it stores everything in a database/dashboard. It's like giving you an analytics of your workday.
It's also open source. So, it works with Windows, Linux and OS X.
If you are interested, you can check more of its features here: http://www.timedoctor.com/features.html
If you are OK with an online version I suggest RescueTime. It installs some software on your computer, then monitors every executable you use and all webpages you visit.
These are all categorized (an advantage of being a centrally hosted solution) and after some time it shows you the time spent in these categories (either in your browser and/or my weekly email overview).
It 'knows' a large amount of programs/websites and those it does not recognize you can add to the categories. The categories you can lump together in productive/not-productive time, so that in its top-most aggregate you get a "percentage productive time".
The paid version has more facilities than the free one, but I suggest you give the free version a try first.
For a windows solution I would suggest Eylean Board. It is primarily a project/task management solution, but it has exactly the time tracking specification you have described.
With Eylean time tracking can be done automatically, with a trigger to stop if you have been idle for a while, or manually, where you simply input the time spent on tasks. You can always use a mix of both as well. Besides the software has a variety of features and is really easy to use.
KTimeTracker is a task management utility from KDE. It tracks how long you spend on different tasks and stores the records in portable formats. It allows you to:
Organize tasks into subtasks up to some 1000 levels
Edit task history and comments
Detect pauses. It stops recording when the computer is idle for a configurable amount of time
The time history can be exported to a comma-delimited text file for importing into other billing and/or project management tools.
It can be installed on Linux or Windows, but it comes as part of the Kontact suit and has to be installed along with all the other components.