Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Chronos - the god of time ...


  • Please log in to reply
3 replies to this topic
Beastmaster
  • Guests
  • Last active:
  • Joined: --
I want to track the outage time of a process. Therefore I've decided to set the start time of the process in an INI file. With every restart of the process it will be checked if that status shows a value (eg. STARTED = 20040405122219) or has been set idle (STARTED = NO).

If a timestamp of a collapsed process still exists, that time stamp should be subst from the current one to get the elapsed time period.

What I want to accomplish:
a) to log the current outage period: ::
B) to cumulate that periods to get a monthly total :
:::

After several tries with CURRENTTIME -= %STARTED% combined with *= 60 /=3600 /=24 :? and other confusing loops - my brain collapsed. Now I need some chrono math genuis input, don't hesitate to show me the way ... :wink:

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
I'm not 100% sure that this will help you, but bear with me.

Try to take advantage of AHK's built in date math whenever possible because it makes things much easier. Here's an example from the help file:

var1 = 20050126
var2 = 20040126
var1 -= %var2%, days
MsgBox, %var1% ; The answer will be 366 since 2004 is a leap year.

Now in the above you can substitute "hours", "minutes", or "seconds" (instead of "days") to get a more precise answer.

This might also be of use, though you probably already know it:
The current time can be put into an variable in either of these ways:
EmptyVar += 0, days ; Must be empty for this to work.
AnyVar = %a_year%%a_mon%%a_mday%%a_hour%%a_min%%a_sec%

You might have been looking for a more complete answer than this. If so, feel free to ask more specific questions, or post your script and we can try to refine it.

Beastmaster
  • Guests
  • Last active:
  • Joined: --
Well I've done that so far (the helpfile is a special friend of mine :) ),
but doing it this way will "only" provide the total of an elapsed time (in sec or min or hour or day), it won't convert/split the total of seconds into a "readable" format:


eg. the conversion of one and a half hour would deliver this (.. , H)
00 01 00 00
eg. the conversion of one and a half hour to minutes would deliver this (.. , M)
00 00 90 00
eg. the conversion of one and a half hour to seconds would deliver this (.. , S)
00 00 00 5400

Cumulating this: 00 days, 01 hour, 90 minutes, 5400 seconds
Expected: 00 days, 4 hours, 30 minutes, 00 seconds

Statement:
I'm not a developer and won't become one this live. So, if any of the below is total nonsense or a full section of code could be shrinked to a single line, don't hesitate to have a laugh and afterwards provide a genius way to do it better :wink:


#SingleInstance 
#CommentFlag ;;

IniRead, STARTED, FB.INI, FBPROCSTATUS, STARTED, NO ;; Check if Start time has been set to "NO" in FB.INI after a previous run
If STARTED <> NO
	{
	IniRead, STARTED, FB.INI, FBPROCSTATUS, STARTED

	CTTOTAL= %A_YEAR%%A_MON%%A_MDAY%%A_HOUR%%A_MIN%%A_SEC%	;; Get current date & time --> <YYYY><MM><DD><hh><mm><ss> eg. 20040405151200 (4th of April 2004 - 15:12:00)


	CTTOTAL -=  %STARTED%, S ;; current total downtime in seconds
	CURRDOWNTIMESEC = %CTTOTAL% ;; keep the current downtime
	GoSUb, Outage

				;; MsgBox, 0, Test0, current total downtime in seconds: %CTTOTAL%
	H = %CTTOTAL%		;; set local variable
	H /= 3600		;; get number of hours (floating)
				;; MsgBox, 0, Test1, number of hours (floating): %H%
	GZH= %H%
	H *=3600		;; get number of hours (integer) in seconds
				;; MsgBox, 0, Test2, number of hours (integer) in seconds: %GZH%
	CTTOTAL -= %H%		;; subst number of hours in seconds from total downtime

				;; MsgBox, 0, Test3, subst number of hours in seconds from total downtime: %CTTOTAL%
	M = %CTTOTAL%		;; set local variable
	M /= 60			;; get number of minutes (floating)
				;; MsgBox, 0, Test4, number of minutes (floating)
: %M%
	GZM= %M%
	M *= 60			;; get number of hours (integer) in seconds
				;; MsgBox, 0, Test5, number of hours (integer) in seconds: %GZM%
	CTTOTAL -= %M%		;; subst number of hours in seconds from total downtime

				;; MsgBox, 0, Test6, subst number of hours in seconds from total downtime: %CTTOTAL%

	If GZH < 10
		GZH = 0%GZH%

	If GZM < 10
		GZM = 0%GZM%

	If CTTOTAL < 10
		CTTOTAL = 0%CTTOTAL%

									;; MsgBox, 0, Outage, Outage: %GZH%:%GZM%:%CTTOTAL%, 10
	IniWrite, %GZH%:%GZM%:%CTTOTAL%, FB.INI, FBPROCSTATUS, DOWNTIME	;; Write INI
	}
