Minutes Since Login?

I'm trying to calculate the time (in minutes) that the current user has been logged in. Here's what I've come up with.

Download: Minutes Since Login.kmmacros (3.7 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 14.1 (23B73)
  • Keyboard Maestro v11.0

Is there an easier way?

The who command should give you exactly what you want - look at the "console" entry next to the user in question.

I could be wrong, but I don't believe that date is reset by anything short of logging out.

EDIT: I didn't look hard enough at what you had, which is already pretty close. who should get you time1 a little more simply than searching last, but I can't think of a better way to do the time math that you're already doing.

1 Like

Thanks, @avtraino. The who command does simplify the script a bit. Thanks for the suggestion.

#!/bin/bash
PATH=$PATH:/usr/local/bin

# Get the current user login date/time
who_line=$(who | grep console)
line="${who_line##*console }"
time1=$(echo $line | xargs)

# Get the current date/time
time2=$(date)

# Input date/times in the specified formats
#time1="Oct 27 19:45"
#time2="Fri Oct 27 20:37:00 EDT 2023"

# Convert input date/times to seconds since the epoch
timestamp1=$(date -j -f "%b %d %H:%M" "$time1" "+%s")
timestamp2=$(date -j -f "%a %b %d %H:%M:%S %Z %Y" "$time2" "+%s")

# Return the time difference in minutes
echo $(( (timestamp2 - timestamp1)/60 ))