Removed double NAME entry.
[AROS.git] / compiler / clib / gettimeofday.c
blob5824da28fd9e477597e6b9c3ffe3003772379a3b
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 Query the current time and/or timezone.
6 */
8 #include "__arosc_privdata.h"
10 #include <proto/exec.h>
11 #include <proto/dos.h>
12 #include <proto/timer.h>
13 #include <proto/locale.h>
14 #include <exec/types.h>
15 #include <devices/timer.h>
16 #include <aros/symbolsets.h>
17 #include <aros/debug.h>
19 #include <time.h>
20 #include <errno.h>
22 struct Device *TimerBase;
23 static void __init_timerbase(void);
25 /*****************************************************************************
27 NAME */
28 #include <sys/time.h>
29 #include <unistd.h>
31 int gettimeofday (
33 /* SYNOPSIS */
34 struct timeval * tv,
35 struct timezone * tz)
37 /* FUNCTION
38 Return the current time and/or timezone.
40 INPUTS
41 tv - If this pointer is non-NULL, the current time will be
42 stored here. The structure looks like this:
44 struct timeval
46 long tv_sec; // seconds
47 long tv_usec; // microseconds
50 tz - If this pointer is non-NULL, the current timezone will be
51 stored here. The structure looks like this:
53 struct timezone
55 int tz_minuteswest; // minutes west of Greenwich
56 int tz_dsttime; // type of dst correction
59 With daylight savings times defined as follows :
61 DST_NONE // not on dst
62 DST_USA // USA style dst
63 DST_AUST // Australian style dst
64 DST_WET // Western European dst
65 DST_MET // Middle European dst
66 DST_EET // Eastern European dst
67 DST_CAN // Canada
68 DST_GB // Great Britain and Eire
69 DST_RUM // Rumania
70 DST_TUR // Turkey
71 DST_AUSTALT // Australian style with shift in 1986
73 And the following macros are defined to operate on this :
75 timerisset(tv) - TRUE if tv contains a time
77 timercmp(tv1, tv2, cmp) - Return the result of the
78 comparison "tv1 cmp tv2"
80 timerclear(tv) - Clear the timeval struct
82 RESULT
83 The number of seconds.
85 NOTES
86 This function must not be used in a shared library or
87 in a threaded application.
89 EXAMPLE
90 struct timeval tv;
92 // Get the current time and print it
93 gettimeofday (&tv, NULL);
95 printf ("Seconds = %ld, uSec = %ld\n", tv->tv_sec, tv->tv_usec);
97 BUGS
99 SEE ALSO
100 ctime(), asctime(), localtime(), time()
102 INTERNALS
104 ******************************************************************************/
106 if (!TimerBase)
107 __init_timerbase();
109 if (tv)
111 if (TimerBase)
113 GetSysTime(tv);
115 /* Adjust with the current timezone, stored in minutes west of GMT */
116 tv->tv_sec += (2922 * 1440 + __arosc_gmtoffset()) * 60;
118 else
120 errno = EACCES;
121 return -1;
125 if (tz)
127 tz->tz_minuteswest = __arosc_gmtoffset();
128 /* FIXME: set tz->tz_dsttime */
129 tz->tz_dsttime = DST_NONE;
132 return 0;
133 } /* gettimeofday */
136 static struct timerequest __timereq;
137 static struct MsgPort __timeport;
139 static void __init_timerbase(void)
141 __timeport.mp_Node.ln_Type = NT_MSGPORT;
142 __timeport.mp_Node.ln_Pri = 0;
143 __timeport.mp_Node.ln_Name = NULL;
144 __timeport.mp_Flags = PA_IGNORE;
145 __timeport.mp_SigTask = FindTask(NULL);
146 __timeport.mp_SigBit = 0;
147 NEWLIST(&__timeport.mp_MsgList);
149 __timereq.tr_node.io_Message.mn_Node.ln_Type = NT_MESSAGE;
150 __timereq.tr_node.io_Message.mn_Node.ln_Pri = 0;
151 __timereq.tr_node.io_Message.mn_Node.ln_Name = NULL;
152 __timereq.tr_node.io_Message.mn_ReplyPort = &__timeport;
153 __timereq.tr_node.io_Message.mn_Length = sizeof (__timereq);
157 OpenDevice
159 "timer.device",
160 UNIT_VBLANK,
161 (struct IORequest *)&__timereq,
168 TimerBase = (struct Device *)__timereq.tr_node.io_Device;
173 static void __exit_timerbase(APTR dummy)
175 if (TimerBase != NULL)
176 CloseDevice((struct IORequest *)&__timereq);
179 ADD2EXIT(__exit_timerbase, 0);