Paste this into Bash:

get_screen_time() {
since=${1:-today}
until_opt=${2:+--until "$2"}

journalctl --output json -u systemd-logind --since "$since" $until_opt | \
jq -r 'select(.MESSAGE_ID and .__REALTIME_TIMESTAMP) |
if (.MESSAGE | test("Lid opened|Starting systemd-logind|Operation .suspend. finished")) then
  .__REALTIME_TIMESTAMP + " start"
elif (.MESSAGE | test("Lid closed|Stopping systemd-logind|system will suspend")) then
  .__REALTIME_TIMESTAMP + " stop"
else
  empty
end' | \
awk -v current_time=$(date +%s%6N) '
{
    if ($2 == "start") {
        last_start = $1
    } else if ($2 == "stop" && last_start != 0) {
        total += ($1 - last_start) / 1000000
        last_start = 0
    }
}
END {
    if (last_start != 0) {
        total += (current_time - last_start) / 1000000
    }
    seconds = int(total)
    hours = int(seconds/3600)
    minutes = int((seconds%3600)/60)
    printf "%02d:%02d", hours, minutes
}'
}
PS1='$(get_screen_time) '$PS1

Now you have screen time for today in your prompt:

00:21 user@asus:~/Documents$ 
00:21 user@asus:~/Documents$ cd ../Pictures/
00:21 user@asus:~/Pictures$

Cool?

    • podbrushkin@mander.xyzOP
      link
      fedilink
      arrow-up
      1
      arrow-down
      1
      ·
      10 hours ago

      I don’t know about android, but on iPhone only way to export screen time data is to screenshot it. Probably android is also restrictive.

    • podbrushkin@mander.xyzOP
      link
      fedilink
      arrow-up
      1
      ·
      18 hours ago

      If there is no lid (desktop pc), then it takes into account only systemd start and suspend messages, which is probably enough. Elegant!

    • podbrushkin@mander.xyzOP
      link
      fedilink
      arrow-up
      1
      ·
      18 hours ago

      Accumulative duration of working sessions. Work session is a period of time limited by any of “start” events and any of “end” events. E.g between “system started” and “suspend on”, or between “lid opened” and “system turn off”. Type of events determined by regex. Effectively it’s a screen time for most of people.

  • MonkderVierte@lemmy.zip
    link
    fedilink
    arrow-up
    7
    arrow-down
    13
    ·
    edit-2
    18 hours ago

    Relies on Systemd.

    Edit: try uptime. Or, Linux only, /proc/uptime.

    Editedit: uptime is for uptime. For last login is last -n1 or “logged in since <time>” is last -s <time>.