1 /* $Id: convdate.c 6372 2003-05-31 19:48:28Z rra $
3 ** Convert date strings and numbers to numbers and strings.
11 #include "inn/messages.h"
14 static const char usage[] = "\
15 Usage: convdate -n [date ...]\n\
16 convdate [-dl] -c [time ...]\n\
17 convdate [-dl] [-s] [date ...]\n\
19 convdate -n converts a date (in any format parseable by parsedate) to the\n\
20 number of seconds since epoch. convdate -s does the same, but converts\n\
21 to a date string. convdate -c converts seconds since epoch to a date\n\
22 string. The default output is the output of ctime (normally the same format\n\
23 as returned by the date command). If -d is given, the output is formatted\n\
24 as a valid Usenet article Date header. If -l is given with -d, format the\n\
25 time in local time rather than UTC. If no options are given, the -s\n\
26 behavior is the default; if no dates are given, the current time is used.\n";
28 /* Whether to format the output as a Date header. */
29 static bool date_format = false;
31 /* Whether to use local time instead of UTC. */
32 static bool date_local = false;
36 ** Return true if the given string is entirely digits.
39 isdigits(const char *p)
42 if (!CTYPE(isdigit, *p))
49 ** Print date corresponding to the provided time_t. By default, the output
50 ** of ctime is printed, but if date_format is true, makedate is used
51 ** instead. If date_local is true, format in local time; otherwise, use
52 ** UTC. Returns success.
55 print_date(time_t date)
57 char date_buffer[128];
61 if (!makedate(date, date_local, date_buffer, sizeof(date_buffer))) {
62 warn("can't format %ld", (long) date);
65 printf("%s\n", date_buffer);
68 result = ctime(&date);
70 warn("can't format %ld", (long) date);
81 ** The core function. Given a string representing a date (in some format
82 ** given by the mode) and a mode ('s', 'n', or 'c', corresponding to the
83 ** basic three options to the program), convert the date and print the
84 ** output. date may be NULL, in which case the current date is used.
85 ** Returns true if conversion was successful, false otherwise.
88 convdate(const char *date, char mode)
92 /* Convert the given date to seconds or obtain the current time. */
95 } else if (mode == 'c') {
96 if (!isdigits(date)) {
97 warn("\"%s\" doesn't look like a number", date);
100 seconds = (time_t) atol(date);
103 seconds = parsedate((char *) date, NULL);
104 if (seconds == (time_t) -1) {
105 warn("can't convert \"%s\"", date);
110 /* Output the resulting date. */
112 printf("%ld\n", (long) seconds);
115 return print_date(seconds);
121 main(int argc, char *argv[])
127 message_program_name = "convdate";
130 while ((option = getopt(argc, argv, "cdhlns")) != EOF) {
133 printf("%s\n", usage);
145 if (mode != 0) die("only one of -c, -n, or -s is allowed");
149 fprintf(stderr, "%s", usage);
159 /* Perform the desired action for each provided argument. */
161 exit(convdate(NULL, mode) ? 0 : 1);
163 for (date = *argv, status = 0; date != NULL; date = *++argv)
164 status += (convdate(date, mode) ? 0 : 1);