Fixed compatibility of output.
[AROS.git] / compiler / posixc / gettimeofday.c
blob641c735bdd8e98c7eaa32bbda282beeadb6fef24
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 <proto/exec.h>
9 #include <proto/dos.h>
10 #include <proto/timer.h>
11 #include <proto/locale.h>
12 #include <exec/types.h>
13 #include <devices/timer.h>
14 #include <aros/symbolsets.h>
15 #include <aros/debug.h>
17 #include <time.h>
18 #include <errno.h>
20 struct Device *TimerBase;
21 static void __init_timerbase(void);
23 /*****************************************************************************
25 NAME */
26 #include <sys/time.h>
28 int gettimeofday (
30 /* SYNOPSIS */
31 struct timeval * tv,
32 struct timezone * tz)
34 /* FUNCTION
35 Return the current time and/or timezone.
37 INPUTS
38 tv - If this pointer is non-NULL, the current time will be
39 stored here. The structure looks like this:
41 struct timeval
43 long tv_sec; // seconds
44 long tv_usec; // microseconds
47 tz - If this pointer is non-NULL, the current timezone will be
48 stored here. The structure looks like this:
50 struct timezone
52 int tz_minuteswest; // minutes west of Greenwich
53 int tz_dsttime; // type of dst correction
56 With daylight savings times defined as follows :
58 DST_NONE // not on dst
59 DST_USA // USA style dst
60 DST_AUST // Australian style dst
61 DST_WET // Western European dst
62 DST_MET // Middle European dst
63 DST_EET // Eastern European dst
64 DST_CAN // Canada
65 DST_GB // Great Britain and Eire
66 DST_RUM // Rumania
67 DST_TUR // Turkey
68 DST_AUSTALT // Australian style with shift in 1986
70 And the following macros are defined to operate on this :
72 timerisset(tv) - TRUE if tv contains a time
74 timercmp(tv1, tv2, cmp) - Return the result of the
75 comparison "tv1 cmp tv2"
77 timerclear(tv) - Clear the timeval struct
79 RESULT
80 The number of seconds.
82 NOTES
83 This function must not be used in a shared library or
84 in a threaded application.
86 EXAMPLE
87 struct timeval tv;
89 // Get the current time and print it
90 gettimeofday (&tv, NULL);
92 printf ("Seconds = %ld, uSec = %ld\n", tv->tv_sec, tv->tv_usec);
94 BUGS
96 SEE ALSO
97 stdc.library/ctime(), stdc.library/asctime(), stdc.library/localtime(),
98 stdc.library/time()
100 INTERNALS
102 ******************************************************************************/
104 if (!TimerBase)
105 __init_timerbase();
107 if (tv)
109 if (TimerBase)
111 GetSysTime(tv);
113 /* Adjust with the current timezone, stored in minutes west of GMT */
114 tv->tv_sec += (2922 * 1440 + __stdc_gmtoffset()) * 60;
116 else
118 errno = EACCES;
119 return -1;
123 if (tz)
125 tz->tz_minuteswest = __stdc_gmtoffset();
126 /* FIXME: set tz->tz_dsttime */
127 tz->tz_dsttime = DST_NONE;
130 return 0;
131 } /* gettimeofday */
134 static struct timerequest __timereq;
135 static struct MsgPort __timeport;
137 static void __init_timerbase(void)
139 __timeport.mp_Node.ln_Type = NT_MSGPORT;
140 __timeport.mp_Node.ln_Pri = 0;
141 __timeport.mp_Node.ln_Name = NULL;
142 __timeport.mp_Flags = PA_IGNORE;
143 __timeport.mp_SigTask = FindTask(NULL);
144 __timeport.mp_SigBit = 0;
145 NEWLIST(&__timeport.mp_MsgList);
147 __timereq.tr_node.io_Message.mn_Node.ln_Type = NT_MESSAGE;
148 __timereq.tr_node.io_Message.mn_Node.ln_Pri = 0;
149 __timereq.tr_node.io_Message.mn_Node.ln_Name = NULL;
150 __timereq.tr_node.io_Message.mn_ReplyPort = &__timeport;
151 __timereq.tr_node.io_Message.mn_Length = sizeof (__timereq);
155 OpenDevice
157 "timer.device",
158 UNIT_VBLANK,
159 (struct IORequest *)&__timereq,
166 TimerBase = (struct Device *)__timereq.tr_node.io_Device;
171 static void __exit_timerbase(APTR dummy)
173 if (TimerBase != NULL)
175 CloseDevice((struct IORequest *)&__timereq);
176 TimerBase = NULL;
180 ADD2EXIT(__exit_timerbase, 0);