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>
35 /* some date parsing routines */
42 static char *day[] = {
52 /* free token list returned by parseLine */
55 LocalFreeTokens(alist)
56 register struct token *alist;
58 register struct token *nlist;
59 for (; alist; alist = nlist) {
71 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
78 LocalParseLine(char *aline, struct token **alist)
81 register char *tptr = NULL;
83 struct token *first, *last;
84 register struct token *ttok;
87 inToken = 0; /* not copying token chars at start */
92 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
94 inToken = 0; /* end of this token */
96 ttok = (struct token *)malloc(sizeof(struct token));
98 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
99 strcpy(ttok->key, tbuffer);
109 /* an alpha character */
114 if (tptr - tbuffer >= sizeof(tbuffer))
115 return -1; /* token too long */
119 /* last token flushed 'cause space(0) --> true */
128 /* keyword database for periodic date parsing */
129 static struct ptemp {
141 "sunday", 0x10000,}, {
142 "monday", 0x10001,}, {
143 "tuesday", 0x10002,}, {
144 "wednesday", 0x10003,}, {
145 "thursday", 0x10004,}, {
147 "friday", 0x10005,}, {
148 "saturday", 0x10006,}, {
158 * atime - time in seconds (Unix std)
160 * return value - ptr to time in text form. Ptr is to a static string.
164 ktime_DateOf(afs_int32 atime)
166 static char tbuffer[30];
168 tp = ctime((time_t *) & atime);
171 tbuffer[24] = 0; /* get rid of new line */
173 strcpy(tbuffer, "BAD TIME");
178 * parse 12:33:12 or 12:33 or 12 into ktime structure
180 * astr - ptr to string to be parsed
181 * ak - ptr to struct for return value.
183 * 0 - ak holds parsed time.
184 * -1 - error in format
188 ParseTime(register struct ktime *ak, register char *astr)
195 field = 0; /* 0=hour, 1=min, 2=sec */
198 ak->mask |= (KTIME_HOUR | KTIME_MIN | KTIME_SEC);
201 if (tc == 0 || tc == ':') {
213 } else if (!isdigit(tc))
214 return -1; /* syntax error */
221 if (ak->hour >= 24 || ak->min >= 60 || ak->sec >= 60)
227 ktime_Str2int32(register char *astr)
231 memset(&tk, 0, sizeof(tk));
232 if (ParseTime(&tk, astr))
233 return (-1); /* syntax error */
235 return ((tk.hour * 60 + tk.min) * 60 + tk.sec);
238 /* ktime_ParsePeriodic
239 * Parses periodic date of form
240 * now | never | at [time spec] | every [time spec]
241 * where [time spec] is a ktime string.
243 * adate - string to be parsed
244 * ak - ptr to structure for returned ktime
246 * 0 - parsed ktime in ak
247 * -1 - specification error
250 /* -1 means error, 0 means now, otherwise returns time of next event */
252 ktime_ParsePeriodic(char *adate, register struct ktime *ak)
255 register afs_int32 code;
258 memset(ak, 0, sizeof(*ak));
259 code = LocalParseLine(adate, &tt);
262 for (; tt; tt = tt->next) {
263 /* look at each token */
264 if (strcmp(tt->key, "now") == 0) {
265 ak->mask |= KTIME_NOW;
268 if (strcmp(tt->key, "never") == 0) {
269 ak->mask |= KTIME_NEVER;
272 if (strcmp(tt->key, "at") == 0)
274 if (strcmp(tt->key, "every") == 0)
276 if (isdigit(tt->key[0])) {
278 code = ParseTime(ak, tt->key);
283 /* otherwise use keyword table */
284 for (tp = ptkeys;; tp++) {
285 if (tp->key == NULL) {
288 if (strcmp(tp->key, tt->key) == 0)
291 /* now look at tp->value to see what we've got */
292 if ((tp->value >> 16) == 1) {
294 ak->mask |= KTIME_DAY;
295 ak->day = tp->value & 0xff;
297 if ((tp->value >> 16) == 2) {
299 if ((tp->value & 0xff) == 1) {
301 if (!(ak->mask & KTIME_HOUR))
306 else if (ak->hour != 12)
309 /* am is almost a noop, except that we map 12:01 am to 0:01 */
320 /* ktime_DisplayString
321 * Display ktime structure as English that could go into the ktime parser
323 * aparm - ktime to be converted to string
324 * astring - ptr to string, for the result
326 * 0 - astring contains ktime string.
329 ktime_DisplayString(struct ktime *aparm, register char *astring)
333 if (aparm->mask & KTIME_NEVER) {
334 strcpy(astring, "never");
336 } else if (aparm->mask & KTIME_NOW) {
337 strcpy(astring, "now");
340 strcpy(astring, "at");
341 if (aparm->mask & KTIME_DAY) {
342 strcat(astring, " ");
343 strcat(astring, day[aparm->day]);
345 if (aparm->mask & KTIME_HOUR) {
346 if (aparm->hour > 12)
347 sprintf(tempString, " %d", aparm->hour - 12);
348 else if (aparm->hour == 0)
349 strcpy(tempString, " 12");
351 sprintf(tempString, " %d", aparm->hour);
352 strcat(astring, tempString);
354 if (aparm->mask & KTIME_MIN) {
355 sprintf(tempString, ":%02d", aparm->min);
356 strcat(astring, tempString);
358 if ((aparm->mask & KTIME_SEC) && aparm->sec != 0) {
359 sprintf(tempString, ":%02d", aparm->sec);
360 strcat(astring, tempString);
362 if (aparm->mask & KTIME_HOUR) {
363 if (aparm->hour >= 12)
364 strcat(astring, " pm");
366 strcat(astring, " am");
372 /* get next time that matches ktime structure after time afrom */
374 ktime_next(struct ktime * aktime, afs_int32 afrom)
376 /* try by guessing from now */
378 time_t start; /* time to start looking */
379 time_t probe; /* a placeholder to use for advancing day to day */
380 time_t time_next; /* actual UTC time of probe, with time of day set */
382 struct ktime_date tdate;
384 start = afrom + time(0); /* time to start search */
385 tmask = aktime->mask;
387 /* handle some special cases */
388 if (tmask & KTIME_NEVER)
390 if (tmask & KTIME_NOW)
393 /* Use probe to fill in members of *tsp. Add 23 hours each iteration until
394 * time_next is correct. Only add 23 hrs to avoid skipping spring
395 * daylight savings time day */
396 for (probe = start;; probe += (23 * 3600)) {
397 tsp = localtime(&probe); /* find out what UTC time "probe" is */
399 tdate.year = tsp->tm_year;
400 tdate.month = tsp->tm_mon + 1;
401 tdate.day = tsp->tm_mday;
403 KTIMEDATE_YEAR | KTIMEDATE_MONTH | KTIMEDATE_DAY | KTIMEDATE_HOUR
404 | KTIMEDATE_MIN | KTIMEDATE_SEC;
405 tdate.hour = aktime->hour; /* edit in our changes */
406 tdate.min = aktime->min;
407 tdate.sec = aktime->sec;
408 time_next = ktime_InterpretDate(&tdate); /* Convert back to UTC time */
409 if (time_next < start)
410 continue; /* "probe" time is already past */
411 if ((tmask & KTIME_DAY) == 0) /* don't care about day, we're done */
413 tsp = localtime(&time_next);
414 if (tsp->tm_wday == aktime->day)
415 break; /* day matches, we're done */
421 /* compare date in both formats, and return as in strcmp */
424 KTimeCmp(register struct ktime *aktime, register struct tm *atm)
426 register afs_int32 tmask;
428 /* don't compare day of the week, since we can't tell the
429 * order in a cyclical set. Caller must check for equality, if
431 tmask = aktime->mask;
432 if (tmask & KTIME_HOUR) {
433 if (aktime->hour > atm->tm_hour)
435 if (aktime->hour < atm->tm_hour)
438 if (tmask & KTIME_MIN) {
439 if (aktime->min > atm->tm_min)
441 if (aktime->min < atm->tm_min)
444 if (tmask & KTIME_SEC) {
445 if (aktime->sec > atm->tm_sec)
447 if (aktime->sec < atm->tm_sec)
454 /* compare date in both formats, and return as in strcmp */
456 KDateCmp(register struct ktime_date *akdate, register struct tm *atm)
458 if (akdate->year > atm->tm_year)
460 if (akdate->year < atm->tm_year)
462 if (akdate->month > atm->tm_mon)
464 if (akdate->month < atm->tm_mon)
466 if (akdate->day > atm->tm_mday)
468 if (akdate->day < atm->tm_mday)
470 if (akdate->mask & KTIMEDATE_HOUR) {
471 if (akdate->hour > atm->tm_hour)
473 if (akdate->hour < atm->tm_hour)
476 if (akdate->mask & KTIMEDATE_MIN) {
477 if (akdate->min > atm->tm_min)
479 if (akdate->min < atm->tm_min)
482 if (akdate->mask & KTIMEDATE_SEC) {
483 if (akdate->sec > atm->tm_sec)
485 if (akdate->sec < atm->tm_sec)
492 * parse date string into ktime_date structure
494 * adate - string to be parsed
495 * akdate - ptr to ktime_date for result
497 * 0 - akdate contains converted date
498 * -1 - parsing failure
502 ktime_ParseDate(char *adate, struct ktime_date *akdate)
505 afs_int32 month, day2, year, hour, min, sec;
509 lcstring(never, adate, sizeof(never));
510 if (strcmp(never, "never") == 0)
511 akdate->mask = KTIMEDATE_NEVER;
512 else if (strcmp(never, "now") == 0)
513 akdate->mask = KTIMEDATE_NOW;
521 sscanf(adate, "%d / %d / %d %d : %d : %d%1s", &month, &day2, &year,
522 &hour, &min, &sec, &c[0]);
526 sscanf(adate, "%d / %d / %d %d : %d%1s", &month, &day2, &year,
530 code = sscanf(adate, "%d / %d / %d%1s", &month, &day2, &year, &c[0]);
537 if ((year < 0) || (month < 1) || (month > 12) || (day2 < 1) || (day2 > 31) || /* more or less */
538 (hour < 0) || (hour > 23) || (min < 0) || (min > 59) || (sec < 0)
543 year += 100; /* make 1/1/1 => Jan 1, 2001 */
544 else if (year >= 1900)
545 year -= 1900; /* allow 1/1/2001 to work */
547 return -2; /* only allow 2 or 4 digit years */
550 KTIMEDATE_YEAR | KTIMEDATE_MONTH | KTIMEDATE_DAY | KTIMEDATE_HOUR |
551 KTIMEDATE_MIN | KTIMEDATE_SEC;
554 akdate->month = month;
560 /* done successfully */
565 * Converts a ktime date string into an afs_int32
567 * adate - ktime date string
568 * aint32 - ptr to afs_int32
570 * 0 - aint32 contains converted date.
574 ktime_DateToInt32(char *adate, afs_int32 * aint32)
576 struct ktime_date tdate;
577 register afs_int32 code;
579 /* parse the date into a ktime_date structure */
580 code = ktime_ParseDate(adate, &tdate);
582 return code; /* failed to parse */
584 code = ktime_InterpretDate(&tdate); /* interpret as seconds since 1970 */
585 *aint32 = code; /* return it */
586 return 0; /* and declare no errors */
589 /* get useful error message to print about date input format */
591 ktime_GetDateUsage(void)
593 return "date format is 'mm/dd/yy [hh:mm]', using a 24 hour clock";
597 /* ktime_InterpretDate
598 * Converts ktime_date to an afs_int32
600 * akdate - date to be converted/interpreted
602 * returns KTIMEDATE_NEVERDATE - if never flag was set, or
603 * date converted to afs_int32.
607 ktime_InterpretDate(struct ktime_date * akdate)
609 register afs_uint32 tresult;
610 register afs_uint32 tbit;
612 register struct tm *tsp;
614 if (akdate->mask & KTIMEDATE_NOW)
616 if (akdate->mask & KTIMEDATE_NEVER)
617 return KTIMEDATE_NEVERDATE;
619 tbit = 1 << 30; /* start off at max signed result */
620 tresult = 0; /* result to return */
622 temp = tresult + tbit; /* see if adding this bit keeps us < akdate */
623 tsp = localtime(&temp);
626 if (tsp->tm_mon == 0) {
631 if (KDateCmp(akdate, tsp) >= 0) {
632 /* if temp still represents earlier than date than we're searching
633 * for, add in bit as increment, otherwise use old value and look
634 * for smaller increment */
637 tbit = tbit >> 1; /* try next bit */