/*  Small clock  */

/*  When you start clock the WB screen should be frontmost  */

/*  Initially try and draw over the title bar using the screens  */
/*  RastPort & Intuitext  */

#include <stdio.h>

#include <exec/types.h>

#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>

#include <clib/intuition_protos.h>
#include <clib/dos_protos.h>

extern struct IntuitionBase *IntuitionBase;


/*  CBack Data  */
extern BPTR _Backstdout;
long __stack = 4096;              /* Amount of stack space our task needs */
char *__procname = "Clock";       /* The name of the task to create       */
long __priority = -40;            /* The priority to run the task at    */
long __BackGroundIO = 1;          /* Flag indicating we want to send I/O to the
                                     original shell.  We will print a banner.
                                     NOTE:  This variable may also be called
                                     _BackGroundIO.  Notice the single
                                     underscore.                       */
char time[20];

struct IntuiText Clock=
	{
	0, 1,
	JAM2,
	0, 0,
	NULL,
	time,
	NULL
	};

void main( int argc, char *argv[] )
	{
	struct Screen *Screen;
	LONG year, day, hour, minute, second, micro;
	LONG year2, day2, hour2;
	
    if (_Backstdout)
      Close(_Backstdout);

    _Backstdout = 0;
		
	Screen = IntuitionBase->FirstScreen;
	
	for( ;; )
		{
		/*  Get the time  */
		CurrentTime( &second, &micro );
	
		/*  This stupid set of algorithms is required to extract the  */
		/*  time in human readable form  */
		micro = second;
		year = micro / (31536000);
		year2 = year * 31536000;
		day = (micro - year2) / 86400;
		day2 = day * 86400;
		hour = (micro - year2 - day2) / 3600;
		hour2 = hour * 3600;
		minute = (micro - year2 - day2 - hour2) / 60;
		second = micro % 60;
		
		/*  12 hour clock please  */
		if( hour > 12 )
			hour -= 12;
			
		if( hour == 0 )
			hour = 12;

		sprintf( time, "%2d:%2d:%2d", hour, minute, second );
	
		PrintIText( &(Screen->RastPort), &Clock, 550, 1 );
		
		/*  Allow some time for processing the algorithm */
		Delay( 45 );
		}
	}
