RescueTime will do this. A component on your system tracks the amount of time you spend in each program (and on each website) and sends it to RescueTime. The website then displays charts and graphs and various tools that you can use to categorize the data and gauge your productivity. The "lite" version (and perhaps a Pro trial) is free, but there is a charge for advanced features.
If you're idle for a certain period of time (the time being configurable, if I remember correctly), it will just ask you what you were doing during that time. So if you are reading a very long question, it should mark n seconds of time as SO, then m seconds of time as idle, which would be left to you to categorize manually.
It sounds a bit hefty to implement in SO itself, but if it's just the information you're after, that will do it.
LeechBlock is also quite effective, if you just want to be cut off before spending too much time on SO. It doesn't have the fancy stats, though. :)