Convert A_TickCount - Time Since Boot

A_TickCount is a variable in AutoHotkey that holds the amount of milliseconds since Windows boot time. This variable works great internally for lots of scripts and programs not only for getting time since boot, but also for getting times that things run (E.g.: threads,hotkey execution etc...), but not too great for human readability. For example if I want to see how long my computer has been running I could use a message box, tooltip, or traytip to display the time in milliseconds:


MsgBox,64,Time Since Boot, %A_TickCount%

might output something like 13399343 millliseconds. You would then divide that number by 1000 to get seconds, then another 60 to get minutes and so on and so forth to get:


13399343/1000 = 13399 seconds
13399/60      = 223   minutes
223/60        = 3.71  hours
etc...

While this might not be hard to do it is much quicker to do the conversions in a function or class and so I have created a class called 'Convert'. This class will eventually encompass all sorts of math conversions, but for now I just have a conversion for A_TickCount. This class allows you to convert both individual total time (total seconds, total minutes etc..) and also allows you to convert for modulated remainders for a displayed, human readable format, e.g. 0:3:45:30 or  0 Days:3 Hours:45 Minutes:30 Seconds. In the class I have a way to return a single line format to send to a file or a multi-line format for display.

Examples

TrayTip,Time Since Boot,% Convert.TickCount.toDisplay()
 

Output:

While

FileAppend,% Convert.TickCount.toFile(),% "C:\Users\" A_Username "\Desktop\boottime.log"
    

will send:

0:4:59:3
 

to a file called 'boottime.log' on your desktop.

The main difference between, for example, say Hours() and toHours() is that any prefixed to____() modulates the returned value to only return the remainder for displayed time. The ____() will return the total corresponding Hours, Minutes etc... E.g. for 28 hours:

Hours()   == 28 hours
toHours() == 4 hours (the remainder after 1 day)
 

The Class

Class Convert {
 Class TickCount {
  toDays(count:=False){
   Return Floor(Mod((((((count:=count?count:A_TickCount)/1000)/60)/60)/24),24))
  }
  Days(count:=False){
   Return Floor(((((count:=count?count:A_TickCount)/1000)/60)/60)/24)
  }
  toHours(count:=False){
   Return Floor(Mod(Mod(((((count:=count?count:A_TickCount)/1000)/60)/60),60),24))
  }
  Hours(count:=False){
   Return Floor(((((count:=count?count:A_TickCount)/1000)/60)/60))
  }
  toMinutes(count:=False){
   Return Floor(Mod((((count:=count?count:A_TickCount)/1000)/60),60))
  }
  Minutes(count:=False){
   Return Floor((((count:=count?count:A_TickCount)/1000)/60))
  }
  toSeconds(count:=False){
   Return Floor(Mod(((count:=count?count:A_TickCount)/1000),60))
  }
  Seconds(count:=False){
   Return Floor(((count:=count?count:A_TickCount)/1000))
  }
  toDisplay(count:=False){
   count:=count?count:A_TickCount
   d:=Convert.TickCount.toDays(count)
   h:=Convert.TickCount.toHours(count)
   m:=Convert.TickCount.toMinutes(count)
   s:=Convert.TickCount.toSeconds(count)
   Return  "Days" A_Tab "Hours" A_Tab "Minutes" A_Tab "Seconds`n" 
     .  d A_Tab h A_Tab m A_Tab s "`nSince Windows Boot."
  }
  toFile(count:=False){
   count:=count?count:A_TickCount
   d:=Convert.TickCount.toDays(count)
   h:=Convert.TickCount.toHours(count)
   m:=Convert.TickCount.toMinutes(count)
   s:=Convert.TickCount.toSeconds(count)  
   Return d ":" h ":" m ":" s
  }
 }
}

0 comments:

Lateralus138's AutoHotkey Functions & Classes

Welcome to Lateralus138's AutoHotkey Functions & Classes!!! This is an archive and general information site for all of my great AutoHotkey functions and classes and probably full scripts in the future. Most functions, classes, and scripts will be posted as regular posts, but will also be available in an organized list in the corresponding 'Pages' section.

Nothing posted yet, but as soon as I do you can find all pages in the 'Pages' menu on the right or the 'AutoHotkey' section of the navigation bar at the top of the page. Plenty of content coming very soon, keep checking back!!!

Disclaimer: Any similarities to any one else's functions or classes are purely coincidental. All functions, classes, or scripts provided here are guaranteed to be either my scripts or the proper recognition will be noted. I write all AutoHotkey from my knowledge of the language/syntax/documentation.

0 comments: