clock


A peculiar way to get the biggest (max/maximum) value between two variables using bitwise operations

Recently, we wanted to make a test and see how we could find the maximum value between two variables using bitwise operations.

We ended up with the following peculiar way to get the biggest value between two variables using bitwise operations

r = a ^ ((a ^ b) & -(a < b));

The above formula has two modes:

  1. When a < b
  2. When a >= b

 

When a < b then the formula will change as follows:

r = a ^ ((a ^ b) & 0xFFFFFFFF);

As we all (should) know, when one of the operators on a bitwise AND operation is composed only from 1s, then the result is whatever value the other operator was holding.
So, the formula then simplifies as follows:

r = a ^ (a ^ b);

which is equal to

r = b;

because we when we apply twice the same value using XOR on another value, we revert back to the original value (so the second ^a nullifies the first ^a)

 

When a >= b then the formula will change as follows:

r = a ^ ((a ^ b) & 0x00000000);

When one of the operators on a bitwise AND operation is composed only from 0s, then the result is always 0 no matter what value the other operator was holding.
So, the formula then simplifies as follows:

r = a ^ (0x00000000);

which is equal to

r = a;

because when one of the operators in a XOR operation is only composed from 0s then the result will be the value of the other operator, no matter what it was.

 

Full example

Below you will find a full example that compares the execution speed of the two methods by executing each several thousands of time on the same random data.

Bitwise-Max.c (compressed) (121 downloads)

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main() {
    {
        const clock_t start = clock();

        srand(10);
        unsigned long int i;
        unsigned int max = 0;
        for (i = 0; i < 1000000000; i++) {
            const int a = rand();
            max = max < a ? a : max;
        }
        const clock_t end = clock();
        const float seconds = (float) (end - start) / CLOCKS_PER_SEC;
        printf("Seconds elapsed %f\tIf statement. Overall max value = %u\n", seconds, max);
    }

    {
        const clock_t start = clock();

        srand(10);
        unsigned long int i;
        unsigned int max = 0;
        for (i = 0; i < 1000000000; i++) {
            const int a = rand();
            max = a ^ ((a ^ max) & -(a < max));
        }
        const clock_t end = clock();
        const float seconds = (float) (end - start) / CLOCKS_PER_SEC;
        printf("Seconds elapsed %f\tBitwise operation. Overall max value = %u\n", seconds, max);
    }
    return 0;
}

Results

Our results show that using the traditional if statement with assignment is faster than using our formula as expected.
Which makes sense as there is an if statement in the formula as well and then additional operations to get the result, instead of just the assignment.

Seconds elapsed 5.770000 If statement. Overall max value = 2147483647
Seconds elapsed 6.180000 Bitwise operation. Overall max value = 2147483647

10 times bigger input

Seconds elapsed 57.450001 If statement. Overall max value = 2147483647
Seconds elapsed 63.869999 Bitwise operation. Overall max value = 2147483647
Advertisements

C/C++: Comparing the performance of syslog vs printf

The following code tries to compare the performance of syslog() with the printf() command. printf_vs_syslog.c (compressed) (279 downloads)
On our machine, it appears that syslog() is faster than printf().

To be as fair as possible, when the application was executing, we were monitoring the system logs as well, so that they will be printed on screen.
On CentOS 7, you can see the syslog in the file /var/log/messages.
The command we used was: sudo tail -f /var/log/messages

Results:

printf: Seconds elapsed 0.480000
syslog: Seconds elapsed 0.180000

Full source code for test:

printf_vs_syslog.c (compressed) (279 downloads)
#include <stdio.h>
#include <syslog.h>
// #include <stdlib.h> is needed for the resolution of EXIT_SUCCESS
#include <stdlib.h>
// #include <time.h> is needed for the clock() function and the macro CLOCKS_PER_SEC
#include <time.h>
// #include <unistd.h> and #include <sys/types.h> are needed for the functions uid_t getuid(void); and uid_t geteuid(void);
//getuid() returns the real user ID of the calling process.
//geteuid() returns the effective user ID of the calling process.
//These functions are always successful.
#include <unistd.h>
#include <sys/types.h>

