Copyright (c) Hyperion Entertainment and contributors.

Date Functions

From AmigaOS Documentation Wiki
Revision as of 22:24, 26 March 2014 by Steven Solie (talk | contribs)
Jump to navigation Jump to search

Date Functions

To ease date-related calculations, the utility library has some functions to convert a date, specified in a ClockData structure, in the number of seconds since 00:00:00 01-Jan-78 and vice versa.

To indicate the date, the ClockData structure (in <utility/date.h>) is used.

struct ClockData
{
    UWORD sec;     /* seconds (0 - 59) */
    UWORD min;     /* minutes (0 - 59) */
    UWORD hour;    /* hour (0 - 23) */
    UWORD mday;    /* day of the month (1 - 31) */
    UWORD month;   /* month of the year (1 - 12) */
    UWORD year;    /* 1978 - */
    UWORD wday;    /* day of the week (0 - 6, where 0 is Sunday) */
};

The following functions are available to operate on ClockData:

Utility Library Date Functions
Amiga2Date() Calculate the date from the specified timestamp (in seconds).
CheckDate() Check the legality of a date.
Date2Amiga() Calculate the timestamp from the specified date.

Amiga2Date() takes a number of seconds from 01-Jan-78 as argument and fills in the supplied ClockData structure with the date and time.

CheckDate() checks if the supplied ClockData structure is valid, and returns the number of seconds from 01-Jan-78 if it is. Note that this function currently does not take the supplied day of the week in account.

Date2Amiga() takes a ClockData structure as argument and returns the number of seconds since 01-Jan-78. The supplied ClockData structure must be valid, since no checking is done.

The following example shows various uses of the utility library date functions.

// a2d.c
#include <exec/types.h>
#include <exec/memory.h>
#include <dos/datetime.h>
#include <devices/timer.h>
 
// Note these three libraries are opened by newlib startup code.
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/utility.h>
 
#include <proto/timer.h>
 
struct TimerIFace *ITimer = NULL;
 
int main()
{
  struct timerequest *tr = IExec->AllocSysObjectTags(ASOT_IOREQUEST,
    ASOIOR_Size, sizeof(struct TimeRequest),
    ASOIOR_ReplyPort, NULL,
    TAG_END);
 
  if (tr != NULL)
  {
    if (!(IExec->OpenDevice("timer.device", UNIT_VBLANK, (struct IORequest *)tr, 0) ))
    {
      struct Library *TimerBase = tr->tr_node.io_Device;
      ITimer = (struct TimerIFace*)IExec->GetInterface(TimerBase, "main", 1, NULL);
 
      if (ITimer != NULL)
      {
        struct TimeVal tv;
        ITimer->GetSysTime(&tv);
 
        IDOS->Printf("GetSysTime():\t%d %d\n", tv.Seconds, tv.Microseconds);
 
        struct ClockData clockdata;
        IUtility->Amiga2Date(tv.Seconds, &clockdata);
 
        IDOS->Printf("Amiga2Date():  sec %d min %d hour %d\n",
          clockdata.sec, clockdata.min, clockdata.hour);
 
        IDOS->Printf("               mday %d month %d year %d wday %d\n",
          clockdata.mday, clockdata.month, clockdata.year, clockdata.wday);
 
        int32 seconds = IUtility->CheckDate(&clockdata);
 
        IDOS->Printf("CheckDate():\t%ld\n", seconds);
 
        seconds = IUtility->Date2Amiga(&clockdata);
 
        IDOS->Printf("Date2Amiga():\t%ld\n", seconds);
 
        IEXec->DropInterface((struct Interface*)ITimer);
      }
 
      IExec->CloseDevice((struct IORequest *)tr);
    }
 
    IExec->FreeSysObject(ASOT_IOREQUEST, tr);
  }
 
  return RETURN_OK;
}

Date Function Reference

The following are brief descriptions of the utility library functions which pertain to date conversion. See the SDK for details on each function call.

Function Description
CheckDate() Check the legality of a date.
Amiga2Date() Calculate the date from a specified timestamp.
Date2Amiga() Calculate the timestamp from a specified date.