2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
16 #include <sys/types.h>
29 /* some date parsing routines */
36 static char *day[] = {
46 /* free token list returned by parseLine */
49 LocalFreeTokens(alist)
50 register struct token *alist;
52 register struct token *nlist;
53 for (; alist; alist = nlist) {
65 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
72 LocalParseLine(char *aline, struct token **alist)
75 register char *tptr = NULL;
77 struct token *first, *last;
78 register struct token *ttok;
81 inToken = 0; /* not copying token chars at start */
86 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
88 inToken = 0; /* end of this token */
90 ttok = (struct token *)malloc(sizeof(struct token));
92 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
93 strcpy(ttok->key, tbuffer);
103 /* an alpha character */
108 if (tptr - tbuffer >= sizeof(tbuffer))
109 return -1; /* token too long */
113 /* last token flushed 'cause space(0) --> true */
122 /* keyword database for periodic date parsing */
123 static struct ptemp {
135 "sunday", 0x10000,}, {
136 "monday", 0x10001,}, {
137 "tuesday", 0x10002,}, {
138 "wednesday", 0x10003,}, {
139 "thursday", 0x10004,}, {
141 "friday", 0x10005,}, {
142 "saturday", 0x10006,}, {
152 * atime - time in seconds (Unix std)
154 * return value - ptr to time in text form. Ptr is to a static string.
158 ktime_DateOf(afs_int32 atime)
160 static char tbuffer[30];
166 tbuffer[24] = 0; /* get rid of new line */
168 strcpy(tbuffer, "BAD TIME");
173 * parse 12:33:12 or 12:33 or 12 into ktime structure
175 * astr - ptr to string to be parsed
176 * ak - ptr to struct for return value.
178 * 0 - ak holds parsed time.
179 * -1 - error in format
183 ParseTime(register struct ktime *ak, register char *astr)
190 field = 0; /* 0=hour, 1=min, 2=sec */
193 ak->mask |= (KTIME_HOUR | KTIME_MIN | KTIME_SEC);
196 if (tc == 0 || tc == ':') {
208 } else if (!isdigit(tc))
209 return -1; /* syntax error */
216 if (ak->hour >= 24 || ak->min >= 60 || ak->sec >= 60)
222 ktime_Str2int32(register char *astr)
226 memset(&tk, 0, sizeof(tk));
227 if (ParseTime(&tk, astr))
228 return (-1); /* syntax error */
230 return ((tk.hour * 60 + tk.min) * 60 + tk.sec);
233 /* ktime_ParsePeriodic
234 * Parses periodic date of form
235 * now | never | at [time spec] | every [time spec]
236 * where [time spec] is a ktime string.
238 * adate - string to be parsed
239 * ak - ptr to structure for returned ktime
241 * 0 - parsed ktime in ak
242 * -1 - specification error
245 /* -1 means error, 0 means now, otherwise returns time of next event */
247 ktime_ParsePeriodic(char *adate, register struct ktime *ak)
250 register afs_int32 code;
253 memset(ak, 0, sizeof(*ak));
254 code = LocalParseLine(adate, &tt);
257 for (; tt; tt = tt->next) {
258 /* look at each token */
259 if (strcmp(tt->key, "now") == 0) {
260 ak->mask |= KTIME_NOW;
263 if (strcmp(tt->key, "never") == 0) {
264 ak->mask |= KTIME_NEVER;
267 if (strcmp(tt->key, "at") == 0)
269 if (strcmp(tt->key, "every") == 0)
271 if (isdigit(tt->key[0])) {
273 code = ParseTime(ak, tt->key);
278 /* otherwise use keyword table */
279 for (tp = ptkeys;; tp++) {
280 if (tp->key == NULL) {
283 if (strcmp(tp->key, tt->key) == 0)
286 /* now look at tp->value to see what we've got */
287 if ((tp->value >> 16) == 1) {
289 ak->mask |= KTIME_DAY;
290 ak->day = tp->value & 0xff;
292 if ((tp->value >> 16) == 2) {
294 if ((tp->value & 0xff) == 1) {
296 if (!(ak->mask & KTIME_HOUR))
301 else if (ak->hour != 12)
304 /* am is almost a noop, except that we map 12:01 am to 0:01 */
315 /* ktime_DisplayString
316 * Display ktime structure as English that could go into the ktime parser
318 * aparm - ktime to be converted to string
319 * astring - ptr to string, for the result
321 * 0 - astring contains ktime string.
324 ktime_DisplayString(struct ktime *aparm, register char *astring)
328 if (aparm->mask & KTIME_NEVER) {
329 strcpy(astring, "never");
331 } else if (aparm->mask & KTIME_NOW) {
332 strcpy(astring, "now");
335 strcpy(astring, "at");
336 if (aparm->mask & KTIME_DAY) {
337 strcat(astring, " ");
338 strcat(astring, day[aparm->day]);
340 if (aparm->mask & KTIME_HOUR) {
341 if (aparm->hour > 12)
342 sprintf(tempString, " %d", aparm->hour - 12);
343 else if (aparm->hour == 0)
344 strcpy(tempString, " 12");
346 sprintf(tempString, " %d", aparm->hour);
347 strcat(astring, tempString);
349 if (aparm->mask & KTIME_MIN) {
350 sprintf(tempString, ":%02d", aparm->min);
351 strcat(astring, tempString);
353 if ((aparm->mask & KTIME_SEC) && aparm->sec != 0) {
354 sprintf(tempString, ":%02d", aparm->sec);
355 strcat(astring, tempString);
357 if (aparm->mask & KTIME_HOUR) {
358 if (aparm->hour >= 12)
359 strcat(astring, " pm");
361 strcat(astring, " am");
367 /* get next time that matches ktime structure after time afrom */
369 ktime_next(struct ktime * aktime, afs_int32 afrom)
371 /* try by guessing from now */
373 time_t start; /* time to start looking */
374 time_t probe; /* a placeholder to use for advancing day to day */
375 time_t time_next; /* actual UTC time of probe, with time of day set */
377 struct ktime_date tdate;
379 start = afrom + time(0); /* time to start search */
380 tmask = aktime->mask;
382 /* handle some special cases */
383 if (tmask & KTIME_NEVER)
385 if (tmask & KTIME_NOW)
388 /* Use probe to fill in members of *tsp. Add 23 hours each iteration until
389 * time_next is correct. Only add 23 hrs to avoid skipping spring
390 * daylight savings time day */
391 for (probe = start;; probe += (23 * 3600)) {
392 tsp = localtime(&probe); /* find out what UTC time "probe" is */
394 tdate.year = tsp->tm_year;
395 tdate.month = tsp->tm_mon + 1;
396 tdate.day = tsp->tm_mday;
398 KTIMEDATE_YEAR | KTIMEDATE_MONTH | KTIMEDATE_DAY | KTIMEDATE_HOUR
399 | KTIMEDATE_MIN | KTIMEDATE_SEC;
400 tdate.hour = aktime->hour; /* edit in our changes */
401 tdate.min = aktime->min;
402 tdate.sec = aktime->sec;
403 time_next = ktime_InterpretDate(&tdate); /* Convert back to UTC time */
404 if (time_next < start)
405 continue; /* "probe" time is already past */
406 if ((tmask & KTIME_DAY) == 0) /* don't care about day, we're done */
408 tsp = localtime(&time_next);
409 if (tsp->tm_wday == aktime->day)
410 break; /* day matches, we're done */
416 /* compare date in both formats, and return as in strcmp */
419 KTimeCmp(register struct ktime *aktime, register struct tm *atm)
421 register afs_int32 tmask;
423 /* don't compare day of the week, since we can't tell the
424 * order in a cyclical set. Caller must check for equality, if
426 tmask = aktime->mask;
427 if (tmask & KTIME_HOUR) {
428 if (aktime->hour > atm->tm_hour)
430 if (aktime->hour < atm->tm_hour)
433 if (tmask & KTIME_MIN) {
434 if (aktime->min > atm->tm_min)
436 if (aktime->min < atm->tm_min)
439 if (tmask & KTIME_SEC) {
440 if (aktime->sec > atm->tm_sec)
442 if (aktime->sec < atm->tm_sec)
449 /* compare date in both formats, and return as in strcmp */
451 KDateCmp(register struct ktime_date *akdate, register struct tm *atm)
453 if (akdate->year > atm->tm_year)
455 if (akdate->year < atm->tm_year)
457 if (akdate->month > atm->tm_mon)
459 if (akdate->month < atm->tm_mon)
461 if (akdate->day > atm->tm_mday)
463 if (akdate->day < atm->tm_mday)
465 if (akdate->mask & KTIMEDATE_HOUR) {
466 if (akdate->hour > atm->tm_hour)
468 if (akdate->hour < atm->tm_hour)
471 if (akdate->mask & KTIMEDATE_MIN) {
472 if (akdate->min > atm->tm_min)
474 if (akdate->min < atm->tm_min)
477 if (akdate->mask & KTIMEDATE_SEC) {
478 if (akdate->sec > atm->tm_sec)
480 if (akdate->sec < atm->tm_sec)
487 * parse date string into ktime_date structure
489 * adate - string to be parsed
490 * akdate - ptr to ktime_date for result
492 * 0 - akdate contains converted date
493 * -1 - parsing failure
497 ktime_ParseDate(char *adate, struct ktime_date *akdate)
500 afs_int32 month, day2, year, hour, min, sec;
504 lcstring(never, adate, sizeof(never));
505 if (strcmp(never, "never") == 0)
506 akdate->mask = KTIMEDATE_NEVER;
507 else if (strcmp(never, "now") == 0)
508 akdate->mask = KTIMEDATE_NOW;
514 /* Old ambiguous mm/dd/yy hh:mm:ss format */
517 sscanf(adate, "%d / %d / %d %d : %d : %d%1s", &month, &day2, &year,
518 &hour, &min, &sec, &c[0]);
522 sscanf(adate, "%d / %d / %d %d : %d%1s", &month, &day2, &year,
527 sscanf(adate, "%d / %d / %d%1s", &month, &day2, &year, &c[0]);
534 /* New ISO 8601 (subset) format */
537 hour = min = sec = 0;
539 sscanf(adate, "%d-%d-%d %d:%d:%d%1s", &year, &month, &day2,
540 &hour, &min, &sec, c);
541 if (code != 3 && code != 5 && code != 6)
548 if ((year < 0) || (month < 1) || (month > 12) || (day2 < 1) || (day2 > 31) || /* more or less */
549 (hour < 0) || (hour > 23) || (min < 0) || (min > 59) || (sec < 0)
554 year += 100; /* make 1/1/1 => Jan 1, 2001 */
555 else if (year >= 1900)
556 year -= 1900; /* allow 1/1/2001 to work */
558 return -2; /* only allow 2 or 4 digit years */
561 KTIMEDATE_YEAR | KTIMEDATE_MONTH | KTIMEDATE_DAY | KTIMEDATE_HOUR |
562 KTIMEDATE_MIN | KTIMEDATE_SEC;
565 akdate->month = month;
571 /* done successfully */
576 * Converts a ktime date string into an afs_int32
578 * adate - ktime date string
579 * aint32 - ptr to afs_int32
581 * 0 - aint32 contains converted date.
585 ktime_DateToInt32(char *adate, afs_int32 * aint32)
587 struct ktime_date tdate;
592 if (sscanf(adate, "%lu%1s", &l, c) == 1 && l > 200000000)
595 /* parse the date into a ktime_date structure */
596 code = ktime_ParseDate(adate, &tdate);
598 return code; /* failed to parse */
599 *aint32 = ktime_InterpretDate(&tdate); /* interpret as seconds since 1970 */
605 /* get useful error message to print about date input format */
607 ktime_GetDateUsage(void)
609 return "date format is '(yyyy-mm-dd | mm/dd/yy) [hh:mm]', using a 24 hour clock";
613 /* ktime_InterpretDate
614 * Converts ktime_date to an afs_int32
616 * akdate - date to be converted/interpreted
618 * returns KTIMEDATE_NEVERDATE - if never flag was set, or
619 * date converted to afs_int32.
623 ktime_InterpretDate(struct ktime_date * akdate)
625 register afs_uint32 tresult;
626 register afs_uint32 tbit;
628 register struct tm *tsp;
630 if (akdate->mask & KTIMEDATE_NOW)
632 if (akdate->mask & KTIMEDATE_NEVER)
633 return KTIMEDATE_NEVERDATE;
635 tbit = 1 << 30; /* start off at max signed result */
636 tresult = 0; /* result to return */
638 temp = tresult + tbit; /* see if adding this bit keeps us < akdate */
639 tsp = localtime(&temp);
642 if (tsp->tm_mon == 0) {
647 if (KDateCmp(akdate, tsp) >= 0) {
648 /* if temp still represents earlier than date than we're searching
649 * for, add in bit as increment, otherwise use old value and look
650 * for smaller increment */
653 tbit = tbit >> 1; /* try next bit */