Is there a way to tell how long your Mac was turned on on a distinctive day?

I would like to know how long my MacBook Pro was on today - total uptime for the day. I just bought it, and I thought the battery was bad but it went on for longer than I thought. Of course, I wasn't standing next to it with a stopwatch... It would be a great feature for me to track time spent on devices anyways, to see which ones I spend the most time on. Maybe in System Report, or using a tool in Utilities? A script?

EDIT: I am asking this to do this on different computers, some built before 2011.


Solution 1:

I have uploaded a bash script as a Gist to my Github account at print_osx_uptimes.sh which may help answer your question, even if not exactly what was asked (which was more complicated).

The script will query the Apple System Log (ASL) facility for 'shutdown' messages. The 'Message' field’s values contain the system uptime as of the shutdown event, which I figured is probably reasonably close to the original poster’s question.

It then converts the uptime seconds to a more readable format showing days, hours, minutes and seconds.

Tested on OS X 10.10.5, but see no reason it should not work on anything from 10.7 onward (likely 10.6 as well).

Edited code to also check for (duh) 'reboot' messages, and updates the output to show what the event type (ie. Sender name) was for each.

Sample output:

$ ./print_osx_uptimes.sh
Shutdown on date/time:         Event                     Reported uptime of:
=============================  =========  ===================================
Mon Jul  6 2015 10:32:21 EDT   shutdown       1 days  11 hrs  30 min  15 secs
Thu Jul 16 2015 20:47:24 EDT   reboot         1 days  22 hrs  15 min  46 secs
Thu Jul 16 2015 22:01:50 EDT   shutdown       0 days  21 hrs   4 min  28 secs
Sun Jul 19 2015 02:10:34 EDT   shutdown       2 days  16 hrs  35 min  35 secs
Tue Jul 21 2015 19:32:35 EDT   shutdown       9 days  17 hrs  41 min  46 secs
Fri Jul 24 2015 20:35:05 EDT   shutdown       9 days   7 hrs  55 min  38 secs
Sat Jul 25 2015 00:03:13 EDT   shutdown       8 days  10 hrs  45 min   8 secs
Tue Jul 28 2015 09:16:47 EDT   shutdown       5 days  21 hrs  20 min  57 secs
Thu Jul 30 2015 22:32:36 EDT   shutdown      10 days   0 hrs   3 min  59 secs
Tue Aug  4 2015 01:37:32 EDT   shutdown       7 days   4 hrs  24 min  35 secs
Fri Aug 14 2015 19:15:04 EDT   reboot         5 days  22 hrs  42 min   1 secs

The meat of the script on Github Gist is:

#!/bin/bash

# https://gist.github.com/jps3/afc1e374b632e1ed1ebc

# function displaytime() is a modified form of that from:
# http://unix.stackexchange.com/a/27014
function displaytime {
  local T=$1
  local D=$((T/60/60/24))
  local H=$((T/60/60%24))
  local M=$((T/60%60))
  local S=$((T%60))
  printf '%5s days  %2s hrs  %2s min  %2s secs' $D $H $M $S
}

function get_syslog_entries () {
  syslog -d store \
    -F '$(Sender) $(Message)' \
    -k Sender Req '^(shutdown|reboot)$' \
    -k Message Req "SHUTDOWN_TIME"
}

while read -a line; do
  sender="${line[0]}"
  log_entry_timestamp="$(date -j -f %s "${line[2]}" +"%a %b %e %Y %T %Z")"
  uptime_timestamp="$(displaytime "${line[3]}")"
  printf "%s   %-8s   %s\n" "${log_entry_timestamp}" "${sender}" "${uptime_timestamp}"
done < <(get_syslog_entries)

Hope this helps. I tried to clean up the script and make the parts and comments useful.

Solution 2:

You may be able to calculate based on total uptime.

See this article on osXdaily.

How to Check a Mac's Uptime

To check your Mac's uptime, simply type ‘uptime’ in the Terminal. Terminal is an application found within /Applications/Utilities/ and is included in every version of Mac OS X.

uptime

Hit the return key and you’ll find the answer.

To check daily uptime with multiple shut-downs in the day, run this command before shutting down and log the result somewhere such as a spreadsheet or text file. Then it is an easy calculation based on date and duration. But really, unless you have poor security, there is not much need to shutdown very often. Sleep uses very little power.

See this Apple Support article:

Standby Mode

For Mac computers that are started from an solid-state drive, OS X includes a deep sleep mode known as Standby Mode. Mac computers manufactured in 2013 or later enter standby after one to three hours of regular sleep. A computer with a fully charged battery can remain in standby for up to thirty days without being plugged in to an AC power source.

Solution 3:

A third party tool such as DssW Sleep Monitor can be used to track the amount of time your Mac was powered on. The application tracks power and battery use. Totals are available in the applications' Info panel.

DssW Sleep Monitor on OS X

I wrote Sleep Monitor, so am bias. Also please note Sleep Monitor 3 works on OS X 10.7 – 10.9.