New bitmap method SetRGBConversionFunction which can be used to
[tangerine.git] / workbench / c / Date.c
blobde38029c71b82b7875595f6b80febf75378c19e8
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Date CLI command
6 Lang: English
7 */
8 /******************************************************************************
10 NAME
12 Date [<day>] [<date>] [<time>] [TO | VER <filename>]
14 SYNOPSIS
16 DAY,DATE,TIME,TO=VER/K
18 LOCATION
20 Workbench:C
22 FUNCTION
24 Displays or sets the system date and/or time.
26 INPUTS
28 DAY -- sets date by name (monday, tuesday, ... , tomorrow, yesterday)
29 DATE -- sets date in format DD-MMM-YY.
30 For MMM either the number or the first 3 letters of the
31 month in English
32 TIME -- sets time in format HH:MM:SS
33 TO -- output is sent to file
35 RESULT
37 NOTES
39 EXAMPLE
41 date 2-feb-06
42 date 21:10
44 BUGS
46 SEE ALSO
48 INTERNALS
50 HISTORY
52 ******************************************************************************/
54 #include <stdio.h>
55 #include <proto/exec.h>
56 #include <exec/errors.h>
57 #include <exec/io.h>
58 #include <proto/dos.h>
59 #include <dos/dos.h>
60 #include <dos/datetime.h>
61 #include <devices/timer.h>
63 #include <string.h>
64 #include <stdlib.h>
66 static const char version[] = "$VER: Date 41.4 (5.3.2000)\n";
68 #define ARG_STRING "DAY,DATE,TIME,TO=VER/K"
69 #define ARG_DAY 0
70 #define ARG_DATE 1
71 #define ARG_TIME 2
72 #define ARG_VER 3
73 #define ARG_COUNT 4
75 static WORD chrcount(STRPTR s, UBYTE c)
77 UBYTE sc;
78 WORD retval = 0;
80 while((sc = *s++))
82 if (sc == c) retval++;
85 return retval;
88 int setdate(STRPTR *day_date_time)
90 int error = RETURN_OK;
91 BYTE timererror;
92 struct timerequest *timerReq;
93 struct MsgPort *timerMP;
94 struct DateTime dt;
95 WORD i, count;
96 UBYTE fulltime[9];
97 STRPTR realtime = NULL, realdate = NULL;
99 for(i = 0; i < 3; i++)
101 if (day_date_time[i] == NULL) continue;
103 if (chrcount(day_date_time[i], '-'))
105 /* must be date */
107 realdate = day_date_time[i];
109 else if ((count = chrcount(day_date_time[i], ':')))
111 /* must be time */
112 if (count == 1)
114 /* seconds are missing */
116 if (strlen(day_date_time[i]) <= 5)
118 strcpy(fulltime, day_date_time[i]);
119 strcat(fulltime, ":00");
120 realtime = fulltime;
122 else
124 realtime = day_date_time[i];
127 else
129 realtime = day_date_time[i];
132 else
134 /* must be week day name */
136 if (!realdate) realdate = day_date_time[i];
141 timerMP = CreateMsgPort();
142 if (timerMP)
144 timerReq = (struct timerequest *)CreateIORequest(timerMP, sizeof(struct timerequest));
146 if (timerReq)
148 timererror = OpenDevice(TIMERNAME, UNIT_VBLANK,
149 &timerReq->tr_node, 0L);
150 if(timererror == 0)
152 dt.dat_Format = FORMAT_DOS;
153 dt.dat_Flags = DTF_FUTURE;
154 dt.dat_StrDay = NULL; /* StrToDate ignores this anyway */
155 dt.dat_StrDate = realdate;
156 dt.dat_StrTime = realtime;
158 DateStamp(&dt.dat_Stamp);
160 if((!realdate && !realtime) || (StrToDate(&dt) == 0))
162 PutStr("***Bad args:\n"
163 "- use DD-MMM-YY or <dayname> or yesterday etc. to set date\n"
164 " HH:MM:SS or HH:MM to set time\n");
165 error = RETURN_FAIL;
166 } else {
168 timerReq->tr_time.tv_secs = dt.dat_Stamp.ds_Days*60*60*24 +
169 dt.dat_Stamp.ds_Minute*60 +
170 dt.dat_Stamp.ds_Tick / TICKS_PER_SECOND;
171 timerReq->tr_time.tv_micro = 0;
172 timerReq->tr_node.io_Command = TR_SETSYSTIME;
173 timerReq->tr_node.io_Flags |= IOF_QUICK;
175 DoIO(&timerReq->tr_node);
178 CloseDevice(&timerReq->tr_node);
180 else
182 PutStr("Date: Error opening timer.device\n");
183 error = RETURN_FAIL;
185 DeleteIORequest(&timerReq->tr_node);
187 else
189 PutStr("Date: Error creating timerequest\n");
190 error = RETURN_FAIL;
192 DeleteMsgPort(timerMP);
194 else
196 PutStr("Date: Error creating MsgPort\n");
197 error = RETURN_FAIL;
200 return error;
204 int printdate(STRPTR filename)
206 BPTR file = Output();
207 int ownfile = 0;
208 int error = RETURN_OK;
209 struct DateTime dt;
210 char dowstring[LEN_DATSTRING * 2], datestring[LEN_DATSTRING * 2],
211 timestring[LEN_DATSTRING * 2], resstring[LEN_DATSTRING*6+1];
213 if(filename != NULL)
215 file = Open(filename, MODE_NEWFILE);
216 ownfile = 1;
219 if(file != (BPTR)NULL)
221 int pos = 0;
223 DateStamp(&dt.dat_Stamp);
225 dt.dat_Format = FORMAT_DEF;
226 dt.dat_Flags = 0;
227 dt.dat_StrDay = dowstring;
228 dt.dat_StrDate = datestring;
229 dt.dat_StrTime = timestring;
230 DateToStr(&dt);
232 CopyMem(dowstring, resstring, strlen(dowstring));
233 pos += strlen(dowstring);
234 resstring[pos++] = ' ';
235 CopyMem(datestring, resstring + pos, strlen(datestring));
236 pos += strlen(datestring);
237 resstring[pos++] = ' ';
238 CopyMem(timestring, resstring + pos, strlen(timestring));
239 pos += strlen(timestring);
240 resstring[pos++] = 0x0a;
242 if(Write(file, resstring, pos) < pos)
244 PrintFault(IoErr(), "Date");
245 error = RETURN_FAIL;
248 if(ownfile == 1)
249 Close(file);
251 else
253 PrintFault(IoErr(), "Date");
254 error = RETURN_FAIL;
257 return error;
260 int __nocommandline = 1;
262 int main(void)
264 int error = RETURN_OK;
265 STRPTR args[ARG_COUNT] = {NULL, NULL, NULL, NULL};
266 struct RDArgs *rda;
268 rda = ReadArgs(ARG_STRING, (IPTR *)args, NULL);
270 if (rda != NULL)
272 if (args[ARG_DAY] != NULL || args[ARG_DATE] != NULL ||
273 args[ARG_TIME] != NULL)
275 if ((error = setdate(args) == RETURN_OK))
277 if (args[ARG_VER] != NULL)
278 printdate(args[ARG_VER]);
281 else
282 error = printdate(args[ARG_VER]);
284 FreeArgs(rda);
286 else
288 PrintFault(IoErr(), "Date");
289 error = RETURN_FAIL;
292 return error;