Copyright (c) Hyperion Entertainment and contributors.

Difference between revisions of "Date Functions"

From AmigaOS Documentation Wiki
Jump to navigation Jump to search
(Fixed to use struct TimeRequest instead of timerequest)
(Fixed Printf() statements in example code)
 
Line 43: Line 43:
 
#include <dos/datetime.h>
 
#include <dos/datetime.h>
 
#include <devices/timer.h>
 
#include <devices/timer.h>
  +
 
 
// Note these three libraries are opened by newlib startup code.
 
// Note these three libraries are opened by newlib startup code.
 
#include <proto/exec.h>
 
#include <proto/exec.h>
 
#include <proto/dos.h>
 
#include <proto/dos.h>
 
#include <proto/utility.h>
 
#include <proto/utility.h>
  +
 
 
#include <proto/timer.h>
 
#include <proto/timer.h>
  +
 
 
struct TimerIFace *ITimer = NULL;
 
struct TimerIFace *ITimer = NULL;
  +
 
 
int main()
 
int main()
 
{
 
{
 
struct MsgPort *mp = IExec->AllocSysObject(ASOT_PORT, NULL);
 
struct MsgPort *mp = IExec->AllocSysObject(ASOT_PORT, NULL);
  +
 
 
if (mp != NULL)
 
if (mp != NULL)
 
{
 
{
Line 63: Line 63:
 
ASOIOR_ReplyPort, mp,
 
ASOIOR_ReplyPort, mp,
 
TAG_END);
 
TAG_END);
  +
 
 
if (tr != NULL)
 
if (tr != NULL)
 
{
 
{
Line 70: Line 70:
 
struct Library *TimerBase = (struct Library *)tr->Request.io_Device;
 
struct Library *TimerBase = (struct Library *)tr->Request.io_Device;
 
ITimer = (struct TimerIFace*)IExec->GetInterface(TimerBase, "main", 1, NULL);
 
ITimer = (struct TimerIFace*)IExec->GetInterface(TimerBase, "main", 1, NULL);
  +
 
 
if (ITimer != NULL)
 
if (ITimer != NULL)
 
{
 
{
 
struct TimeVal tv;
 
struct TimeVal tv;
 
ITimer->GetSysTime(&tv);
 
ITimer->GetSysTime(&tv);
  +
 
IDOS->Printf("GetSysTime():\t%d %d\n", tv.Seconds, tv.Microseconds);
+
IDOS->Printf("GetSysTime():\t%lu %lu\n", tv.Seconds, tv.Microseconds);
  +
 
 
struct ClockData clockdata;
 
struct ClockData clockdata;
 
IUtility->Amiga2Date(tv.Seconds, &clockdata);
 
IUtility->Amiga2Date(tv.Seconds, &clockdata);
  +
 
IDOS->Printf("Amiga2Date(): sec %d min %d hour %d\n",
+
IDOS->Printf("Amiga2Date(): sec %lu min %lu hour %lu\n",
 
clockdata.sec, clockdata.min, clockdata.hour);
 
clockdata.sec, clockdata.min, clockdata.hour);
  +
 
IDOS->Printf(" mday %d month %d year %d wday %d\n",
+
IDOS->Printf(" mday %lu month %lu year %lu wday %lu\n",
 
clockdata.mday, clockdata.month, clockdata.year, clockdata.wday);
 
clockdata.mday, clockdata.month, clockdata.year, clockdata.wday);
  +
 
int32 seconds = IUtility->CheckDate(&clockdata);
+
uint32 seconds = IUtility->CheckDate(&clockdata);
  +
 
IDOS->Printf("CheckDate():\t%ld\n", seconds);
+
IDOS->Printf("CheckDate():\t%lu\n", seconds);
  +
 
 
seconds = IUtility->Date2Amiga(&clockdata);
 
seconds = IUtility->Date2Amiga(&clockdata);
  +
 
IDOS->Printf("Date2Amiga():\t%ld\n", seconds);
+
IDOS->Printf("Date2Amiga():\t%lu\n", seconds);
  +
 
IEXec->DropInterface((struct Interface*)ITimer);
+
IExec->DropInterface((struct Interface*)ITimer);
 
}
 
}
  +
 
 
IExec->CloseDevice((struct IORequest *)tr);
 
IExec->CloseDevice((struct IORequest *)tr);
 
}
 
}
  +
 
 
IExec->FreeSysObject(ASOT_IOREQUEST, tr);
 
IExec->FreeSysObject(ASOT_IOREQUEST, tr);
 
}
 
}
  +
 
 
IExec->FreeSysObject(ASOT_PORT, mp);
 
IExec->FreeSysObject(ASOT_PORT, mp);
 
}
 
}
  +
 
 
return RETURN_OK;
 
return RETURN_OK;
 
}
 
}

Latest revision as of 14:31, 22 May 2020

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 MsgPort *mp = IExec->AllocSysObject(ASOT_PORT, NULL);
 
  if (mp != NULL)
  {
    struct TimeRequest *tr = IExec->AllocSysObjectTags(ASOT_IOREQUEST,
      ASOIOR_Size, sizeof(struct TimeRequest),
      ASOIOR_ReplyPort, mp,
      TAG_END);
 
    if (tr != NULL)
    {
      if (!IExec->OpenDevice("timer.device", UNIT_VBLANK, (struct IORequest *)tr, 0))
      {
        struct Library *TimerBase = (struct Library *)tr->Request.io_Device;
        ITimer = (struct TimerIFace*)IExec->GetInterface(TimerBase, "main", 1, NULL);
 
        if (ITimer != NULL)
        {
          struct TimeVal tv;
          ITimer->GetSysTime(&tv);
 
          IDOS->Printf("GetSysTime():\t%lu %lu\n", tv.Seconds, tv.Microseconds);
 
          struct ClockData clockdata;
          IUtility->Amiga2Date(tv.Seconds, &clockdata);
 
          IDOS->Printf("Amiga2Date():  sec %lu min %lu hour %lu\n",
            clockdata.sec, clockdata.min, clockdata.hour);
 
          IDOS->Printf("               mday %lu month %lu year %lu wday %lu\n",
            clockdata.mday, clockdata.month, clockdata.year, clockdata.wday);
 
          uint32 seconds = IUtility->CheckDate(&clockdata);
 
          IDOS->Printf("CheckDate():\t%lu\n", seconds);
 
          seconds = IUtility->Date2Amiga(&clockdata);
 
          IDOS->Printf("Date2Amiga():\t%lu\n", seconds);
 
          IExec->DropInterface((struct Interface*)ITimer);
        }
 
        IExec->CloseDevice((struct IORequest *)tr);
      }
 
      IExec->FreeSysObject(ASOT_IOREQUEST, tr);
    }
 
    IExec->FreeSysObject(ASOT_PORT, mp);
  }
 
  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.