]> diplodocus.org Git - nmh/blob - docs/historical/mh-nov-1983/cmds/emitl.c
Removed --depth 1 from git clone invocation.
[nmh] / docs / historical / mh-nov-1983 / cmds / emitl.c
1 /*
2 * Routine to convert a localtime(3) format date back into
3 * a system format date.
4 *
5 * Hats off to Bob Kridle for the insight that the way to do
6 * this is by binary search of the system date space.
7 */
8
9 #include <sys/time.h>
10
11 struct tm *localtime();
12
13 long
14 emitl(dp)
15 struct tm *dp;
16 {
17 long conv;
18 register int i, bit;
19 struct tm dcopy;
20
21 dcopy = *dp;
22 dp = &dcopy;
23 conv = 0;
24 for (i = 31; i >= 0; i--) {
25 bit = 1 << i;
26 conv |= bit;
27 if (dcmp(localtime(&conv), dp) > 0)
28 conv &= ~bit;
29 }
30 return(conv);
31 }
32
33 /*
34 * Compare two localtime dates, return result.
35 */
36
37 #define DECIDE(a) \
38 if (dp->a > dp2->a) \
39 return(1); \
40 if (dp->a < dp2->a) \
41 return(-1)
42
43 static
44 dcmp(dp, dp2)
45 register struct tm *dp, *dp2;
46 {
47
48 DECIDE(tm_year);
49 DECIDE(tm_mon);
50 DECIDE(tm_mday);
51 DECIDE(tm_hour);
52 DECIDE(tm_min);
53 DECIDE(tm_sec);
54 return(0);
55 }