#define RANGE (100000)

int main()
{
    {
        const clock_t start = clock();

        unsigned int i;
        for (i = 0; i < RANGE; i++){
            printf ("Program started by Real User %u (Effective User %u)\n", getuid(), geteuid());
        }
        printf("\n");

        const clock_t end = clock();
        const float seconds = (float) (end - start) / CLOCKS_PER_SEC;
        printf("printf: Seconds elapsed %f\n", seconds);
    }
    {
        const clock_t start = clock();

        setlogmask (LOG_UPTO (LOG_NOTICE));
        openlog ("bytefreaks", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
        unsigned int i;
        for (i = 0; i < RANGE; i++){
            syslog (LOG_NOTICE, "Program started by Real User %u (Effective User %u)", getuid(), geteuid());
        }
        closelog ();

        const clock_t end = clock();
        const float seconds = (float) (end - start) / CLOCKS_PER_SEC;
        printf("syslog: Seconds elapsed %f\n", seconds);

    }
    return EXIT_SUCCESS;
}


printf_vs_syslog.c (compressed) (279 downloads)

C: Code to time execution with accuracy greater than a second

The following application computes the time needed for a process to finish using the method clock().
The result of the application is the time in seconds as a floating number (where 1.0 = 1 second).
It provides greater accuracy than seconds as the estimation is done using processor time used by the program.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <limits.h>

int main()
{

    /* clock_t clock(void)
     The clock() function returns an approximation of processor time used by the program.
     The value returned is the CPU time used so far as a clock_t,
     to get the number of seconds used, divide by CLOCKS_PER_SEC.
     On error it returns -1. */
    const clock_t start = clock();

    /* svoid srand(unsigned int __seed)
     The srand() function sets its argument as the seed for a new sequence of pseudo-random
     integers to be returned by rand(). These sequences are repeatable by calling srand() with the
     same seed value.
     If no seed value is provided, the rand() function is automatically seeded with a value of 1. */
    /* time_t time(time_t *__timer)
     time() returns the time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds.
     If the __timer variable is not NULL, the return value is also stored there. */
    srand(time(NULL));
    unsigned long i;
    for (i = 0; i < 10000000; i++)
    {
        /* int rand(void)
         The rand() function returns a pseudo-random integer in the range 0 to RAND_MAX inclusive. */
        rand();
    }
    const clock_t end = clock();

    /* ISO/IEC 9899:1999 7.23.1: Components of time
    The macro `CLOCKS_PER_SEC' is an expression with type `clock_t' that is
    the number per second of the value returned by the `clock' function. */
    /* CAE XSH, Issue 4, Version 2: <time.h>
    The value of CLOCKS_PER_SEC is required to be 1 million on all
    XSI-conformant systems. */
    const float seconds = (float) (end - start) / CLOCKS_PER_SEC;

    printf("Seconds elapsed %f\n", seconds);
    return 0;
}

On-screen clock/count down/counter (version 3, Java)

We ported the original watch from AutoHotKey to Java.

UPDATE: Click here  Watch v3 - Windows executable (1463 downloads) to download the application with a Windows launcher.

You can download the Java Jar version from this link : Watch v3.0 Java (346 downloads)

It started being used in the 106th IEEE Region 8 Committee Meeting that was held at the Fairmont Monte Carlo in Monaco on 19-20 of March 2016.

Key features

  • You can change the icon that appears in the task bar by placing an gif file with the name watch.gif in the config directory of the executable
  • You can drag the watch to a more convenient place with the mouse
  • You can move the watch using the arrow keys. Using the shift key it makes the movement more precise. Using the control key it makes the movement faster.
  • Double clicking on the watch it will hide it
  • Double clicking on the tray menu icon of the watch it will toggle it’s visibility
  • From the menu on the task bar you can “Set it on top”, allow mouse to click through the clock, hide/show the gui
  • You can set the color of the Font and the Color of the background using a color picker
  • You can set the transparency of the GUI
  • The tool has two modes, it can operate as a clock that shows time in 24 hour mode or AM/PM and can operate as a count down tool (timer)
  • The timer mode allows you to pause (and continue), reset and stop the time, reset and continue the time (using lap) or zero the clock

Good willed feedback is always welcome


On-screen clock/count down/counter — AutoHotkey (AHK) Source Code

Below is the source code for the version that is used for IEEE Region 8 Committee meetings.

It is written using the AutoHotkey scripting language and you will need to download the compiler for it in order for you to use it.

AutoHotkey is a free, open-source custom scripting language for Microsoft Windows, initially aimed at providing easy keyboard shortcuts or hotkeys, fast macro-creation and software automation that allows users of most levels of computer skill to automate repetitive tasks in any Windows application. User interfaces can easily be extended or modified by AutoHotkey (for example, overriding the default Windows control key commands with their Emacs equivalents). The Autohotkey installation includes its own extensive help file with an always updated web based version.From Wikipedia

Download here watch.v2.ahk.zip (1326 downloads)

#SingleInstance Force
#Persistent
#NoEnv
DetectHiddenWindows On
SetTitleMatchMode 2

st_Configuration_File := "watch.ini"
st_Configuration_Section := "Parameters"
st_Settings_Title := "Watch 2.0 - UI Settings"
st_Mode_Title := "Watch 2.0 - Mode Settings"

;ListVars
;http://www.autohotkey.com/docs/commands/ListVars.htm

;----------------------Check if Settings File Exists-----------------
;IniRead, OutputVar, Filename, Section, Key [, Default]
st_Clock_Width := "Clock_Width"
IniRead, var_Clock_Width, %st_Configuration_File%, %st_Configuration_Section%, %st_Clock_Width%, 245
st_Clock_Height := "Clock_Height"
IniRead, var_Clock_Height, %st_Configuration_File%, %st_Configuration_Section%, %st_Clock_Height%, 66
st_Lock_Position := "Lock_Position"
IniRead, var_Lock_Position, %st_Configuration_File%, %st_Configuration_Section%, %st_Lock_Position%, 0
st_Always_On_Top := "Always_On_Top"
IniRead, var_Always_On_Top, %st_Configuration_File%, %st_Configuration_Section%, %st_Always_On_Top%, 1
st_Position_X := "Position_X"
st_Default_Position_X := A_ScreenWidth - var_Clock_Width
IniRead, var_Position_X, %st_Configuration_File%, %st_Configuration_Section%, %st_Position_X%, %st_Default_Position_X%
st_Position_Y := "Position_Y"
st_Default_Position_Y := A_ScreenHeight - var_Clock_Height
IniRead, var_Position_Y, %st_Configuration_File%, %st_Configuration_Section%, %st_Position_Y%, %st_Default_Position_Y%
st_Click_Through_Clock := "Click_Through_Clock"
IniRead, var_Click_Through_Clock, %st_Configuration_File%, %st_Configuration_Section%, %st_Click_Through_Clock%, 0
st_Back_Color := "Back_Color"
IniRead, var_Back_Color, %st_Configuration_File%, %st_Configuration_Section%, %st_Back_Color%, 0x000000
st_Clock_Color := "Clock_Color"
IniRead, var_Clock_Color, %st_Configuration_File%, %st_Configuration_Section%, %st_Clock_Color%, 0xECECEE
st_Clock_Font_Size := "Clock_Font_Size"
IniRead, var_Clock_Font_Size, %st_Configuration_File%, %st_Configuration_Section%, %st_Clock_Font_Size%, 28
st_Clock_Mode := "Clock_Mode"
IniRead, var_Clock_Mode, %st_Configuration_File%, %st_Configuration_Section%, %st_Clock_Mode%, 24Hour
st_Timer_Mode := "Timer_Mode"
IniRead, var_Timer_Mode, %st_Configuration_File%, %st_Configuration_Section%, %st_Timer_Mode%, FixedTime
st_Transparency := "Transparency"
IniRead, var_Transparency, %st_Configuration_File%, %st_Configuration_Section%, %st_Transparency%, 230
st_Watch_Mode := "Watch_Mode"
;IniRead, var_Watch_Mode, %st_Configuration_File%, %st_Configuration_Section%, %st_Watch_Mode%, Clock
;Xeirwn -- Hardcoded clock as default mode
var_Watch_Mode := "Clock"
SetTimer, fun_TicTac, 1000
;------------------------------------------------------------

Gui, 1:+ToolWindow -Caption
Gui, 1:Color, %var_Back_Color%, %var_Back_Color%
Gui, 1:Font, S%var_Clock_Font_Size%
Gui, 1:Font, C%var_Clock_Color%, bold, Arial
Gui, 1:Font, bold
Gui, 1:Font, Arial
FormatTime, var_Formatted_Time, , '  'HH:mm:ss
Gui, 1:Add, text, X5 Y5 W%var_Clock_Width% H%var_Clock_Height% GMove vElem_Clock, %var_Formatted_Time%
Gui, 1:Show, X%var_Position_X% Y%var_Position_Y% W%var_Clock_Width% H%var_Clock_Height%, elem_Watch_Window
WinSet, Region, 0-0 W%var_Clock_Width% H%var_Clock_Height% R6-6, elem_Watch_Window
;;WinSet, Region, 0-0 W%var_Clock_Width% H%var_Clock_Height% R6-6, elem_Watch_Window
WinSet, Transparent, %var_Transparency%, elem_Watch_Window

ifExist, watch.ico
{
	Menu, Tray, Icon, watch.ico
}
Menu, Tray, Tip, Watch 1.0
Menu, Tray, NoStandard
Menu, Tray, add, &Always On Top, fun_Set_Always_On_Top
Menu, Tray, add, Click &Through, fun_Click_Through
;Menu, Tray, add, &Lock Position, fun_Lock_Position
Menu, Tray, add, Clock &Colors, fun_Set_Clock_Colors
Menu, Tray, add, &Set Mode, fun_Set_Mode
Menu, Tray, add
Menu, Tray, add, &Quit Clock, ExitApp
Menu, Tray, add, Hide/&Show, fun_Toggle_Hide
Menu, Tray, add
Menu, Tray, add, About, fun_About
Menu, Tray, default, Hide/&Show

if (var_Click_Through_Clock = "1")
{
	Gui, 1:+E0x20
	Menu, Tray, Check, Click &Through
}

if (var_Always_On_Top = "1")
{
	WinSet, AlwaysOnTop, On, elem_Watch_Window
	Menu, Tray, Check, &Always On Top
}

if (var_Lock_Position = "1")
{
	;TODO -- XEIRWN -- REVIEW
	Gui, 1:-E0x10
	Menu, Tray, Check, &Lock Position
}

;----------------------Set Transparency Using the Alt Key--------------------
#ifWinActive, elem_Watch_Window
{
	!WheelUp::
	if (var_Transparency <= 230) { var_Transparency += 5 WinSet, Transparent, %var_Transparency%, elem_Watch_Window return } !WheelDown:: if (var_Transparency >= 15)
	{
		var_Transparency -= 5
		WinSet, Transparent, %var_Transparency%, elem_Watch_Window
		return
	}

	return
}
;------------------------------------------------------------
return

;;TODO -- XEIRWN -- REVIEW
;----------------------Enable Window Drag-------------------
Move:
	PostMessage, 0xA1, 2,,, A
	if A_GuiEvent = DoubleClick
	{
		GoSub, fun_Show_Hide
	}
return
;------------------------------------------------------------

;----------------------Clock Engine-----------------------
fun_TicTac:
	if (var_Watch_Mode = "Clock")
	{
		FormatTime, var_Formatted_Value, , '  'HH:mm:ss
		if (var_Clock_Mode = "AM/PM")
		{
			FormatTime, var_Formatted_Value, , hh:mm:ss tt
		}
	}
	else if (var_Watch_Mode = "Timer")
	{
		if (var_Timer_Mode = "FixedTime")
		{
			FormatTime, var_Seconds, %var_Set_FixedTime%, s
			FormatTime, var_Minutes, %var_Set_FixedTime%, m
			FormatTime, var_Hours, %var_Set_FixedTime%, H
			if (var_Seconds > "0" or var_Minutes > "0" or var_Hours > "0")
			{
				var_Set_FixedTime += -1, seconds
			}
			else
			{
				SetTimer, fun_TicTac, off
			}
			FormatTime, var_Formatted_Value, %var_Set_FixedTime%, '  'HH:mm:ss
		}
		else
		{
			;TODO Xeirwn -- Implement count to date time
			;http://www.autohotkey.com/docs/commands/EnvSub.htm
			;http://www.autohotkey.com/docs/commands/FormatTime.htm
			;http://www.autohotkey.com/docs/Variables.htm
		}
	}
	GuiControl,, Elem_Clock, %var_Formatted_Value%
	return
;------------------------------------------------------------

;----------------------Clock Engine Maintenance-----------------------
fun_TicTacToc:
	if (var_Watch_Mode = "Clock")
	{
	}
	else if (var_Watch_Mode = "Timer")
	{
		if (var_Timer_Mode = "FixedTime")
		{
			FormatTime, var_Formatted_Value, %var_Set_FixedTime%, '  'HH:mm:ss
		}
	}
	GuiControl,, Elem_Clock, %var_Formatted_Value%
	return
;------------------------------------------------------------

;---------------------Show Or Hide Clock---------------------
fun_Show_Hide:
fun_Toggle_Hide:
	var_Toggle := !var_Toggle
	if (var_Toggle = "1")
	{
		WinHide, elem_Watch_Window
	}
	else
	{
		WinShow, elem_Watch_Window
	}
return
;------------------------------------------------------------

;---------------------Don't Save Position--------------------
fun_Lock_Position:
	if (var_Lock_Position = "1")
	{
		;XEIRWN -- TODO -- REVIEW
		Gui, 1:-E0x10
	}
	else
	{
		;XEIRWN -- TODO -- REVIEW
		Gui, 1:+E0x10
	}
	var_Lock_Position := !var_Lock_Position
	Menu, tray, togglecheck, &Lock Position
	IniWrite, %var_Lock_Position%, %st_Configuration_File%, %st_Configuration_Section%, %st_Lock_Position%
return
;------------------------------------------------------------

;----------------------Set Clock Colors----------------------
fun_Set_Clock_Colors:
	SPosX := (A_ScreenWidth)/2 
	SPosY := (A_ScreenHeight)/2
	Gui, 2:Color, black
	Gui, 2:+ToolWindow +AlwaysOnTop -Caption
	Gui, 2:Font, S7 cwhite, Verdana
	Gui, 2:Add, Text, x6 y6 w130 h20 gMove, %st_Settings_Title%
	Gui, 2:Add, Text, x148 y6 w20 h20 gGui2Close, X
	Gui, 2:Add, GroupBox, x6 y25 w150 h75 , Colors
	Gui, 2:Add, Text, x16 y45 w80 h20 , Background:
	Gui, 2:Add, Button, x96 y45 w55 h17 vBack_Color_Btn gfun_Set_Back_Color, %var_Back_Color%
	Gui, 2:Add, Text, x16 y75 w100 h20 , Clock Color:
	Gui, 2:Add, Button, x96 y75 w55 h17 vClock_Color_Btn gfun_Set_Clock_Color, %var_Clock_Color%
	Gui, 2:Add, GroupBox, x6 y103 w150 h38 Right, Transparency
	Gui, 2:Add, Slider, x16 y118 w130 h18 Range15-230 gfun_Set_Transparency vTrans_Value, %var_Transparency%
	Gui, 2:Show, X%SPosX% Y%SPosY% W163 H152, %st_Settings_Title%
	WinSet, Region, 0-0 w163 h152 R10-10, elem_Colors_Window
	WinSet, Transparent, 237, elem_Colors_Window
return

;----------------------Control Mode----------------------
;TODO -- Check the variables and set the appropriate radio elements as checked
fun_Set_Mode:
	SPosX := (A_ScreenWidth)/2 
	SPosY := (A_ScreenHeight)/2
	Gui, 3:Color, black
	Gui, 3:+ToolWindow +AlwaysOnTop -Caption
	Gui, 3:Font, S7 cwhite, Verdana
	Gui, 3:Add, Text, x6 y6 w140 h20 gMove, %st_Mode_Title%
	Gui, 3:Add, Text, x180 y6 w20 h20 gGui3Close, X
	Gui, 3:Add, Radio, x16 y25 gfun_ClockMode Checked, Clock mode.
	Gui, 3:Add, Radio, x106 y25 gfun_TimerMode, Timer mode.
	;Xeirwn -- When selecting timer mode, set the content to something
	Gui, 3:Add, GroupBox, x6 y55 w177 h75 , Clock
	Gui, 3:Add, Radio, x16 y75 gfun_ClockMode24Hour Checked, 24 Hour mode.
	Gui, 3:Add, Radio, x16 y105 gfun_ClockModeAMPM, AM/PM mode.
	Gui, 3:Add, GroupBox, x6 y133 w177 h200 Right, Timer
	;Gui, 3:Add, Radio, x16 y153 gfun_TimerModeToDateTime, Count to datetime.
	Gui, 3:Add, Radio, x16 y223 gfun_TimerModeFixedTime Checked, Count fixed time.
	;Gui, 3:Add, DateTime, x16 y173 wp20 h20 vToDateTime, d/M/yy HH:mm:ss
	Gui, 3:Add, DateTime, x16 y243 wp20 h20 vFixedTime, HH:mm:ss
	;TODO -- Set Fixed Time to some other value other than the current
	Gui, 3:Add, Button, w55 gfun_Timer_Continue, &Continue
	;Xeirwn -- When pressing many times the continue, it delays the execution as it resets it
	;http://www.autohotkey.com/docs/commands/SetTimer.htm
	Gui, 3:Add, Button, xp55 wp gfun_TimerPause, &Pause
	Gui, 3:Add, Button, xp55 wp gfun_TimerLap Default, &Lap
	Gui, 3:Add, Button, x16 yp30 w55 gfun_TimerReset, &Reset
	Gui, 3:Add, Button, xp60 w55 gfun_TimerZero, &Zero
	Gui, 3:Show, X%SPosX% Y%SPosY% W193 H345, %st_Mode_Title%
	WinSet, Region, 0-0 w193 h345 R10-10, st_Mode_Title
	WinSet, Transparent, 237, Watch 1.0 - Mode Settings
return

;-----------------Continue Timer-----------------
fun_Timer_Continue:
	Gui, Submit, NoHide
	SetTimer, fun_TicTac, on
return

;-----------------Pause Timer-----------------
fun_TimerPause:
	Gui, Submit, NoHide
	SetTimer, fun_TicTac, off
return

;-----------------Timer Lap-----------------
fun_TimerLap:
	Gui, Submit, NoHide
	var_Set_FixedTime = %FixedTime%
	IniWrite, %FixedTime%, %st_Configuration_File%, %st_Configuration_Section%, FixedTime
	SetTimer, fun_TicTac, on
	SetTimer, fun_TicTacToc, -1
return

;-----------------Reset Timer-----------------
fun_TimerReset:
	Gui, Submit, NoHide
	var_Set_FixedTime = %FixedTime%
	IniWrite, %FixedTime%, %st_Configuration_File%, %st_Configuration_Section%, FixedTime
	SetTimer, fun_TicTac, off
	SetTimer, fun_TicTacToc, -1
return

;-----------------Zero Timer-----------------
fun_TimerZero:
	Gui, Submit, NoHide
	var_Set_FixedTime = 19700101000000
	SetTimer, fun_TicTac, off
	SetTimer, fun_TicTacToc, -1
return

;-----------------Set Clock Mode-----------------
fun_ClockMode:
	Gui, Submit, NoHide
	var_Watch_Mode := "Clock"
	IniWrite, %var_Watch_Mode%, %st_Configuration_File%, %st_Configuration_Section%, %st_Watch_Mode%
	SetTimer, fun_TicTac, on
return

;-----------------Set Timer Mode-----------------
fun_TimerMode:
	Gui, Submit, NoHide
	var_Watch_Mode := "Timer"
	IniWrite, %var_Watch_Mode%, %st_Configuration_File%, %st_Configuration_Section%, %st_Watch_Mode%
	SetTimer, fun_TicTac, off
return

;-----------------Set AM/PM Clock Mode-----------------
fun_ClockModeAMPM:
	Gui, Submit, NoHide
	var_Clock_Mode := "AM/PM"
	IniWrite, %var_Clock_Mode%, %st_Configuration_File%, %st_Configuration_Section%, %st_Clock_Mode%
return

;-----------------Set 24Hour Clock Mode-----------------
fun_ClockMode24Hour:
	Gui, Submit, NoHide
	var_Clock_Mode := "24Hour"
	IniWrite, %var_Clock_Mode%, %st_Configuration_File%, %st_Configuration_Section%, %st_Clock_Mode%
return

;-----------------Set To DateTime Timer Mode-----------------
fun_TimerModeToDateTime:
	Gui, Submit, NoHide
	var_Timer_Mode := "ToDateTime"
	IniWrite, %var_Timer_Mode%, %st_Configuration_File%, %st_Configuration_Section%, %st_Timer_Mode%
return

;-----------------Set To Fixed Time Timer Mode-----------------
fun_TimerModeFixedTime:
	Gui, Submit, NoHide
	var_Timer_Mode := "FixedTime"
	IniWrite, %var_Timer_Mode%, %st_Configuration_File%, %st_Configuration_Section%, %st_Timer_Mode%
return

;----------------------Close Settings Window----------------------
Gui2Close:
	Gui, 2:Destroy
return

;----------------------Close Settings Window----------------------
Gui3Close:
	Gui, 3:Destroy
return

;-----------------Set Clock Background Color-----------------
fun_Set_Back_Color:
	Gui, Submit, NoHide
	var_Back_Color := RGB(dlg_color(var_Clock_Color, hwnd))
	if var_Back_Color <>
	{
		Gui, 1:Color, %var_Back_Color%
		GuiControl, 2:, Back_Color_Btn, %var_Back_Color%
		IniWrite, %var_Back_Color%, %st_Configuration_File%, %st_Configuration_Section%, %st_Back_Color%
	}
return

;-----------------------Set Clock Color----------------------
fun_Set_Clock_Color:
	Gui, Submit, NoHide
	var_Clock_Color := RGB(dlg_color(var_Clock_Color, hwnd))
	if var_Clock_Color <>
	{
		IniWrite, %var_Clock_Color%, %st_Configuration_File%, %st_Configuration_Section%, %st_Clock_Color%
		Gui, 1:Font, c%var_Clock_Color%
		GuiControl, 1:Font, Elem_Clock
		GuiControl, 2:, Clock_Color_Btn, %var_Clock_Color%
	}
return
;------------------------------------------------------------

;-----------------------Set Transparency----------------------
fun_Set_Transparency:
	Gui, Submit, NoHide
	var_Transparency := Trans_Value
	WinSet, Transparent, %var_Transparency%, elem_Watch_Window
	IniWrite, %var_Transparency%, %st_Configuration_File%, %st_Configuration_Section%, %st_Transparency%
return
;------------------------------------------------------------

;-----------------------Set AlwaysOnTop----------------------
fun_Set_Always_On_Top:
	if (var_Always_On_Top = "1")
	{
		WinSet, AlwaysOnTop, Off, elem_Watch_Window
	}
	else
	{
		WinSet, AlwaysOnTop, On, elem_Watch_Window
	}
	var_Always_On_Top := !var_Always_On_Top
	Menu, tray, togglecheck, &Always On Top
	IniWrite, %var_Always_On_Top%, %st_Configuration_File%, %st_Configuration_Section%, %st_Always_On_Top%
return
;------------------------------------------------------------

;-----------------------Exit Application----------------------
ExitApp:
	GoSub, fun_Save_Position
	ExitApp
return

;---------------------Save Clock Position--------------------
fun_Save_Position:
	WinGetPos, %var_Position_X%, %var_Position_Y%,,, elem_Watch_Window
	IniWrite, %var_Position_X%, %st_Configuration_File%, %st_Configuration_Section%, %st_Position_X%
	IniWrite, %var_Position_Y%, %st_Configuration_File%, %st_Configuration_Section%, %st_Position_Y%
return
;------------------------------------------------------------

;------------------Click Through Clock Window-----------------
fun_Click_Through:
	var_Click_Through_Clock := !var_Click_Through_Clock
	if (var_Click_Through_Clock == "1")
	{
		Gui, 1: +E0x20
	} else {
		Gui, 1: -E0x20
	}
	Menu, tray, togglecheck, Click &Through
	IniWrite, %var_Click_Through_Clock%, %st_Configuration_File%, %st_Configuration_Section%, %st_Click_Through_Clock%
return
;------------------------------------------------------------

;http://www.autohotkey.com/board/topic/94083-ahk-11-font-and-color-dialogs/
Dlg_Color(Color,hwnd){
	static
	if !cc{
		VarSetCapacity(CUSTOM,16*A_PtrSize,0),cc:=1,size:=VarSetCapacity(CHOOSECOLOR,9*A_PtrSize,0)
		Loop,16{
			IniRead,col,color.ini,color,%A_Index%,0
			NumPut(col,CUSTOM,(A_Index-1)*4,"UInt")
		}
	}
	NumPut(size,CHOOSECOLOR,0,"UInt"),NumPut(hwnd,CHOOSECOLOR,A_PtrSize,"UPtr")
	,NumPut(Color,CHOOSECOLOR,3*A_PtrSize,"UInt"),NumPut(3,CHOOSECOLOR,5*A_PtrSize,"UInt")
	,NumPut(&CUSTOM,CHOOSECOLOR,4*A_PtrSize,"UPtr")
	ret:=DllCall("comdlg32\ChooseColor","UPtr",&CHOOSECOLOR,"UInt")
	if !ret
	exit
	Loop,16
	IniWrite,% NumGet(custom,(A_Index-1)*4,"UInt"),color.ini,color,%A_Index%
	IniWrite,% Color:=NumGet(CHOOSECOLOR,3*A_PtrSize,"UInt"),color.ini,default,color
	return Color
}

rgb(c){
	setformat,IntegerFast,H
	c:=(c&255)<<16|(c&65280)|(c>>16),c:=SubStr(c,1)
	SetFormat,IntegerFast,D
	return c
}

fun_About:
	applicationName=Watch
	Gui,99:Destroy
	Gui,99:Margin,20,20
	Gui,99:Add,Picture,xm Icon1,%applicationName%.exe
	Gui,99:Font,Bold
	Gui,99:Add,Text,x+10 yp+10,%applicationName% v1.0
	Gui,99:Font
	Gui,99:Add,Text,y+10,This application will create a widget that will have the functionality to show the time or operate as a countdown timer

	Gui,99:Add,Picture,xm y+20 Icon5,%applicationName%.exe
	Gui,99:Font,Bold
	Gui,99:Add,Text,x+10 yp+10,Software by George Michael
	Gui,99:Font
	Gui,99:Add,Text,y+10,For more tools and information, please visit 
	Gui,99:Font,CBlue Underline
	Gui,99:Add,Text,y+5 GBYTEFREAKSNET,www.bytefreaks.net
	Gui,99:Font

	Gui,99:Show,,%applicationName% fun_About
	hCurs:=DllCall("LoadCursor","UInt",NULL,"Int",32649,"UInt") ;IDC_HAND
	OnMessage(0x200,"WM_MOUSEMOVE") 
	return

BYTEFREAKSNET:
	Run,http://www.bytefreaks.net,,UseErrorLevel
	return

Key features

  • You can change the ico that appears in the task bar by placing an ico file with the name watch.ico in the same directory as the executable (or the source)
  • You can drag the watch to a more convenient place
  • Double clicking on the watch it will hide it
  • Double clicking on the icon of the watch it will toggle it’s visibility
  • From the menu on the task bar you can “Set it on top”, allow mouse to click through the clock, hide/show the gui
  • You can set the color of the Font and the Color of the background using a color picker
  • You can set the transparency of the GUI
  • The tool has two modes, it can operate as a clock that shows time in 24 hour mode or AM/PM and can operate as a count down tool (timer)
  • The timer mode allows you to pause (and continue), reset and stop the time, reset and continue the time (using lap) or zero the clock