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>
22 /* some date parsing routines */
29 static char *day[] = {
39 /* free token list returned by parseLine */
42 LocalFreeTokens(alist)
46 for (; alist; alist = nlist) {
58 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
65 LocalParseLine(char *aline, struct token **alist)
70 struct token *first, *last;
74 inToken = 0; /* not copying token chars at start */
79 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
81 inToken = 0; /* end of this token */
83 ttok = (struct token *)malloc(sizeof(struct token));
85 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
86 strcpy(ttok->key, tbuffer);
96 /* an alpha character */
101 if (tptr - tbuffer >= sizeof(tbuffer))
102 return -1; /* token too long */
106 /* last token flushed 'cause space(0) --> true */
115 /* keyword database for periodic date parsing */
116 static struct ptemp {
128 "sunday", 0x10000,}, {
129 "monday", 0x10001,}, {
130 "tuesday", 0x10002,}, {
131 "wednesday", 0x10003,}, {
132 "thursday", 0x10004,}, {
134 "friday", 0x10005,}, {
135 "saturday", 0x10006,}, {
145 * atime - time in seconds (Unix std)
147 * return value - ptr to time in text form. Ptr is to a static string.
151 ktime_DateOf(afs_int32 atime)
153 static char tbuffer[30];
159 tbuffer[24] = 0; /* get rid of new line */
161 strcpy(tbuffer, "BAD TIME");
166 * parse 12:33:12 or 12:33 or 12 into ktime structure
168 * astr - ptr to string to be parsed
169 * ak - ptr to struct for return value.
171 * 0 - ak holds parsed time.
172 * -1 - error in format
176 ParseTime(struct ktime *ak, char *astr)
183 field = 0; /* 0=hour, 1=min, 2=sec */
186 ak->mask |= (KTIME_HOUR | KTIME_MIN | KTIME_SEC);
189 if (tc == 0 || tc == ':') {
201 } else if (!isdigit(tc))
202 return -1; /* syntax error */
209 if (ak->hour >= 24 || ak->min >= 60 || ak->sec >= 60)
215 ktime_Str2int32(char *astr)
219 memset(&tk, 0, sizeof(tk));
220 if (ParseTime(&tk, astr))
221 return (-1); /* syntax error */
223 return ((tk.hour * 60 + tk.min) * 60 + tk.sec);
226 /* ktime_ParsePeriodic
227 * Parses periodic date of form
228 * now | never | at [time spec] | every [time spec]
229 * where [time spec] is a ktime string.
231 * adate - string to be parsed
232 * ak - ptr to structure for returned ktime
234 * 0 - parsed ktime in ak
235 * -1 - specification error
238 /* -1 means error, 0 means now, otherwise returns time of next event */
240 ktime_ParsePeriodic(char *adate, struct ktime *ak)
246 memset(ak, 0, sizeof(*ak));
247 code = LocalParseLine(adate, &tt);
250 for (; tt; tt = tt->next) {
251 /* look at each token */
252 if (strcmp(tt->key, "now") == 0) {
253 ak->mask |= KTIME_NOW;
256 if (strcmp(tt->key, "never") == 0) {
257 ak->mask |= KTIME_NEVER;
260 if (strcmp(tt->key, "at") == 0)
262 if (strcmp(tt->key, "every") == 0)
264 if (isdigit(tt->key[0])) {
266 code = ParseTime(ak, tt->key);
271 /* otherwise use keyword table */
272 for (tp = ptkeys;; tp++) {
273 if (tp->key == NULL) {
276 if (strcmp(tp->key, tt->key) == 0)
279 /* now look at tp->value to see what we've got */
280 if ((tp->value >> 16) == 1) {
282 ak->mask |= KTIME_DAY;
283 ak->day = tp->value & 0xff;
285 if ((tp->value >> 16) == 2) {
287 if ((tp->value & 0xff) == 1) {
289 if (!(ak->mask & KTIME_HOUR))
294 else if (ak->hour != 12)
297 /* am is almost a noop, except that we map 12:01 am to 0:01 */
308 /* ktime_DisplayString
309 * Display ktime structure as English that could go into the ktime parser
311 * aparm - ktime to be converted to string
312 * astring - ptr to string, for the result
314 * 0 - astring contains ktime string.
317 ktime_DisplayString(struct ktime *aparm, char *astring)
321 if (aparm->mask & KTIME_NEVER) {
322 strcpy(astring, "never");
324 } else if (aparm->mask & KTIME_NOW) {
325 strcpy(astring, "now");
328 strcpy(astring, "at");
329 if (aparm->mask & KTIME_DAY) {
330 strcat(astring, " ");
331 strcat(astring, day[aparm->day]);
333 if (aparm->mask & KTIME_HOUR) {
334 if (aparm->hour > 12)
335 sprintf(tempString, " %d", aparm->hour - 12);
336 else if (aparm->hour == 0)
337 strcpy(tempString, " 12");
339 sprintf(tempString, " %d", aparm->hour);
340 strcat(astring, tempString);
342 if (aparm->mask & KTIME_MIN) {
343 sprintf(tempString, ":%02d", aparm->min);
344 strcat(astring, tempString);
346 if ((aparm->mask & KTIME_SEC) && aparm->sec != 0) {
347 sprintf(tempString, ":%02d", aparm->sec);
348 strcat(astring, tempString);
350 if (aparm->mask & KTIME_HOUR) {
351 if (aparm->hour >= 12)
352 strcat(astring, " pm");
354 strcat(astring, " am");
360 /* get next time that matches ktime structure after time afrom */
362 ktime_next(struct ktime * aktime, afs_int32 afrom)
364 /* try by guessing from now */
366 time_t start; /* time to start looking */
367 time_t probe; /* a placeholder to use for advancing day to day */
368 time_t time_next; /* actual UTC time of probe, with time of day set */
370 struct ktime_date tdate;
372 start = afrom + time(0); /* time to start search */
373 tmask = aktime->mask;
375 /* handle some special cases */
376 if (tmask & KTIME_NEVER)
378 if (tmask & KTIME_NOW)
381 /* Use probe to fill in members of *tsp. Add 23 hours each iteration until
382 * time_next is correct. Only add 23 hrs to avoid skipping spring
383 * daylight savings time day */
384 for (probe = start;; probe += (23 * 3600)) {
385 tsp = localtime(&probe); /* find out what UTC time "probe" is */
387 tdate.year = tsp->tm_year;
388 tdate.month = tsp->tm_mon + 1;
389 tdate.day = tsp->tm_mday;
391 KTIMEDATE_YEAR | KTIMEDATE_MONTH | KTIMEDATE_DAY | KTIMEDATE_HOUR
392 | KTIMEDATE_MIN | KTIMEDATE_SEC;
393 tdate.hour = aktime->hour; /* edit in our changes */
394 tdate.min = aktime->min;
395 tdate.sec = aktime->sec;
396 time_next = ktime_InterpretDate(&tdate); /* Convert back to UTC time */
397 if (time_next < start)
398 continue; /* "probe" time is already past */
399 if ((tmask & KTIME_DAY) == 0) /* don't care about day, we're done */
401 tsp = localtime(&time_next);
402 if (tsp->tm_wday == aktime->day)
403 break; /* day matches, we're done */
409 /* compare date in both formats, and return as in strcmp */
412 KTimeCmp(struct ktime *aktime, struct tm *atm)
416 /* don't compare day of the week, since we can't tell the
417 * order in a cyclical set. Caller must check for equality, if
419 tmask = aktime->mask;
420 if (tmask & KTIME_HOUR) {
421 if (aktime->hour > atm->tm_hour)
423 if (aktime->hour < atm->tm_hour)
426 if (tmask & KTIME_MIN) {
427 if (aktime->min > atm->tm_min)
429 if (aktime->min < atm->tm_min)
432 if (tmask & KTIME_SEC) {
433 if (aktime->sec > atm->tm_sec)
435 if (aktime->sec < atm->tm_sec)
442 /* compare date in both formats, and return as in strcmp */
444 KDateCmp(struct ktime_date *akdate, struct tm *atm)
446 if (akdate->year > atm->tm_year)
448 if (akdate->year < atm->tm_year)
450 if (akdate->month > atm->tm_mon)
452 if (akdate->month < atm->tm_mon)
454 if (akdate->day > atm->tm_mday)
456 if (akdate->day < atm->tm_mday)
458 if (akdate->mask & KTIMEDATE_HOUR) {
459 if (akdate->hour > atm->tm_hour)
461 if (akdate->hour < atm->tm_hour)
464 if (akdate->mask & KTIMEDATE_MIN) {
465 if (akdate->min > atm->tm_min)
467 if (akdate->min < atm->tm_min)
470 if (akdate->mask & KTIMEDATE_SEC) {
471 if (akdate->sec > atm->tm_sec)
473 if (akdate->sec < atm->tm_sec)
480 * parse date string into ktime_date structure
482 * adate - string to be parsed
483 * akdate - ptr to ktime_date for result
485 * 0 - akdate contains converted date
486 * -1 - parsing failure
490 ktime_ParseDate(char *adate, struct ktime_date *akdate)
493 afs_int32 month, day2, year, hour, min, sec;
497 lcstring(never, adate, sizeof(never));
498 if (strcmp(never, "never") == 0)
499 akdate->mask = KTIMEDATE_NEVER;
500 else if (strcmp(never, "now") == 0)
501 akdate->mask = KTIMEDATE_NOW;
507 /* Old ambiguous mm/dd/yy hh:mm:ss format */
510 sscanf(adate, "%d / %d / %d %d : %d : %d%1s", &month, &day2, &year,
511 &hour, &min, &sec, &c[0]);
515 sscanf(adate, "%d / %d / %d %d : %d%1s", &month, &day2, &year,
520 sscanf(adate, "%d / %d / %d%1s", &month, &day2, &year, &c[0]);
527 /* New ISO 8601 (subset) format */
530 hour = min = sec = 0;
532 sscanf(adate, "%d-%d-%d %d:%d:%d%1s", &year, &month, &day2,
533 &hour, &min, &sec, c);
534 if (code != 3 && code != 5 && code != 6)
541 if ((year < 0) || (month < 1) || (month > 12) || (day2 < 1) || (day2 > 31) || /* more or less */
542 (hour < 0) || (hour > 23) || (min < 0) || (min > 59) || (sec < 0)
547 year += 100; /* make 1/1/1 => Jan 1, 2001 */
548 else if (year >= 1900)
549 year -= 1900; /* allow 1/1/2001 to work */
551 return -2; /* only allow 2 or 4 digit years */
554 KTIMEDATE_YEAR | KTIMEDATE_MONTH | KTIMEDATE_DAY | KTIMEDATE_HOUR |
555 KTIMEDATE_MIN | KTIMEDATE_SEC;
558 akdate->month = month;
564 /* done successfully */
569 * Converts a ktime date string into an afs_int32
571 * adate - ktime date string
572 * aint32 - ptr to afs_int32
574 * 0 - aint32 contains converted date.
578 ktime_DateToInt32(char *adate, afs_int32 * aint32)
580 struct ktime_date tdate;
585 if (sscanf(adate, "%lu%1s", &l, c) == 1 && l > 200000000)
588 /* parse the date into a ktime_date structure */
589 code = ktime_ParseDate(adate, &tdate);
591 return code; /* failed to parse */
592 *aint32 = ktime_InterpretDate(&tdate); /* interpret as seconds since 1970 */
598 /* get useful error message to print about date input format */
600 ktime_GetDateUsage(void)
602 return "date format is '(yyyy-mm-dd | mm/dd/yy) [hh:mm]', using a 24 hour clock";
606 /* ktime_InterpretDate
607 * Converts ktime_date to an afs_int32
609 * akdate - date to be converted/interpreted
611 * returns KTIMEDATE_NEVERDATE - if never flag was set, or
612 * date converted to afs_int32.
616 ktime_InterpretDate(struct ktime_date * akdate)
623 if (akdate->mask & KTIMEDATE_NOW)
625 if (akdate->mask & KTIMEDATE_NEVER)
626 return KTIMEDATE_NEVERDATE;
628 tbit = 1 << 30; /* start off at max signed result */
629 tresult = 0; /* result to return */
631 temp = tresult + tbit; /* see if adding this bit keeps us < akdate */
632 tsp = localtime(&temp);
635 if (tsp->tm_mon == 0) {
640 if (KDateCmp(akdate, tsp) >= 0) {
641 /* if temp still represents earlier than date than we're searching
642 * for, add in bit as increment, otherwise use old value and look
643 * for smaller increment */
646 tbit = tbit >> 1; /* try next bit */