The more I think of it, the more I believe there’s a straight-forward solution somewhere there. We could update some variables on-demand on a couple of occasions. Also, I might be missing some stuff so bear with me.
Let’s say we store timestamps (temporarily) named last_measurement_timestamp,last_charged_timestamp and time_on_battery (which stores total seconds the device was active or in suspend mode).
When we just started the OS after fresh install, last_charged_timestamp is null, this would result in the UI showing just a - next to the Last Charged and Time on Battery entries in System Details. When last_charged_timestamp has a value, it’s displayed in the UI and Time on Battery gets populated by formatted time_on_battery value.
When starting the device, only one variable gets set:
last_measurement_timestamp = current_timestamp
When opening the System Details, those variables get updated internally like so:
time_on_battery = time_on_battery + current_timestamp - last_measurement_timestamp
last_measurement_timestamp = current_timestamp
When shutting down the device:
time_on_battery = time_on_battery + current_timestamp - last_measurement_timestamp
After you start charging the device, only the moment when you unplug it should get caught. This way it shouldn’t matter if it’s in suspend mode or not.
last_charged_timestamp = current_timestamp
Like I said, I might be missing some crucial details, but that seems like a simple way to get it done.