Commit | Line | Data |
---|---|---|
76797561 DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
7 | #ifndef lint | |
95f51977 | 8 | static char sccsid[] = "@(#)unctime.c 5.1 (Berkeley) 6/5/85"; |
76797561 DF |
9 | #endif not lint |
10 | ||
778e0301 KM |
11 | #include "include.4.1/sys/types.h" |
12 | #include "include.4.1/time.h" | |
6e1fe29b BJ |
13 | /* |
14 | * Convert a ctime(3) format string into a system format date. | |
15 | * Return the date thus calculated. | |
16 | * | |
17 | * Return -1 if the string is not in ctime format. | |
18 | */ | |
19 | ||
20 | /* | |
21 | * Offsets into the ctime string to various parts. | |
22 | */ | |
23 | ||
24 | #define E_MONTH 4 | |
25 | #define E_DAY 8 | |
26 | #define E_HOUR 11 | |
27 | #define E_MINUTE 14 | |
28 | #define E_SECOND 17 | |
29 | #define E_YEAR 20 | |
30 | ||
31 | time_t unctime(str) | |
32 | char *str; | |
33 | { | |
34 | struct tm then; | |
35 | char dbuf[30]; | |
36 | time_t emitl(); | |
37 | ||
38 | if (strlen(str) != 25) | |
39 | str[25] = 0; | |
40 | strcpy(dbuf, str); | |
41 | dbuf[E_MONTH+3] = 0; | |
42 | if ( (then.tm_mon = lookup(&dbuf[E_MONTH])) < 0) | |
43 | return(-1);; | |
44 | then.tm_mday = atoi(&dbuf[E_DAY]); | |
45 | then.tm_hour = atoi(&dbuf[E_HOUR]); | |
46 | then.tm_min = atoi(&dbuf[E_MINUTE]); | |
47 | then.tm_sec = atoi(&dbuf[E_SECOND]); | |
48 | then.tm_year = atoi(&dbuf[E_YEAR]) - 1900; | |
49 | return(emitl(&then)); | |
50 | } | |
51 | ||
52 | static char months[] = | |
53 | "JanFebMarAprMayJunJulAugSepOctNovDec"; | |
54 | ||
55 | static | |
56 | lookup(str) | |
57 | char *str; | |
58 | { | |
59 | register char *cp, *cp2; | |
60 | ||
61 | for (cp = months, cp2 = str; *cp != 0; cp += 3) | |
62 | if (strncmp(cp, cp2, 3) == 0) | |
63 | return((cp-months) / 3); | |
64 | return(-1); | |
65 | } | |
66 | /* | |
67 | * Routine to convert a localtime(3) format date back into | |
68 | * a system format date. | |
69 | * | |
70 | * Use a binary search. | |
71 | */ | |
72 | ||
73 | struct tm *localtime(); | |
74 | ||
75 | time_t emitl(dp) | |
76 | struct tm *dp; | |
77 | { | |
78 | time_t conv; | |
79 | register int i, bit; | |
80 | struct tm dcopy; | |
81 | ||
82 | dcopy = *dp; | |
83 | dp = &dcopy; | |
84 | conv = 0; | |
85 | for (i = 31; i >= 0; i--) { | |
86 | bit = 1 << i; | |
87 | conv |= bit; | |
88 | if (dcmp(localtime(&conv), dp) > 0) | |
89 | conv &= ~bit; | |
90 | } | |
91 | return(conv); | |
92 | } | |
93 | ||
94 | /* | |
95 | * Compare two localtime dates, return result. | |
96 | */ | |
97 | ||
98 | #define DECIDE(a) \ | |
99 | if (dp->a > dp2->a) \ | |
100 | return(1); \ | |
101 | if (dp->a < dp2->a) \ | |
102 | return(-1) | |
103 | ||
104 | static | |
105 | dcmp(dp, dp2) | |
106 | register struct tm *dp, *dp2; | |
107 | { | |
108 | ||
109 | DECIDE(tm_year); | |
110 | DECIDE(tm_mon); | |
111 | DECIDE(tm_mday); | |
112 | DECIDE(tm_hour); | |
113 | DECIDE(tm_min); | |
114 | DECIDE(tm_sec); | |
115 | return(0); | |
116 | } |