2 Copyright © 1995-2011, 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 ******************************************************************************/
54 #include <proto/exec.h>
55 #include <exec/errors.h>
57 #include <proto/dos.h>
59 #include <dos/datetime.h>
60 #include <devices/timer.h>
64 const TEXT version
[] = "$VER: Date 41.4 (15.11.2011)\n";
66 #define ARG_STRING "DAY,DATE,TIME,TO=VER/K"
73 static WORD
chrcount(STRPTR s
, UBYTE c
)
80 if (sc
== c
) retval
++;
86 int setdate(STRPTR
*day_date_time
)
88 int error
= RETURN_OK
;
90 struct timerequest
*timerReq
;
91 struct MsgPort
*timerMP
;
95 STRPTR realtime
= NULL
, realdate
= NULL
;
97 for(i
= 0; i
< 3; i
++)
99 if (day_date_time
[i
] == NULL
) continue;
101 if (chrcount(day_date_time
[i
], '-'))
105 realdate
= day_date_time
[i
];
107 else if ((count
= chrcount(day_date_time
[i
], ':')))
112 /* seconds are missing */
114 if (strlen(day_date_time
[i
]) <= 5)
116 strcpy(fulltime
, day_date_time
[i
]);
117 strcat(fulltime
, ":00");
122 realtime
= day_date_time
[i
];
127 realtime
= day_date_time
[i
];
132 /* must be week day name */
134 if (!realdate
) realdate
= day_date_time
[i
];
139 dt
.dat_Format
= FORMAT_DOS
;
140 dt
.dat_Flags
= DTF_FUTURE
;
141 dt
.dat_StrDay
= NULL
; /* StrToDate ignores this anyway */
142 dt
.dat_StrDate
= realdate
;
143 dt
.dat_StrTime
= realtime
;
145 DateStamp(&dt
.dat_Stamp
);
147 if((!realdate
&& !realtime
) || (StrToDate(&dt
) == 0))
149 PutStr( "***Bad args:\n"
150 "- use DD-MMM-YY or <dayname> or yesterday etc. to set date\n"
151 " HH:MM:SS or HH:MM to set time\n");
156 timerMP
= CreateMsgPort();
159 timerReq
= (struct timerequest
*)CreateIORequest(timerMP
, sizeof(struct timerequest
));
163 timererror
= OpenDevice(TIMERNAME
, UNIT_VBLANK
,
164 &timerReq
->tr_node
, 0L);
167 timerReq
->tr_time
.tv_secs
= dt
.dat_Stamp
.ds_Days
*60*60*24 +
168 dt
.dat_Stamp
.ds_Minute
*60 +
169 dt
.dat_Stamp
.ds_Tick
/ TICKS_PER_SECOND
;
170 timerReq
->tr_time
.tv_micro
= 0;
171 timerReq
->tr_node
.io_Command
= TR_SETSYSTIME
;
172 timerReq
->tr_node
.io_Flags
|= IOF_QUICK
;
174 DoIO(&timerReq
->tr_node
);
176 CloseDevice(&timerReq
->tr_node
);
180 PutStr("Date: Error opening timer.device\n");
183 DeleteIORequest(&timerReq
->tr_node
);
187 PutStr("Date: Error creating timerequest\n");
190 DeleteMsgPort(timerMP
);
194 PutStr("Date: Error creating MsgPort\n");
203 int printdate(STRPTR filename
)
205 BPTR file
= Output();
207 int error
= RETURN_OK
;
209 char daystring
[LEN_DATSTRING
* 2], datestring
[LEN_DATSTRING
* 2],
210 timestring
[LEN_DATSTRING
* 2], resstring
[LEN_DATSTRING
*6+1];
214 file
= Open(filename
, MODE_NEWFILE
);
218 if(file
!= (BPTR
)NULL
)
222 DateStamp(&dt
.dat_Stamp
);
224 dt
.dat_Format
= FORMAT_DOS
; // FORMAT_DEF;
226 dt
.dat_StrDay
= daystring
;
227 dt
.dat_StrDate
= datestring
;
228 dt
.dat_StrTime
= timestring
;
231 CopyMem(daystring
, resstring
, strlen(daystring
));
232 pos
+= strlen(daystring
);
233 resstring
[pos
++] = ' ';
234 CopyMem(datestring
, resstring
+ pos
, strlen(datestring
));
235 pos
+= strlen(datestring
);
236 resstring
[pos
++] = ' ';
237 CopyMem(timestring
, resstring
+ pos
, strlen(timestring
));
238 pos
+= strlen(timestring
);
239 resstring
[pos
++] = 0x0a;
241 if(Write(file
, resstring
, pos
) < pos
)
243 PrintFault(IoErr(), "Date");
254 PrintFault(IoErr(), "Date");
261 int __nocommandline
= 1;
265 int error
= RETURN_OK
;
266 STRPTR args
[ARG_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
269 rda
= ReadArgs(ARG_STRING
, (IPTR
*)args
, NULL
);
273 if (args
[ARG_DAY
] != NULL
|| args
[ARG_DATE
] != NULL
|| args
[ARG_TIME
] != NULL
)
275 if (((error
= setdate(args
)) == RETURN_OK
))
277 if (args
[ARG_VER
] != NULL
)
278 printdate(args
[ARG_VER
]);
283 error
= printdate(args
[ARG_VER
]);
290 PrintFault(IoErr(), "Date");