Copyright (c) Hyperion Entertainment and contributors.
Difference between revisions of "Date Functions"
Steven Solie (talk | contribs) |
(Added missing message port allocation/freeing) |
||
Line 55: | Line 55: | ||
int main() |
int main() |
||
{ |
{ |
||
− | struct |
+ | struct MsgPort *mp = IExec->AllocSysObject(ASOT_PORT, NULL); |
− | ASOIOR_Size, sizeof(struct TimeRequest), |
||
− | ASOIOR_ReplyPort, NULL, |
||
− | TAG_END); |
||
− | if ( |
+ | if (mp != NULL) |
{ |
{ |
||
+ | struct timerequest *tr = IExec->AllocSysObjectTags(ASOT_IOREQUEST, |
||
− | if (!(IExec->OpenDevice("timer.device", UNIT_VBLANK, (struct IORequest *)tr, 0) )) |
||
+ | ASOIOR_Size, sizeof(struct TimeRequest), |
||
− | { |
||
+ | ASOIOR_ReplyPort, mp, |
||
− | struct Library *TimerBase = tr->tr_node.io_Device; |
||
+ | TAG_END); |
||
− | ITimer = (struct TimerIFace*)IExec->GetInterface(TimerBase, "main", 1, NULL); |
||
− | + | if (tr != NULL) |
|
+ | { |
||
+ | if (!IExec->OpenDevice("timer.device", UNIT_VBLANK, (struct IORequest *)tr, 0)) |
||
{ |
{ |
||
− | struct |
+ | struct Library *TimerBase = tr->tr_node.io_Device; |
− | ITimer-> |
+ | 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; |
||
− | IDOS->Printf("GetSysTime():\t%d %d\n", tv.Seconds, tv.Microseconds); |
||
+ | IUtility->Amiga2Date(tv.Seconds, &clockdata); |
||
+ | IDOS->Printf("Amiga2Date(): sec %d min %d hour %d\n", |
||
− | struct ClockData clockdata; |
||
− | + | clockdata.sec, clockdata.min, clockdata.hour); |
|
− | IDOS->Printf(" |
+ | IDOS->Printf(" mday %d month %d year %d wday %d\n", |
− | clockdata. |
+ | clockdata.mday, clockdata.month, clockdata.year, clockdata.wday); |
+ | int32 seconds = IUtility->CheckDate(&clockdata); |
||
− | IDOS->Printf(" mday %d month %d year %d wday %d\n", |
||
− | clockdata.mday, clockdata.month, clockdata.year, clockdata.wday); |
||
− | + | IDOS->Printf("CheckDate():\t%ld\n", seconds); |
|
+ | seconds = IUtility->Date2Amiga(&clockdata); |
||
− | IDOS->Printf("CheckDate():\t%ld\n", seconds); |
||
− | + | IDOS->Printf("Date2Amiga():\t%ld\n", seconds); |
|
− | + | IEXec->DropInterface((struct Interface*)ITimer); |
|
+ | } |
||
− | + | IExec->CloseDevice((struct IORequest *)tr); |
|
} |
} |
||
− | IExec-> |
+ | IExec->FreeSysObject(ASOT_IOREQUEST, tr); |
} |
} |
||
− | IExec->FreeSysObject( |
+ | IExec->FreeSysObject(ASOT_PORT, mp); |
} |
} |
||
Revision as of 22:03, 17 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:
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 = 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); } 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. |