1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * Copyright (c) 1998,2001 by Sun Microsystems, Inc.
3*7c478bd9Sstevel@tonic-gate * All rights reserved.
4*7c478bd9Sstevel@tonic-gate */
5*7c478bd9Sstevel@tonic-gate
6*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
7*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
8*7c478bd9Sstevel@tonic-gate
9*7c478bd9Sstevel@tonic-gate /*
10*7c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
11*7c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
12*7c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
13*7c478bd9Sstevel@tonic-gate */
14*7c478bd9Sstevel@tonic-gate
15*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
16*7c478bd9Sstevel@tonic-gate #include <time.h>
17*7c478bd9Sstevel@tonic-gate #include <string.h>
18*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
19*7c478bd9Sstevel@tonic-gate /*
20*7c478bd9Sstevel@tonic-gate * Convert a ctime(3) format string into a system format date.
21*7c478bd9Sstevel@tonic-gate * Return the date thus calculated.
22*7c478bd9Sstevel@tonic-gate *
23*7c478bd9Sstevel@tonic-gate * Return -1 if the string is not in ctime format.
24*7c478bd9Sstevel@tonic-gate */
25*7c478bd9Sstevel@tonic-gate
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate * Offsets into the ctime string to various parts.
28*7c478bd9Sstevel@tonic-gate */
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate #define E_MONTH 4
31*7c478bd9Sstevel@tonic-gate #define E_DAY 8
32*7c478bd9Sstevel@tonic-gate #define E_HOUR 11
33*7c478bd9Sstevel@tonic-gate #define E_MINUTE 14
34*7c478bd9Sstevel@tonic-gate #define E_SECOND 17
35*7c478bd9Sstevel@tonic-gate #define E_YEAR 20
36*7c478bd9Sstevel@tonic-gate
37*7c478bd9Sstevel@tonic-gate #ifdef __STDC__
38*7c478bd9Sstevel@tonic-gate static int lookup(char *);
39*7c478bd9Sstevel@tonic-gate static time_t emitl(struct tm *);
40*7c478bd9Sstevel@tonic-gate #else
41*7c478bd9Sstevel@tonic-gate static int lookup();
42*7c478bd9Sstevel@tonic-gate static time_t emitl();
43*7c478bd9Sstevel@tonic-gate #endif
44*7c478bd9Sstevel@tonic-gate
45*7c478bd9Sstevel@tonic-gate time_t
unctime(str)46*7c478bd9Sstevel@tonic-gate unctime(str)
47*7c478bd9Sstevel@tonic-gate char *str;
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate struct tm then;
50*7c478bd9Sstevel@tonic-gate char dbuf[30];
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate /* Definition of ctime(3) is 24 characters + newline + NUL */
53*7c478bd9Sstevel@tonic-gate (void) strncpy(dbuf, str, 24);
54*7c478bd9Sstevel@tonic-gate dbuf[24] = '\0';
55*7c478bd9Sstevel@tonic-gate dbuf[E_MONTH+3] = '\0';
56*7c478bd9Sstevel@tonic-gate then.tm_mon = lookup(&dbuf[E_MONTH]);
57*7c478bd9Sstevel@tonic-gate if (then.tm_mon < 0) {
58*7c478bd9Sstevel@tonic-gate return (-1);
59*7c478bd9Sstevel@tonic-gate }
60*7c478bd9Sstevel@tonic-gate then.tm_mday = atoi(&dbuf[E_DAY]);
61*7c478bd9Sstevel@tonic-gate then.tm_hour = atoi(&dbuf[E_HOUR]);
62*7c478bd9Sstevel@tonic-gate then.tm_min = atoi(&dbuf[E_MINUTE]);
63*7c478bd9Sstevel@tonic-gate then.tm_sec = atoi(&dbuf[E_SECOND]);
64*7c478bd9Sstevel@tonic-gate then.tm_year = atoi(&dbuf[E_YEAR]) - 1900;
65*7c478bd9Sstevel@tonic-gate return (emitl(&then));
66*7c478bd9Sstevel@tonic-gate }
67*7c478bd9Sstevel@tonic-gate
68*7c478bd9Sstevel@tonic-gate static char months[] =
69*7c478bd9Sstevel@tonic-gate "JanFebMarAprMayJunJulAugSepOctNovDec";
70*7c478bd9Sstevel@tonic-gate
71*7c478bd9Sstevel@tonic-gate static int
lookup(str)72*7c478bd9Sstevel@tonic-gate lookup(str)
73*7c478bd9Sstevel@tonic-gate char *str;
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate char *cp, *cp2;
76*7c478bd9Sstevel@tonic-gate
77*7c478bd9Sstevel@tonic-gate for (cp = months, cp2 = str; *cp != 0; cp += 3)
78*7c478bd9Sstevel@tonic-gate if (strncmp(cp, cp2, 3) == 0)
79*7c478bd9Sstevel@tonic-gate /* LINTED ptr arith will give < INT_MAX result */
80*7c478bd9Sstevel@tonic-gate return (((int)(cp-months)) / 3);
81*7c478bd9Sstevel@tonic-gate return (-1);
82*7c478bd9Sstevel@tonic-gate }
83*7c478bd9Sstevel@tonic-gate /*
84*7c478bd9Sstevel@tonic-gate * Routine to convert a localtime(3) format date back into
85*7c478bd9Sstevel@tonic-gate * a system format date.
86*7c478bd9Sstevel@tonic-gate */
87*7c478bd9Sstevel@tonic-gate static time_t
emitl(dp)88*7c478bd9Sstevel@tonic-gate emitl(dp)
89*7c478bd9Sstevel@tonic-gate struct tm *dp;
90*7c478bd9Sstevel@tonic-gate {
91*7c478bd9Sstevel@tonic-gate dp->tm_isdst = -1;
92*7c478bd9Sstevel@tonic-gate return (mktime(dp));
93*7c478bd9Sstevel@tonic-gate }
94