Exit

;-----

Outage:
	IniRead, DOWNTIMESEC, FB.INI, FBPROCSTATUS, DOWNTIMESEC     ;; Read start time of collapsed process from INI file
	CURRDOWNTIMESEC += %DOWNTIMESEC%			    ;; Add previous and current downtime to get a downtime total
	IniWrite, %CURRDOWNTIME%, FB.INI, FBPROCSTATUS, DOWNTIMESEC ;; Write that value to the INI
	Return

If you want to have a try:
a) Copy the code to notepad (or even better - an AHK script template), switch of word wrap & btw, there are a bunch of MsgBoxes set (disabled) for testing purposes.

B) Create an INI at the same directory as the script.

Filename: FB.INI
[FBPROCSTATUS]
STARTED=20040405121000
DOWNTIMESEC=
DOWNTIME=

-----

SetFormat hasn't worked for me :cry: , and I was to annoyed to check this out ...

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Hopefully this will help until such time as there is a built-in function to do this type of conversion. The below is a complete working script, so you can either try it out or just copy & paste the subroutine part into your own script.

#SingleInstance

TimeStart = 20040401
TimeFinish = 20040405013150

; Measure the difference in seconds:
ConvertSeconds = %TimeFinish%
ConvertSeconds -= %TimeStart%, seconds

Gosub, GetConvertResult
MsgBox, %ConvertSeconds% seconds is equivalent to %ConvertResult%

Exit



GetConvertResult:
; This subroutine takes the variable ConvertSeconds (set by the caller)
; and stores the converted time in the variable ConvertResult.

; Make a copy so that the caller's ConvertSeconds variable is not modified:
GCRTemp = %ConvertSeconds%

; Make floating point math more precise so that seconds are more accurate
; (this prevents it from being off by 1 second sometimes):
SetFormat, float, 0.20

; Convert to days by dividing by the number of seconds in a day.
GCRTemp /= 86400.0

; Truncate the decimal portion, leaving only the whole number of days in CountDays:
StringGetPos, DecimalPos, GCRTemp, .
StringLeft, CountDays, GCRTemp, %DecimalPos%

GCRTemp -= %CountDays%
; Now GCRTemp contains something like 0.513, which is the number of
; "partial days" remaining that aren't yet reflected in CountDays.
GCRTemp *= 24 ; Now GCRTemp contains the number of remaining hours.

; Truncate the decimal portion, leaving only the whole number of hours in CountHours:
StringGetPos, DecimalPos, GCRTemp, .
StringLeft, CountHours, GCRTemp, %DecimalPos%

GCRTemp -= %CountHours%
; Now GCRTemp contains something like 0.513, which is the number of
; "partial hours" remaining that aren't yet reflected in CountHours.
GCRTemp *= 60 ; Now GCRTemp contains the number of remaining minutes.

; Truncate the decimal portion, leaving only the whole number of minutes in CountMinutes:
StringGetPos, DecimalPos, GCRTemp, .
StringLeft, CountMinutes, GCRTemp, %DecimalPos%

GCRTemp -= %CountMinutes%
; Now GCRTemp contains something like 0.513, which is the number of
; "partial minutes" remaining that aren't yet reflected in CountMinutes.
GCRTemp *= 60 ; Now GCRTemp contains the number of remaining seconds.

; Truncate the decimal portion, leaving only the whole number of seconds in CountSeconds:
StringGetPos, DecimalPos, GCRTemp, .
StringLeft, CountSeconds, GCRTemp, %DecimalPos%

ConvertResult = %CountDays%d %CountHours%h %CountMinutes%m %CountSeconds%s

return