]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/seconds.c
ip/tftpd: use procsetuser() instead of writing #c/user
[plan9front.git] / sys / src / cmd / seconds.c
1 #include <u.h>
2 #include <libc.h>
3 #include <ctype.h>
4
5 char *knownfmt[] = {
6         /* asctime */
7         "WW MMM DD hh:mm:ss ?Z YYYY",
8         /* RFC3339 */
9         "YYYY-MM-DD[T]hh:mm:ss[Z]?Z",
10         "YYYY-MM-DD[T]hh:mm:ss[Z]?Z",
11         "YYYY-MM-DD[T]hh:mm:ss ?Z",
12         "YYYY-MM-DD[T]hh:mm:ss?Z",
13         nil,
14 };
15
16 char *datefmt[] = {
17         /* RFC5322 */
18         "?W ?DD ?MMM ?YYYY",
19         "?W, DD-?MM-YY",
20         /* RFC822/RFC2822 */
21         "DD MMM YYYY",
22         "DD MMM YY",
23         /* RFC850 */
24         "WW, DD-MMM-YY",
25         /* RFC1123 */
26         "WWW, DD MMM YYYY",
27         /* RFC 3339 and human-readable variants */
28         "YYYY-MM-DD",
29         "YYYY-MM-DD [@] ",
30         /* random formats */
31         "?W ?MMM ?DD ?YYYY",
32         "?MMM ?DD ?YYYY",
33         "?DD ?MM ?YYYY",
34         "MMM ?DD ?YYYY",
35         "YYYY ?MM ?DD",
36         "YYYY ?DD ?MM",
37         "YYYY/MM?/DD?",
38         "MMM YYYY ?DD",
39         "?DD YYYY MMM",
40         "MM/DD/YYYY",
41         nil
42 };
43
44 char *timefmt[] = {
45         " hh:mm:ss",
46         " hh:mm",
47         " hh",
48         " hh:mm:ss ?A",
49         " hh:mm ?A",
50         " hh ?A",
51         "",
52         nil,
53 };
54
55 char *zonefmt[] = {
56         " ?Z",
57         "",
58         nil,
59 };
60
61 static int
62 nojunk(char *p)
63 {
64         while(isspace(*p))
65                 p++;
66         if(*p == '\0')
67                 return 1;
68         werrstr("trailing junk");
69         return 0;
70 }
71
72 static void
73 usage(void)
74 {
75         fprint(2, "usage: %s [-f fmt] date-time...\n", argv0);
76         exits("usage");
77 }
78
79 /*
80  * seconds absolute_date ... - convert absolute_date to seconds since epoch
81  */
82 void
83 main(int argc, char **argv)
84 {
85         char **f, **df, **tf, **zf, *fmt, *ep, buf[256];
86         Tzone *tz;
87         Tm tm;
88         int i;
89
90         fmt = nil;
91         ARGBEGIN{
92         case 'f':
93                 fmt = EARGF(usage());
94                 break;
95         default:
96                 usage();
97         }ARGEND;
98
99         if((tz = tzload("local")) == nil)
100                 sysfatal("bad local time: %r");
101         for(i = 0; i < argc; i++){
102                 if(fmt != nil){
103                         if(tmparse(&tm, fmt, argv[i], tz, &ep) && nojunk(ep))
104                                 goto Found;
105                 }else{
106                         for(f = knownfmt; *f != nil; f++)
107                                 if(tmparse(&tm, *f, argv[i], tz, &ep) != nil && nojunk(ep))
108                                         goto Found;
109                         for(df = datefmt; *df; df++)
110                         for(tf = timefmt; *tf; tf++)
111                         for(zf = zonefmt; *zf; zf++){
112                                 snprint(buf, sizeof(buf), "%s%s%s", *df, *tf, *zf);
113                                 if(tmparse(&tm, buf, argv[i], tz, &ep) != nil && nojunk(ep))
114                                         goto Found;
115                         }
116                 }
117                 sysfatal("tmparse: %r");
118 Found:
119                 print("%lld\n", tmnorm(&tm));
120         }
121         exits(nil);
122 }