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?


Relies on Systemd.
Edit: tryuptime. Or, Linux only,/proc/uptime.Editedit:
uptimeis for uptime. For last login islast -n1or “logged in since <time>” islast -s <time>.Oh no, now it will be compatible only with most of the distros out there.
Only Linux distros. Which is, why i mentioned it.
uptimelastworks on BSD’s and not-Systemd’s too.Fair point, maybe in future consider adding more value to your initial comment, especially when you are referring to the usual discussion topics. Just stating that it uses systemd will make everyone think you want to start the usual pointless systemd hate nonsense discussion
Ok. But this this is a post in a Linux community. So one might assume it’s targeted towards Linux users.
I discovered it on /all, my bad.
Relies on bash
Relies on awk
Relies on the Gregorian calendar
That was mentioned in the title.
This was meant in jest. It does use systemd, which is also mentioned in the post.
Only in the script.
Well yeah, that’s what most people use
Cool. Some don’t. Reason enough to mention requirements.
Uptime shows how long the system has been up, not how long one has been interacting with the system.
Oh, right, my bad. But fret not there’s
last -n1for that.I don’t believe that does the same thing either. What if I lock my computer, sleep it, and step away for the day? I haven’t logged out, but my interactive session has ended.
Last shows how long the last user logged in has been logged in. So if your system routinely has multiple users logged in, this may not be a useful metric.
Yawn. Oh no youre using a different init than I would have preferred 🥺🥺🥺😣😭
This is what I picture everytime someone complains about systemd.
Systemd is great and at this point y’all just like being contrarian.
Stop complaining. I just mentioned the requirement because you didn’t.
There’s a whole cross-compatible command called
uptimelast, you know?Does
uptimecount or skip the time a system is in suspend? On mobile, can’t check manWhat, the logind thing?
oh the irony