11 /* This 'date' command supports only 2 time setting formats,
12 all the GNU strftime stuff (its in libc, lets use it),
13 setting time using UTC and displaying int, as well as
14 an RFC 822 complient date output for shell scripting
17 const char date_usage[] = "date [-uR] [+FORMAT|+%f] [ [-s|-d] MMDDhhmm[[CC]YY]\n | [[[[CCYY.]MM.DD-]hh:mm[:ss]]]] ]";
19 static struct option const long_options[] =
21 {"date", required_argument, NULL, 'd'},
22 /* {"rfc-822", no_argument, NULL, 'R'},
23 {"set", required_argument, NULL, 's'},
24 {"uct", no_argument, NULL, 'u'},
25 {"utc", no_argument, NULL, 'u'},
26 {"universal", no_argument, NULL, 'u'}, */
32 /* Input parsing code is always bulky - used heavy duty libc stuff as
33 much as possible, missed out a lot of bounds checking */
35 /* Default input handling to save suprising some people */
38 date_conv_time(struct tm *tm_time, const char *t_string) {
41 nr = sscanf(t_string, "%2d%2d%2d%2d%d",
48 if(nr < 4 || nr > 5) {
49 fprintf(stderr, "date: invalid date `%s'\n", t_string);
53 /* correct for century - minor Y2K problem here? */
54 if(tm_time->tm_year >= 1900)
55 tm_time->tm_year -= 1900;
64 /* The new stuff for LRP */
67 date_conv_ftime(struct tm *tm_time, const char *t_string) {
68 struct tm itm_time, jtm_time, ktm_time, \
69 ltm_time, mtm_time, ntm_time;
78 /* Parse input and assign appropriately to tm_time */
80 if(sscanf(t_string, "%d:%d:%d",
83 &itm_time.tm_sec) == 3 ) {
88 } else if (sscanf(t_string, "%d:%d",
90 &jtm_time.tm_min) == 2) {
95 } else if (sscanf(t_string, "%d.%d-%d:%d:%d",
100 &ktm_time.tm_sec) == 5) {
102 ktm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
106 } else if (sscanf(t_string, "%d.%d-%d:%d",
110 <m_time.tm_min) == 4) {
112 ltm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
116 } else if (sscanf(t_string, "%d.%d.%d-%d:%d:%d",
122 &mtm_time.tm_sec) == 6) {
124 mtm_time.tm_year -= 1900; /* Adjust years */
125 mtm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
129 } else if (sscanf(t_string, "%d.%d.%d-%d:%d",
134 &ntm_time.tm_min) == 5) {
135 ntm_time.tm_year -= 1900; /* Adjust years */
136 ntm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
142 fprintf(stderr, "date: invalid date `%s'\n", t_string);
151 fprintf(stderr, "date: only one date argument can be given at a time.\n");
156 date_main(struct FileInfo * i, int argc, char * * argv)
158 char *date_str = NULL;
159 char *date_fmt = NULL;
170 /* Interpret command line args */
173 while ((optc = getopt_long (argc, argv, "d:Rs:u", long_options, NULL))
185 if(date_str != NULL) date_err();
191 if (putenv ("TZ=UTC0") != 0) {
192 fprintf(stderr,"date: memory exhausted\n");
201 if(date_str != NULL) date_err();
212 n_args = argc - optind;
215 switch(argv[optind][0]) {
217 /* Date format strings */
218 if(date_fmt != NULL) {
219 fprintf(stderr, "date: only one date format can be given.\n");
222 date_fmt = &argv[optind][1];
229 /* Anything left over must be a date string to set the time */
231 if(date_str != NULL) date_err();
232 date_str = argv[optind];
239 /* Now we have parsed all the information except the date format
240 which depends on whether the clock is being set or read */
243 memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
244 /* Zero out fields - take her back to midnight!*/
245 if(date_str != NULL) {
251 /* Process any date input to UNIX time since 1 Jan 1970 */
252 if(date_str != NULL) {
254 if(strchr(date_str, ':') != NULL) {
255 date_conv_ftime(&tm_time, date_str);
257 date_conv_time(&tm_time, date_str);
260 /* Correct any day of week and day of year etc fields */
261 tm = mktime(&tm_time);
263 fprintf(stderr, "date: invalid date `%s'\n", date_str);
267 /* if setting time, set it */
269 if( stime(&tm) < 0) {
270 fprintf(stderr, "date: can't set date.\n");
278 /* Deal with format string */
279 if(date_fmt == NULL) {
282 ? "%a, %_d %b %Y %H:%M:%S GMT"
283 : "%a, %_d %b %Y %H:%M:%S %z")
284 : "%a %b %e %H:%M:%S %Z %Y");
286 } else if ( *date_fmt == '\0' ) {
287 /* Imitate what GNU 'date' does with NO format string! */
292 /* Handle special conversions */
294 if( strncmp( date_fmt, "%f", 2) == 0 ) {
295 date_fmt = "%Y.%m.%d-%H:%M:%S";
298 /* Print OUTPUT (after ALL that!) */
299 t_buff = malloc(201);
300 strftime(t_buff, 200, date_fmt, &tm_time);
301 printf("%s\n", t_buff);