Copyright (c) Hyperion Entertainment and contributors.

BattClock Resource

From AmigaOS Documentation Wiki
Jump to navigation Jump to search


The battery-backed clock (BattClock) keeps Amiga time while the system is powered off. The time from the BattClock is loaded into the Amiga system clock as part of the boot sequence.

The BattClock Resource component provides access to the BattClock. Three functions allow you to read the BattClock value, reset it and set it to a value you desire.

The Utility Library contains time functions which convert the number of seconds since 12:00 AM, January 1, 1978 to a date and time we can understand, and vice versa. You will find these functions useful when dealing with the BattClock. The example program below uses the Amiga2Date() utility function to convert the value returned by ReadBattClock(). See the Utility Library page for a discussion of the scope of the library, and the autodocs for a listing of its functions.

So, you want to be a Time Lord?
This resource will allow you to set the BattClock to any value you desire. Keep in mind that this time will endure a reboot and could adversely affect your system!


#include <exec/types.h>
#include <dos/dos.h>
#include <utility/date.h>
#include <resources/battclock.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/battclock.h>
#include <proto/utility.h>
int main()
    STRPTR ampm;
    uint32 AmigaTime;
    struct ClockData MyClock;
    CONST CONST_STRPTR Days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    CONST CONST_STRPTR Months[] = {"January", "February", "March", "April", "May", "June", "July", "August", 
    "September", "October", "November", "December"};
    struct Library *BattClockBase = IExec->OpenResource(BATTCLOCKNAME);
    struct BattClockIFace *IBattClock = (struct BattClockIFace *) IExec->GetInterface(BattClockBase, "main", 1, NULL);
    if (IBattClock != NULL) {
        /* Get number of seconds till now */
        AmigaTime = IBattClock->ReadBattClock();
        /* Convert to a ClockData structure */
        IUtility->Amiga2Date(AmigaTime, &MyClock);
        IDOS->Printf("\nRobin, tell everyone the BatDate and BatTime");
        /* Print the Date */
        IDOS->Printf("\n\nOkay Batman, the BatDate is ");
        IDOS->Printf("%s, %s %ld, %ld", Days[MyClock.wday], Months[MyClock.month-1], MyClock.mday, MyClock.year);
        /* Convert military time to normal time and set AM/PM */
        if (MyClock.hour < 12) {
            ampm = "AM";        /* hour less than 12, must be morning */
        else {
            ampm = "PM";         /* hour greater than 12,must be night */
            MyClock.hour -= 12;  /* subtract the extra 12 of military */
        if (MyClock.hour == 0) {
                MyClock.hour = 12;   /* don't forget the 12s */
        /* Print the time */
        IDOS->Printf("\n             the BatTime is ");
        IDOS->Printf("%02ld:%02ld:%02ld %s\n\n", MyClock.hour, MyClock.min, MyClock.sec, ampm);
        IExec->DropInterface((struct Interface*) IBattClock);
       IDOS->Printf("Error: Unable to open the %s\n", BATTCLOCKNAME);
    return 0;

Additional programming information on the BattClock Resource can be found in the respective include files and in the BattClock Resource and Utility Library autodocs.

Function reference

ReadBattClock() Read the time from the BattClock. Returns the time as the number of seconds since 12:00 AM, January 1, 1978.
ResetBattClock() Reset the BattClock to 12:00 AM, January 1, 1978.
WriteBattClock() Set the BattClock to the number of seconds you pass it relative to 12:00 AM, January 1, 1978.