2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
8 /******************************************************************************
12 Date [<day>] [<date>] [<time>] [TO | VER <filename>]
16 DAY,DATE,TIME,TO=VER/K
24 Displays or sets the system date and/or time.
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
32 TIME -- sets time in format HH:MM:SS
33 TO -- output is sent to file
52 ******************************************************************************/
55 #include <proto/exec.h>
56 #include <exec/errors.h>
58 #include <proto/dos.h>
60 #include <dos/datetime.h>
61 #include <devices/timer.h>
66 static const char version
[] = "$VER: Date 41.4 (5.3.2000)\n";
68 #define ARG_STRING "DAY,DATE,TIME,TO=VER/K"
75 static WORD
chrcount(STRPTR s
, UBYTE c
)
82 if (sc
== c
) retval
++;
88 int setdate(STRPTR
*day_date_time
)
90 int error
= RETURN_OK
;
92 struct timerequest
*timerReq
;
93 struct MsgPort
*timerMP
;
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
], '-'))
107 realdate
= day_date_time
[i
];
109 else if ((count
= chrcount(day_date_time
[i
], ':')))
114 /* seconds are missing */
116 if (strlen(day_date_time
[i
]) <= 5)
118 strcpy(fulltime
, day_date_time
[i
]);
119 strcat(fulltime
, ":00");
124 realtime
= day_date_time
[i
];
129 realtime
= day_date_time
[i
];
134 /* must be week day name */
136 if (!realdate
) realdate
= day_date_time
[i
];
141 timerMP
= CreateMsgPort();
144 timerReq
= (struct timerequest
*)CreateIORequest(timerMP
, sizeof(struct timerequest
));
148 timererror
= OpenDevice(TIMERNAME
, UNIT_VBLANK
,
149 &timerReq
->tr_node
, 0L);
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");
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
);
182 PutStr("Date: Error opening timer.device\n");
185 DeleteIORequest(&timerReq
->tr_node
);
189 PutStr("Date: Error creating timerequest\n");
192 DeleteMsgPort(timerMP
);
196 PutStr("Date: Error creating MsgPort\n");
204 int printdate(STRPTR filename
)
206 BPTR file
= Output();
208 int error
= RETURN_OK
;
210 char dowstring
[LEN_DATSTRING
* 2], datestring
[LEN_DATSTRING
* 2],
211 timestring
[LEN_DATSTRING
* 2], resstring
[LEN_DATSTRING
*6+1];
215 file
= Open(filename
, MODE_NEWFILE
);
219 if(file
!= (BPTR
)NULL
)
223 DateStamp(&dt
.dat_Stamp
);
225 dt
.dat_Format
= FORMAT_DEF
;
227 dt
.dat_StrDay
= dowstring
;
228 dt
.dat_StrDate
= datestring
;
229 dt
.dat_StrTime
= timestring
;
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");
253 PrintFault(IoErr(), "Date");
260 int __nocommandline
= 1;
264 int error
= RETURN_OK
;
265 STRPTR args
[ARG_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
268 rda
= ReadArgs(ARG_STRING
, (IPTR
*)args
, 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
]);
282 error
= printdate(args
[ARG_VER
]);
288 PrintFault(IoErr(), "Date");