]>
git.cameronkatri.com Git - pw-darwin.git/blob - pw/psdate.c
2 * Copyright (c) 1996 by David L. Nugent <davidn@blaze.net.au>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer as
10 * the first lines of this file unmodified.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by David L. Nugent.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE DAVID L. NUGENT ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 a2i(char const ** str
)
59 numerics(char const * str
)
61 int rc
= isdigit(*str
);
64 while (isdigit(*str
) || *str
== 'x')
70 aindex(char const * arr
[], char const ** str
, int len
)
76 l
= strlen(strncpy(mystr
, *str
, len
));
77 for (i
= 0; i
< l
; i
++)
78 mystr
[i
] = (char) tolower(mystr
[i
]);
79 for (i
= 0; arr
[i
] && strcmp(mystr
, arr
[i
]) != 0; i
++);
82 else { /* Skip past it */
83 while (**str
&& isalpha(**str
))
85 /* And any following whitespace */
86 while (**str
&& (**str
== ',' || isspace(**str
)))
93 weekday(char const ** str
)
95 static char const *days
[] =
96 {"sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL
};
98 return aindex(days
, str
, 3);
102 month(char const ** str
)
104 static char const *months
[] =
105 {"jan", "feb", "mar", "apr", "may", "jun", "jul",
106 "aug", "sep", "oct", "nov", "dec", NULL
};
108 return aindex(months
, str
, 3);
112 parse_time(char const * str
, int *hour
, int *min
, int *sec
)
115 if ((str
= strchr(str
, ':')) == NULL
)
120 *sec
= ((str
= strchr(str
, ':')) == NULL
) ? 0 : atoi(++str
);
126 parse_datesub(char const * str
, int *day
, int *mon
, int *year
)
130 static char const nchrs
[] = "0123456789 \t,/-.";
132 if ((i
= month(&str
)) != -1) {
134 if ((i
= a2i(&str
)) != 0)
136 } else if ((i
= a2i(&str
)) != 0) {
138 while (*str
&& strchr(nchrs
+ 10, *str
) != NULL
)
140 if ((i
= month(&str
)) != -1)
142 else if ((i
= a2i(&str
)) != 0)
147 while (*str
&& strchr(nchrs
+ 10, *str
) != NULL
)
160 * Parse time must be flexible, it handles the following formats:
161 * nnnnnnnnnnn UNIX timestamp (all numeric), 0 = now
162 * 0xnnnnnnnn UNIX timestamp in hexadecimal
163 * 0nnnnnnnnn UNIX timestamp in octal
165 * +nnnn[smhdwoy] Given time + nnnn hours, mins, days, weeks, months or years
166 * -nnnn[smhdwoy] Given time - nnnn hours, mins, days, weeks, months or years
167 * dd[ ./-]mmm[ ./-]yy Date }
168 * hh:mm:ss Time } May be combined
172 parse_date(time_t dt
, char const * str
)
182 while (*str
&& isspace(*str
))
186 val
= strtol(str
, &p
, 0);
188 } else if (*str
== '+' || *str
== '-') {
189 val
= strtol(str
, &p
, 0);
192 case 'H': /* hours */
197 case 'M': /* minutes */
201 case 'S': /* seconds */
206 dt
+= (val
* 86400L);
209 case 'W': /* weeks */
210 dt
+= (val
* 604800L);
213 case 'O': /* months */
215 T
->tm_mon
+= (int) val
;
219 case 'Y': /* years */
221 T
->tm_year
+= (int) val
;
226 if (T
->tm_mday
!= i
) {
229 dt
-= (time_t) 86400L;
231 default: /* unknown */
232 break; /* leave untouched */
238 * Skip past any weekday prefix
241 str
= strncpy(tmp
, str
, sizeof tmp
- 1);
242 tmp
[sizeof tmp
- 1] = '\0';
246 * See if we can break off any timezone
248 while ((q
= strrchr(tmp
, ' ')) != NULL
) {
249 if (strchr("(+-", q
[1]) != NULL
)
254 while (q
[j
] && isupper(q
[j
]))
264 * See if there is a time hh:mm[:ss]
266 if ((p
= strchr(tmp
, ':')) == NULL
) {
269 * No time string involved
271 T
->tm_hour
= T
->tm_min
= T
->tm_sec
= 0;
272 parse_datesub(tmp
, &T
->tm_mday
, &T
->tm_mon
, &T
->tm_year
);
274 char datestr
[64], timestr
[64];
277 * Let's chip off the time string
279 if ((q
= strpbrk(p
, " \t")) != NULL
) { /* Time first? */
282 strncpy(timestr
, str
, l
);
284 strncpy(datestr
, q
+ 1, sizeof datestr
);
285 datestr
[sizeof datestr
- 1] = '\0';
286 parse_time(timestr
, &T
->tm_hour
, &T
->tm_min
, &T
->tm_sec
);
287 parse_datesub(datestr
, &T
->tm_mday
, &T
->tm_mon
, &T
->tm_year
);
288 } else if ((q
= strrchr(tmp
, ' ')) != NULL
) { /* Time last */
291 strncpy(timestr
, q
+ 1, sizeof timestr
);
292 timestr
[sizeof timestr
- 1] = '\0';
293 strncpy(datestr
, tmp
, l
);
295 } else /* Bail out */
297 parse_time(timestr
, &T
->tm_hour
, &T
->tm_min
, &T
->tm_sec
);
298 parse_datesub(datestr
, &T
->tm_mday
, &T
->tm_mon
, &T
->tm_year
);