1 /*-
2 * Copyright (c) 2000, 2001 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $Id: timetostr.c,v 1.11 2005/05/25 21:55:09 vlm Exp $
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #ifdef HAVE_SYSEXITS_H
33 #include <sysexits.h>
34 #else /* HAVE_SYSEXITS_H */
35 #define EX_USAGE 64
36 #endif /* HAVE_SYSEXITS_H */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #ifdef HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif
45
46 #ifdef HAVE_GETOPT_H
47 #include <getopt.h>
48 #endif
49
50 #include <sf_time.h>
51 #include <sf_core.h>
52
53 void usage(char *);
54
55 int timetostr_mode = 0;
56
57 int
main(int ac,char ** av)58 main(int ac, char **av) {
59
60 timetostr_mode = (av[0] && av[0][0] == 't');
61
62 if(timetostr_mode) {
63 /* timetostr mode */
64
65 int c;
66 int flags = 0;
67
68 while((c = getopt(ac, av, "lugrif:")) != -1)
69 switch(c) {
70 case 'l':
71 flags |= TFMT_LOCAL;
72 break;
73 case 'u':
74 flags |= TFMT_UF;
75 break;
76 case 'r':
77 flags |= TFMT_RFC822;
78 break;
79 case 'i':
80 flags |= TFMT_ISO8601;
81 break;
82 case 'g':
83 flags |= TFMT_OLD822GMT;
84 break;
85 case 'f':
86 flags = atoi(optarg);
87 break;
88 default:
89 usage(*av);
90 }
91 ac -= optind;
92
93 if(ac != 1)
94 usage(*av);
95
96 av += optind;
97
98 for(c = 0; c < ac; c++)
99 printf("%s\n", timetostr(atol(av[c]), flags));
100
101 } else {
102 /* timetostr mode */
103
104 svect *sl;
105 time_t tloc;
106 int i;
107
108 if(ac < 2)
109 usage(*av);
110
111 sl = sinit();
112
113 for(i = 1; i < ac; i++)
114 sadd(sl, av[i]);
115
116 tloc = strtotime(sjoin(sl, " "));
117 printf("%ld\n", tloc);
118 sfree(sl);
119 }
120
121 return 0;
122 }
123
usage(char * name)124 void usage(char *name) {
125 if(timetostr_mode) {
126 fprintf(stderr,
127 "Usage: %s [-l] [-u] [-g] [-r] [-i] [-f <flags>] <unixtime>\n",
128 name);
129 } else {
130 fprintf(stderr,
131 "Usage: %s <date_or_time_string>\n",
132 name);
133 }
134 exit(EX_USAGE);
135 }
136
137