Missing dependencies added.
[AROS-Contrib.git] / rexx / lstring / time.c
blob3c8bd8e6ab30c14a9de6cddac517665d2c768bdf
1 /*
2 * $Header$
3 * $Log$
4 * Revision 1.1 2001/04/04 05:43:38 wang
5 * First commit: compiles on Linux, Amiga, Windows, Windows CE, generic gcc
7 * Revision 1.5 1999/11/26 12:52:25 bnv
8 * Added: Windows CE support
10 * Revision 1.4 1999/03/10 16:55:55 bnv
11 * Added MSC support
13 * Revision 1.3 1999/03/01 11:06:33 bnv
14 * Change in the format to long int for tv.tv_usec just to keep compiler happy
16 * Revision 1.2 1998/11/06 08:57:08 bnv
17 * Added: in the includes the time.h and sys/time.h
19 * Revision 1.1 1998/07/02 17:18:00 bnv
20 * Initial Version
24 #if defined(MSDOS) && !defined(__WIN32__) && !defined(_MSC_VER) && !defined(WCE)
25 # include <dos.h>
26 # include <time.h>
27 #elif defined(WCE)
28 #elif defined(_MSC_VER)
29 # include <time.h>
30 # include <sys/types.h>
31 # include <sys/timeb.h>
32 #else
33 /* Load both of them time.h and sys/time.h, you never now where the
34 * struct timeval is
36 # include <time.h>
37 # include <sys/time.h>
38 # include <unistd.h>
39 #endif
40 #include <lerror.h>
41 #include <lstring.h>
43 static double elapsed=0.0;
45 /* ------------------ _Ltimeinit ----------------- */
46 void
47 _Ltimeinit( void )
49 #if defined(MSDOS) && !defined(__WIN32__) && !defined(_MSC_VER)
50 struct time t;
51 #elif defined(WCE)
52 /* nothing to declare */
53 #elif defined(_MSC_VER)
54 struct _timeb tb;
55 #else
56 struct timeval tv;
57 struct timezone tz;
58 #endif
60 #ifdef WCE
61 elapsed = (double)GetTickCount() / 1000.0;
62 #elif defined(MSDOS) && !defined(__WIN32__) && !defined(_MSC_VER)
63 gettime(&t);
64 elapsed = (double)t.ti_hour*3600 + (double)t.ti_min*60 +
65 (double)t.ti_sec + (double)t.ti_hund/100.0;
66 #elif defined(_MSC_VER)
67 _ftime(&tb);
68 elapsed = (double)tb.time + (double)tb.millitm/1000.0;
69 #else
70 gettimeofday(&tv,&tz);
71 elapsed = tv.tv_sec + (double)tv.tv_usec/1000000.0;
72 #endif
73 } /* _Ltimeinit */
75 /* -------------------- Ltime ---------------------- */
76 void
77 Ltime( const PLstr timestr, char option )
79 double unow;
80 int hour;
81 #ifdef WCE
82 SYSTEMTIME time;
83 TCHAR buf[30];
84 TCHAR *ampm;
85 #else
86 time_t now;
87 char *ampm;
88 struct tm *tmdata ;
89 # if defined(MSDOS) && !defined(__WIN32__) && !defined(_MSC_VER)
90 struct time t;
91 # elif defined(_MSC_VER)
92 struct _timeb tb;
93 # else
94 struct timeval tv;
95 struct timezone tz;
96 # endif
97 #endif
99 option = l2u[(byte)option];
100 Lfx(timestr,30); LZEROSTR(*timestr);
102 #ifndef WCE
103 now = time(NULL);
104 tmdata = localtime(&now) ;
105 #else
106 GetLocalTime(&time);
107 #endif
109 switch (option) {
110 case 'C':
111 #ifndef WCE
112 hour = tmdata->tm_hour ;
113 ampm = (hour>11) ? "pm" : "am" ;
114 if ((hour=hour%12)==0) hour = 12 ;
115 sprintf(LSTR(*timestr),"%d:%02d%s",
116 hour, tmdata->tm_min, ampm) ;
117 #else
118 hour = time.wHour ;
119 ampm = (hour>11) ? TEXT("pm") : TEXT("am");
120 if ((hour=hour%12)==0) hour = 12 ;
121 swprintf(buf,TEXT("%d:%02d%s"),
122 hour, time.wMinute, ampm) ;
123 #endif
124 break;
126 case 'E':
127 #ifdef WCE
128 unow = (double)GetTickCount() / 1000.0;
129 #elif defined(MSDOS) && !defined(__WIN32__) && !defined(_MSC_VER)
130 gettime(&t);
131 unow = (double)t.ti_hour*3600 + (double)t.ti_min*60 +
132 (double)t.ti_sec + (double)t.ti_hund/100.0;
133 #elif defined(_MSC_VER)
134 _ftime(&tb);
135 unow = (double)tb.time + (double)tb.millitm/1000.0;
136 #else
137 gettimeofday(&tv,&tz);
138 unow = (double)tv.tv_sec + (double)tv.tv_usec/1000000.0;
139 #endif
140 LREAL(*timestr) = unow-elapsed;
141 LTYPE(*timestr) = LREAL_TY;
142 LLEN(*timestr) = sizeof(double);
143 return;
145 case 'H':
146 #ifndef WCE
147 sprintf(LSTR(*timestr), "%d", tmdata->tm_hour) ;
148 #else
149 swprintf(buf, TEXT("%d"), time.wHour) ;
150 #endif
151 break;
153 case 'L':
154 #ifdef WCE
155 swprintf(buf, TEXT("%02d:%02d:%02d.%03d"), time.wHour,
156 time.wMinute, time.wSecond, time.wMilliseconds) ;
157 #elif defined(MSDOS) && !defined(__WIN32__) && !defined(_MSC_VER)
158 gettime(&t);
159 sprintf(LSTR(*timestr), "%02d:%02d:%02d.%02d",
160 t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund);
161 #elif defined(_MSC_VER)
162 _ftime(&tb);
163 sprintf(LSTR(*timestr), "%02d:%02d:%02d.%03ld",
164 tmdata->tm_hour, tmdata->tm_min,
165 tmdata->tm_sec, tb.millitm) ;
166 #else
167 gettimeofday(&tv,&tz);
168 sprintf(LSTR(*timestr), "%02d:%02d:%02d.%06ld",
169 tmdata->tm_hour, tmdata->tm_min,
170 tmdata->tm_sec, (long)tv.tv_usec) ;
171 #endif
172 break;
174 case 'M':
175 #ifndef WCE
176 sprintf(LSTR(*timestr), "%d",
177 tmdata->tm_hour*60 + tmdata->tm_min) ;
178 #else
179 swprintf(buf, TEXT("%d"),
180 time.wHour*60 + time.wMinute);
181 #endif
182 break;
184 case 'N':
185 #ifndef WCE
186 sprintf(LSTR(*timestr), "%02d:%02d:%02d",
187 tmdata->tm_hour, tmdata->tm_min,
188 tmdata->tm_sec ) ;
189 #else
190 swprintf(buf, TEXT("%02d:%02d:%02d"),
191 time.wHour, time.wMinute, time.wSecond);
192 #endif
193 break;
195 case 'R':
196 #ifdef WCE
197 unow = (double)GetTickCount() / 1000.0;
198 #elif defined(MSDOS) && !defined(__WIN32__) && !defined(_MSC_VER)
199 gettime(&t);
200 unow = (double)t.ti_hour*3600 + (double)t.ti_min*60 +
201 (double)t.ti_sec + (double)t.ti_hund/100.0;
202 #elif defined(_MSC_VER)
203 _ftime(&tb);
204 unow = (double)tb.time + (double)tb.millitm/1000.0;
205 #else
206 gettimeofday(&tv,&tz);
207 unow = (double)tv.tv_sec + (double)tv.tv_usec/1000000.0;
208 #endif
209 LREAL(*timestr) = unow-elapsed;
210 elapsed = unow;
211 LTYPE(*timestr) = LREAL_TY;
212 LLEN(*timestr) = sizeof(double);
213 return;
215 case 'S':
216 #ifndef WCE
217 sprintf(LSTR(*timestr), "%ld",
218 (long)((long)(tmdata->tm_hour*60L)+tmdata->tm_min)
219 *60L + (long)tmdata->tm_sec) ;
220 #else
221 swprintf(buf, TEXT("%ld"),
222 (long)((long)(time.wHour*60L)+time.wMinute)
223 *60L + (long)time.wSecond) ;
224 #endif
225 break;
227 default:
228 Lerror(ERR_INCORRECT_CALL,0);
230 #ifndef WCE
231 LLEN(*timestr) = STRLEN(LSTR(*timestr));
232 #else
233 wcstombs(LSTR(*timestr), buf, LLEN(*timestr)=wcslen(buf));
234 #endif
235 } /* Ltime */