Calculate time since boot machine

I’m trying to display the time elapsed since the Mac started, but only the minutes are correct, the number of days and the hours are wrong. Why ?

Dim jours, heures, minutes As Integer
Var lnTick As Double = System.Microseconds

heures = lnTick / 3600000
if heures > 23 then 'hours
  jours = heures/24
  heures = (heures mod 24)
else
  jours = 0 'days
end

lnTick = (lnTick mod 3600000)
minutes = lnTick / 60000
//Secondes = (lnTick Mod 60000) / 1000

MsgBox "Your computer has been on for "+ Str(jours)+" days" + Str(heures)+" hours " + Str(minutes)+" minutes. "

Ultimately, I would like:
< 1 day: only display the hour and minutes

= 1 day; display day, hour and minutes

Thanks,
Daniel

I get:

Your computer has been on for 182 days7 hours 50 minutes.

on my m1 MacBook Pro. I fired it around 8h15 this morning (it’s 12H50 now)…

Statistiques données par macOS (à 13H24):

Sub Pressed() Handles Pressed
  
  Var days As Integer = 0
  Var hours As Integer = 0
  Var minutes As Integer = 0
  
  Var sec As Double = System.Microseconds / 1000000.0 '  1 sec is 1000000 µs
  
  While sec > 0.0
    If sec >= 3600.0 * 24.0 Then
      sec = sec - 3600.0 * 24.0
      days = days + 1
    ElseIf sec >= 3600.0  Then
      sec = sec - 3600.0
      hours = hours + 1
    ElseIf sec >= 60.0 Then
      sec = sec - 60.0
      minutes = minutes + 1
    Else
      sec = -1.0
    End If
  Wend
  
  MsgBox "Your computer has been on for " + Str(days) + " days " + Str(hours) + " hours " + Str(minutes) + " minutes."
  
End Sub
1 Like

Seems OK to me:

1 Like

Thanks @MarkusR

There are occasions where this will return misleading information. The online docs do mention this issue. Basically, on Windows at least, a Restart and a Shutdown/Reboot re not the same. Tthe System.Microseconds value doesn’t get reset if the user has done a Shutdown. This is because (unless disabled in Widows settings) Windows saves a fast-start “environment” so it can start up faster when the machine is restarted. The microseconds from boot are saved in this environment and reloaded when starting. This is why you can turn on the machine, run the test, and have it report 8 days… from startup.

When the user does a Restart, it is a true restart and the entire system, including the microseconds from boot, are reset.

Macs may have the same situation or not. I don’t have a Mac to test with.

There’s another approach:

Var Total As Double=System.Microseconds/1000000

Var Seconds As Integer=Total mod 60
Total=(Total-Seconds)/60

Var Minutes as Integer=Total mod 60
Total=(Total-Minutes)/60

Var Hours As Integer=Total mod 24
Total=(Total-Hours)/24

Var Days As Integer=Total

on macOS the “w” command does the trick - you could parse this from a Shell perhaps?

/usr/bin/w
15:46  up 3 days, 22:23, 2 users, load averages: 1.36 1.59 1.42

“w” seems to put out a lot more information. I’ve always used “uptime” which is easier to remember and only provided the one line.

/usr/bin/uptime
10:15  up 4 days, 12:48, 2 users, load averages: 2.54 2.78 2.46

Alternatively a pure Xojo answer and easy to follow:

Var dZero As New DateTime(0)
Var dNow As New DateTime(System.Microseconds / 1000000)
Var dInterval As DateInterval = dNow - dZero

Yields a DateInterval object with properties of Days, Hours, Minutes and Seconds, even Years if required.