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>
14 #include <sys/types.h>
27 /* some date parsing routines */
34 static char *day[] = {
44 /* free token list returned by parseLine */
47 LocalFreeTokens(alist)
51 for (; alist; alist = nlist) {
63 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
70 LocalParseLine(char *aline, struct token **alist)
75 struct token *first, *last;
79 inToken = 0; /* not copying token chars at start */
84 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
86 inToken = 0; /* end of this token */
88 ttok = (struct token *)malloc(sizeof(struct token));
90 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
91 strcpy(ttok->key, tbuffer);
101 /* an alpha character */
106 if (tptr - tbuffer >= sizeof(tbuffer))
107 return -1; /* token too long */
111 /* last token flushed 'cause space(0) --> true */
120 /* keyword database for periodic date parsing */
121 static struct ptemp {
133 "sunday", 0x10000,}, {
134 "monday", 0x10001,}, {
135 "tuesday", 0x10002,}, {
136 "wednesday", 0x10003,}, {
137 "thursday", 0x10004,}, {
139 "friday", 0x10005,}, {
140 "saturday", 0x10006,}, {
150 * atime - time in seconds (Unix std)
152 * return value - ptr to time in text form. Ptr is to a static string.
156 ktime_DateOf(afs_int32 atime)
158 static char tbuffer[30];
164 tbuffer[24] = 0; /* get rid of new line */
166 strcpy(tbuffer, "BAD TIME");
171 * parse 12:33:12 or 12:33 or 12 into ktime structure
173 * astr - ptr to string to be parsed
174 * ak - ptr to struct for return value.
176 * 0 - ak holds parsed time.
177 * -1 - error in format
181 ParseTime(struct ktime *ak, char *astr)
188 field = 0; /* 0=hour, 1=min, 2=sec */
191 ak->mask |= (KTIME_HOUR | KTIME_MIN | KTIME_SEC);
194 if (tc == 0 || tc == ':') {
206 } else if (!isdigit(tc))
207 return -1; /* syntax error */
214 if (ak->hour >= 24 || ak->min >= 60 || ak->sec >= 60)
220 ktime_Str2int32(char *astr)
224 memset(&tk, 0, sizeof(tk));
225 if (ParseTime(&tk, astr))
226 return (-1); /* syntax error */
228 return ((tk.hour * 60 + tk.min) * 60 + tk.sec);
231 /* ktime_ParsePeriodic
232 * Parses periodic date of form
233 * now | never | at [time spec] | every [time spec]
234 * where [time spec] is a ktime string.
236 * adate - string to be parsed
237 * ak - ptr to structure for returned ktime
239 * 0 - parsed ktime in ak
240 * -1 - specification error
243 /* -1 means error, 0 means now, otherwise returns time of next event */
245 ktime_ParsePeriodic(char *adate, struct ktime *ak)
251 memset(ak, 0, sizeof(*ak));
252 code = LocalParseLine(adate, &tt);
255 for (; tt; tt = tt->next) {
256 /* look at each token */
257 if (strcmp(tt->key, "now") == 0) {
258 ak->mask |= KTIME_NOW;
261 if (strcmp(tt->key, "never") == 0) {
262 ak->mask |= KTIME_NEVER;
265 if (strcmp(tt->key, "at") == 0)
267 if (strcmp(tt->key, "every") == 0)
269 if (isdigit(tt->key[0])) {
271 code = ParseTime(ak, tt->key);
276 /* otherwise use keyword table */
277 for (tp = ptkeys;; tp++) {
278 if (tp->key == NULL) {
281 if (strcmp(tp->key, tt->key) == 0)
284 /* now look at tp->value to see what we've got */
285 if ((tp->value >> 16) == 1) {
287 ak->mask |= KTIME_DAY;
288 ak->day = tp->value & 0xff;
290 if ((tp->value >> 16) == 2) {
292 if ((tp->value & 0xff) == 1) {
294 if (!(ak->mask & KTIME_HOUR))
299 else if (ak->hour != 12)
302 /* am is almost a noop, except that we map 12:01 am to 0:01 */
313 /* ktime_DisplayString
314 * Display ktime structure as English that could go into the ktime parser
316 * aparm - ktime to be converted to string
317 * astring - ptr to string, for the result
319 * 0 - astring contains ktime string.
322 ktime_DisplayString(struct ktime *aparm, char *astring)
326 if (aparm->mask & KTIME_NEVER) {
327 strcpy(astring, "never");
329 } else if (aparm->mask & KTIME_NOW) {
330 strcpy(astring, "now");
333 strcpy(astring, "at");
334 if (aparm->mask & KTIME_DAY) {
335 strcat(astring, " ");
336 strcat(astring, day[aparm->day]);
338 if (aparm->mask & KTIME_HOUR) {
339 if (aparm->hour > 12)
340 sprintf(tempString, " %d", aparm->hour - 12);
341 else if (aparm->hour == 0)
342 strcpy(tempString, " 12");
344 sprintf(tempString, " %d", aparm->hour);
345 strcat(astring, tempString);
347 if (aparm->mask & KTIME_MIN) {
348 sprintf(tempString, ":%02d", aparm->min);
349 strcat(astring, tempString);
351 if ((aparm->mask & KTIME_SEC) && aparm->sec != 0) {
352 sprintf(tempString, ":%02d", aparm->sec);
353 strcat(astring, tempString);
355 if (aparm->mask & KTIME_HOUR) {
356 if (aparm->hour >= 12)
357 strcat(astring, " pm");
359 strcat(astring, " am");
365 /* get next time that matches ktime structure after time afrom */
367 ktime_next(struct ktime * aktime, afs_int32 afrom)
369 /* try by guessing from now */
371 time_t start; /* time to start looking */
372 time_t probe; /* a placeholder to use for advancing day to day */
373 time_t time_next; /* actual UTC time of probe, with time of day set */
375 struct ktime_date tdate;
377 start = afrom + time(0); /* time to start search */
378 tmask = aktime->mask;
380 /* handle some special cases */
381 if (tmask & KTIME_NEVER)
383 if (tmask & KTIME_NOW)
386 /* Use probe to fill in members of *tsp. Add 23 hours each iteration until
387 * time_next is correct. Only add 23 hrs to avoid skipping spring
388 * daylight savings time day */
389 for (probe = start;; probe += (23 * 3600)) {
390 tsp = localtime(&probe); /* find out what UTC time "probe" is */
392 tdate.year = tsp->tm_year;
393 tdate.month = tsp->tm_mon + 1;
394 tdate.day = tsp->tm_mday;
396 KTIMEDATE_YEAR | KTIMEDATE_MONTH | KTIMEDATE_DAY | KTIMEDATE_HOUR
397 | KTIMEDATE_MIN | KTIMEDATE_SEC;
398 tdate.hour = aktime->hour; /* edit in our changes */
399 tdate.min = aktime->min;
400 tdate.sec = aktime->sec;
401 time_next = ktime_InterpretDate(&tdate); /* Convert back to UTC time */
402 if (time_next < start)
403 continue; /* "probe" time is already past */
404 if ((tmask & KTIME_DAY) == 0) /* don't care about day, we're done */
406 tsp = localtime(&time_next);
407 if (tsp->tm_wday == aktime->day)
408 break; /* day matches, we're done */
414 /* compare date in both formats, and return as in strcmp */
417 KTimeCmp(struct ktime *aktime, struct tm *atm)
421 /* don't compare day of the week, since we can't tell the
422 * order in a cyclical set. Caller must check for equality, if
424 tmask = aktime->mask;
425 if (tmask & KTIME_HOUR) {
426 if (aktime->hour > atm->tm_hour)
428 if (aktime->hour < atm->tm_hour)
431 if (tmask & KTIME_MIN) {
432 if (aktime->min > atm->tm_min)
434 if (aktime->min < atm->tm_min)
437 if (tmask & KTIME_SEC) {
438 if (aktime->sec > atm->tm_sec)
440 if (aktime->sec < atm->tm_sec)
447 /* compare date in both formats, and return as in strcmp */
449 KDateCmp(struct ktime_date *akdate, struct tm *atm)
451 if (akdate->year > atm->tm_year)
453 if (akdate->year < atm->tm_year)
455 if (akdate->month > atm->tm_mon)
457 if (akdate->month < atm->tm_mon)
459 if (akdate->day > atm->tm_mday)
461 if (akdate->day < atm->tm_mday)
463 if (akdate->mask & KTIMEDATE_HOUR) {
464 if (akdate->hour > atm->tm_hour)
466 if (akdate->hour < atm->tm_hour)
469 if (akdate->mask & KTIMEDATE_MIN) {
470 if (akdate->min > atm->tm_min)
472 if (akdate->min < atm->tm_min)
475 if (akdate->mask & KTIMEDATE_SEC) {
476 if (akdate->sec > atm->tm_sec)
478 if (akdate->sec < atm->tm_sec)
485 * parse date string into ktime_date structure
487 * adate - string to be parsed
488 * akdate - ptr to ktime_date for result
490 * 0 - akdate contains converted date
491 * -1 - parsing failure
495 ktime_ParseDate(char *adate, struct ktime_date *akdate)
498 afs_int32 month, day2, year, hour, min, sec;
502 lcstring(never, adate, sizeof(never));
503 if (strcmp(never, "never") == 0)
504 akdate->mask = KTIMEDATE_NEVER;
505 else if (strcmp(never, "now") == 0)
506 akdate->mask = KTIMEDATE_NOW;
512 /* Old ambiguous mm/dd/yy hh:mm:ss format */
515 sscanf(adate, "%d / %d / %d %d : %d : %d%1s", &month, &day2, &year,
516 &hour, &min, &sec, &c[0]);
520 sscanf(adate, "%d / %d / %d %d : %d%1s", &month, &day2, &year,
525 sscanf(adate, "%d / %d / %d%1s", &month, &day2, &year, &c[0]);
532 /* New ISO 8601 (subset) format */
535 hour = min = sec = 0;
537 sscanf(adate, "%d-%d-%d %d:%d:%d%1s", &year, &month, &day2,
538 &hour, &min, &sec, c);
539 if (code != 3 && code != 5 && code != 6)
546 if ((year < 0) || (month < 1) || (month > 12) || (day2 < 1) || (day2 > 31) || /* more or less */
547 (hour < 0) || (hour > 23) || (min < 0) || (min > 59) || (sec < 0)
552 year += 100; /* make 1/1/1 => Jan 1, 2001 */
553 else if (year >= 1900)
554 year -= 1900; /* allow 1/1/2001 to work */
556 return -2; /* only allow 2 or 4 digit years */
559 KTIMEDATE_YEAR | KTIMEDATE_MONTH | KTIMEDATE_DAY | KTIMEDATE_HOUR |
560 KTIMEDATE_MIN | KTIMEDATE_SEC;
563 akdate->month = month;
569 /* done successfully */
574 * Converts a ktime date string into an afs_int32
576 * adate - ktime date string
577 * aint32 - ptr to afs_int32
579 * 0 - aint32 contains converted date.
583 ktime_DateToInt32(char *adate, afs_int32 * aint32)
585 struct ktime_date tdate;
590 if (sscanf(adate, "%lu%1s", &l, c) == 1 && l > 200000000)
593 /* parse the date into a ktime_date structure */
594 code = ktime_ParseDate(adate, &tdate);
596 return code; /* failed to parse */
597 *aint32 = ktime_InterpretDate(&tdate); /* interpret as seconds since 1970 */
603 /* get useful error message to print about date input format */
605 ktime_GetDateUsage(void)
607 return "date format is '(yyyy-mm-dd | mm/dd/yy) [hh:mm]', using a 24 hour clock";
611 /* ktime_InterpretDate
612 * Converts ktime_date to an afs_int32
614 * akdate - date to be converted/interpreted
616 * returns KTIMEDATE_NEVERDATE - if never flag was set, or
617 * date converted to afs_int32.
621 ktime_InterpretDate(struct ktime_date * akdate)
628 if (akdate->mask & KTIMEDATE_NOW)
630 if (akdate->mask & KTIMEDATE_NEVER)
631 return KTIMEDATE_NEVERDATE;
633 tbit = 1 << 30; /* start off at max signed result */
634 tresult = 0; /* result to return */
636 temp = tresult + tbit; /* see if adding this bit keeps us < akdate */
637 tsp = localtime(&temp);
640 if (tsp->tm_mon == 0) {
645 if (KDateCmp(akdate, tsp) >= 0) {
646 /* if temp still represents earlier than date than we're searching
647 * for, add in bit as increment, otherwise use old value and look
648 * for smaller increment */
651 tbit = tbit >> 1; /* try next bit */