About

The program bellow retrieves the current time from timer.device and displays the current time in the format HH:mm:ss.milliseconds as well as the seconds to the hour.

Code

SecondsToHour.c
/*************************************************************************/
/*    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
/*                                                                       */
/*  SecondsToHour                                                        */
/*                                                                       */
/*  Displays the current time and the number of seconds to the hour.     */
/*                                                                       */
/*  Compile using SASC: sc link SecondsToHour.c                          */
/*                                                                       */
/*************************************************************************/
 
#include <exec/types.h>
#include <exec/io.h>
#include <exec/memory.h>
#include <devices/timer.h>
 
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>
#include <clib/dos_protos.h>
#include <clib/intuition_protos.h>
 
#include <stdio.h>
 
///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    //
///////////////////////////////////////////////////////////////////////////
int wasMillisecondsToClock(int min, int sec, int mil) {
    return (
        3600000 - (
            sec % 60
        ) * 1000 - (
            min % 60
        ) * 60 * 1000 - mil
    ) % 3600000;
}
 
int main(void) {
    struct  timerequest *TimerIO;
    struct  MsgPort     *TimerMP;
    struct  Message     *TimerMSG;
 
    /* days, hours, seconds, minutes, milliseconds */
    ULONG days, hrs, secs, mins, mico;
 
    /* Create port. */
    if (!(TimerMP = CreatePort(0, 0))) {
        printf("error: could not create port.\n");
        return 1;
    }
 
    /* Create IO port */
    if (!(TimerIO = (struct timerequest*)CreateExtIO(TimerMP, sizeof(struct timerequest)))) {
        printf("error: could not create timer IO port.\n");
        return 1;
    }
 
    /* Open timer device. */
    if (OpenDevice(TIMERNAME, UNIT_VBLANK, (struct IORequest*)TimerIO, 0L)) {
        printf("error: could not open device.\n");
        return 1;
    }
 
    /* Request current system time from timer. */
    TimerIO->tr_node.io_Command = TR_GETSYSTIME;
    /* Send the request. */
    SendIO((struct IORequest*)TimerIO);
    /* Wait for request to arrive. */
    WaitPort(TimerMP);
    TimerMSG = GetMsg(TimerMP);
    if (!(TimerMSG == (struct Message*)TimerIO)) {
        printf("error: could not get system time.\n");
        return 1;
    }
 
    /* Get microseconds and seconds from timer. */
    mico = TimerIO->tr_time.tv_micro;
    secs = TimerIO->tr_time.tv_secs;
 
    /* Compute days, hours, minutes, seconds since Jan.1,1978 */
    mins = secs / 60;
    hrs = mins / 60;
    days = hrs / 24;
    secs = secs % 60;
    mins = mins % 60;
    hrs = hrs % 24;
 
    /* Display the current time. */
    printf(
        "The current time is %d:%d:%d.%d\n", 
        hrs, 
        mins, 
        secs, 
        mico / 1000
    );
 
    /* Display the seconds to the hour. */
    printf(
        "There are %d seconds to the hour.\n", 
        wasMillisecondsToClock(
            mins, 
            secs, 
            mico / 1000
        ) / 1000
    );
 
    /* Cleanup. */
    CloseDevice((struct IORequest*)TimerIO);
    DeleteExtIO((struct IORequest*)TimerIO);
    DeletePort(TimerMP);
}