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>
21 /* some date parsing routines */
28 static char *day[] = {
38 /* free token list returned by parseLine */
41 LocalFreeTokens(alist)
45 for (; alist; alist = nlist) {
57 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
64 LocalParseLine(char *aline, struct token **alist)
69 struct token *first, *last;
73 inToken = 0; /* not copying token chars at start */
78 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
80 inToken = 0; /* end of this token */
82 ttok = (struct token *)malloc(sizeof(struct token));
84 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
85 strcpy(ttok->key, tbuffer);
95 /* an alpha character */
100 if (tptr - tbuffer >= sizeof(tbuffer))
101 return -1; /* token too long */
105 /* last token flushed 'cause space(0) --> true */
114 /* keyword database for periodic date parsing */
115 static struct ptemp {
127 "sunday", 0x10000,}, {
128 "monday", 0x10001,}, {
129 "tuesday", 0x10002,}, {
130 "wednesday", 0x10003,}, {
131 "thursday", 0x10004,}, {
133 "friday", 0x10005,}, {
134 "saturday", 0x10006,}, {
144 * atime - time in seconds (Unix std)
146 * return value - ptr to time in text form. Ptr is to a static string.
150 ktime_DateOf(afs_int32 atime)
152 static char tbuffer[30];
158 tbuffer[24] = 0; /* get rid of new line */
160 strcpy(tbuffer, "BAD TIME");
165 * parse 12:33:12 or 12:33 or 12 into ktime structure
167 * astr - ptr to string to be parsed
168 * ak - ptr to struct for return value.
170 * 0 - ak holds parsed time.
171 * -1 - error in format
175 ParseTime(struct ktime *ak, char *astr)
182 field = 0; /* 0=hour, 1=min, 2=sec */
185 ak->mask |= (KTIME_HOUR | KTIME_MIN | KTIME_SEC);
188 if (tc == 0 || tc == ':') {
200 } else if (!isdigit(tc))
201 return -1; /* syntax error */
208 if (ak->hour >= 24 || ak->min >= 60 || ak->sec >= 60)
214 ktime_Str2int32(char *astr)
218 memset(&tk, 0, sizeof(tk));
219 if (ParseTime(&tk, astr))
220 return (-1); /* syntax error */
222 return ((tk.hour * 60 + tk.min) * 60 + tk.sec);
225 /* ktime_ParsePeriodic
226 * Parses periodic date of form
227 * now | never | at [time spec] | every [time spec]
228 * where [time spec] is a ktime string.
230 * adate - string to be parsed
231 * ak - ptr to structure for returned ktime
233 * 0 - parsed ktime in ak
234 * -1 - specification error
237 /* -1 means error, 0 means now, otherwise returns time of next event */
239 ktime_ParsePeriodic(char *adate, struct ktime *ak)
245 memset(ak, 0, sizeof(*ak));
246 code = LocalParseLine(adate, &tt);
249 for (; tt; tt = tt->next) {
250 /* look at each token */
251 if (strcmp(tt->key, "now") == 0) {
252 ak->mask |= KTIME_NOW;
255 if (strcmp(tt->key, "never") == 0) {
256 ak->mask |= KTIME_NEVER;
259 if (strcmp(tt->key, "at") == 0)
261 if (strcmp(tt->key, "every") == 0)
263 if (isdigit(tt->key[0])) {
265 code = ParseTime(ak, tt->key);
270 /* otherwise use keyword table */
271 for (tp = ptkeys;; tp++) {
272 if (tp->key == NULL) {
275 if (strcmp(tp->key, tt->key) == 0)
278 /* now look at tp->value to see what we've got */
279 if ((tp->value >> 16) == 1) {
281 ak->mask |= KTIME_DAY;
282 ak->day = tp->value & 0xff;
284 if ((tp->value >> 16) == 2) {
286 if ((tp->value & 0xff) == 1) {
288 if (!(ak->mask & KTIME_HOUR))
293 else if (ak->hour != 12)
296 /* am is almost a noop, except that we map 12:01 am to 0:01 */
307 /* ktime_DisplayString
308 * Display ktime structure as English that could go into the ktime parser
310 * aparm - ktime to be converted to string
311 * astring - ptr to string, for the result
313 * 0 - astring contains ktime string.
316 ktime_DisplayString(struct ktime *aparm, char *astring)
320 if (aparm->mask & KTIME_NEVER) {
321 strcpy(astring, "never");
323 } else if (aparm->mask & KTIME_NOW) {
324 strcpy(astring, "now");
327 strcpy(astring, "at");
328 if (aparm->mask & KTIME_DAY) {
329 strcat(astring, " ");
330 strcat(astring, day[aparm->day]);
332 if (aparm->mask & KTIME_HOUR) {
333 if (aparm->hour > 12)
334 sprintf(tempString, " %d", aparm->hour - 12);
335 else if (aparm->hour == 0)
336 strcpy(tempString, " 12");
338 sprintf(tempString, " %d", aparm->hour);
339 strcat(astring, tempString);
341 if (aparm->mask & KTIME_MIN) {
342 sprintf(tempString, ":%02d", aparm->min);
343 strcat(astring, tempString);
345 if ((aparm->mask & KTIME_SEC) && aparm->sec != 0) {
346 sprintf(tempString, ":%02d", aparm->sec);
347 strcat(astring, tempString);
349 if (aparm->mask & KTIME_HOUR) {
350 if (aparm->hour >= 12)
351 strcat(astring, " pm");
353 strcat(astring, " am");
359 /* get next time that matches ktime structure after time afrom */
361 ktime_next(struct ktime * aktime, afs_int32 afrom)
363 /* try by guessing from now */
365 time_t start; /* time to start looking */
366 time_t probe; /* a placeholder to use for advancing day to day */
367 time_t time_next; /* actual UTC time of probe, with time of day set */
369 struct ktime_date tdate;
371 start = afrom + time(0); /* time to start search */
372 tmask = aktime->mask;
374 /* handle some special cases */
375 if (tmask & KTIME_NEVER)
377 if (tmask & KTIME_NOW)
380 /* Use probe to fill in members of *tsp. Add 23 hours each iteration until
381 * time_next is correct. Only add 23 hrs to avoid skipping spring
382 * daylight savings time day */
383 for (probe = start;; probe += (23 * 3600)) {
384 tsp = localtime(&probe); /* find out what UTC time "probe" is */
386 tdate.year = tsp->tm_year;
387 tdate.month = tsp->tm_mon + 1;
388 tdate.day = tsp->tm_mday;
390 KTIMEDATE_YEAR | KTIMEDATE_MONTH | KTIMEDATE_DAY | KTIMEDATE_HOUR
391 | KTIMEDATE_MIN | KTIMEDATE_SEC;
392 tdate.hour = aktime->hour; /* edit in our changes */
393 tdate.min = aktime->min;
394 tdate.sec = aktime->sec;
395 time_next = ktime_InterpretDate(&tdate); /* Convert back to UTC time */
396 if (time_next < start)
397 continue; /* "probe" time is already past */
398 if ((tmask & KTIME_DAY) == 0) /* don't care about day, we're done */
400 tsp = localtime(&time_next);
401 if (tsp->tm_wday == aktime->day)
402 break; /* day matches, we're done */
408 /* compare date in both formats, and return as in strcmp */
411 KTimeCmp(struct ktime *aktime, struct tm *atm)
415 /* don't compare day of the week, since we can't tell the
416 * order in a cyclical set. Caller must check for equality, if
418 tmask = aktime->mask;
419 if (tmask & KTIME_HOUR) {
420 if (aktime->hour > atm->tm_hour)
422 if (aktime->hour < atm->tm_hour)
425 if (tmask & KTIME_MIN) {
426 if (aktime->min > atm->tm_min)
428 if (aktime->min < atm->tm_min)
431 if (tmask & KTIME_SEC) {
432 if (aktime->sec > atm->tm_sec)
434 if (aktime->sec < atm->tm_sec)
441 /* compare date in both formats, and return as in strcmp */
443 KDateCmp(struct ktime_date *akdate, struct tm *atm)
445 if (akdate->year > atm->tm_year)
447 if (akdate->year < atm->tm_year)
449 if (akdate->month > atm->tm_mon)
451 if (akdate->month < atm->tm_mon)
453 if (akdate->day > atm->tm_mday)
455 if (akdate->day < atm->tm_mday)
457 if (akdate->mask & KTIMEDATE_HOUR) {
458 if (akdate->hour > atm->tm_hour)
460 if (akdate->hour < atm->tm_hour)
463 if (akdate->mask & KTIMEDATE_MIN) {
464 if (akdate->min > atm->tm_min)
466 if (akdate->min < atm->tm_min)
469 if (akdate->mask & KTIMEDATE_SEC) {
470 if (akdate->sec > atm->tm_sec)
472 if (akdate->sec < atm->tm_sec)
479 * parse date string into ktime_date structure
481 * adate - string to be parsed
482 * akdate - ptr to ktime_date for result
484 * 0 - akdate contains converted date
485 * -1 - parsing failure
489 ktime_ParseDate(char *adate, struct ktime_date *akdate)
492 afs_int32 month, day2, year, hour, min, sec;
496 lcstring(never, adate, sizeof(never));
497 if (strcmp(never, "never") == 0)
498 akdate->mask = KTIMEDATE_NEVER;
499 else if (strcmp(never, "now") == 0)
500 akdate->mask = KTIMEDATE_NOW;
506 /* Old ambiguous mm/dd/yy hh:mm:ss format */
509 sscanf(adate, "%d / %d / %d %d : %d : %d%1s", &month, &day2, &year,
510 &hour, &min, &sec, &c[0]);
514 sscanf(adate, "%d / %d / %d %d : %d%1s", &month, &day2, &year,
519 sscanf(adate, "%d / %d / %d%1s", &month, &day2, &year, &c[0]);
526 /* New ISO 8601 (subset) format */
529 hour = min = sec = 0;
531 sscanf(adate, "%d-%d-%d %d:%d:%d%1s", &year, &month, &day2,
532 &hour, &min, &sec, c);
533 if (code != 3 && code != 5 && code != 6)
540 if ((year < 0) || (month < 1) || (month > 12) || (day2 < 1) || (day2 > 31) || /* more or less */
541 (hour < 0) || (hour > 23) || (min < 0) || (min > 59) || (sec < 0)
546 year += 100; /* make 1/1/1 => Jan 1, 2001 */
547 else if (year >= 1900)
548 year -= 1900; /* allow 1/1/2001 to work */
550 return -2; /* only allow 2 or 4 digit years */
553 KTIMEDATE_YEAR | KTIMEDATE_MONTH | KTIMEDATE_DAY | KTIMEDATE_HOUR |
554 KTIMEDATE_MIN | KTIMEDATE_SEC;
557 akdate->month = month;
563 /* done successfully */
568 * Converts a ktime date string into an afs_int32
570 * adate - ktime date string
571 * aint32 - ptr to afs_int32
573 * 0 - aint32 contains converted date.
577 ktime_DateToInt32(char *adate, afs_int32 * aint32)
579 struct ktime_date tdate;
584 if (sscanf(adate, "%lu%1s", &l, c) == 1 && l > 200000000)
587 /* parse the date into a ktime_date structure */
588 code = ktime_ParseDate(adate, &tdate);
590 return code; /* failed to parse */
591 *aint32 = ktime_InterpretDate(&tdate); /* interpret as seconds since 1970 */
597 /* get useful error message to print about date input format */
599 ktime_GetDateUsage(void)
601 return "date format is '(yyyy-mm-dd | mm/dd/yy) [hh:mm]', using a 24 hour clock";
605 /* ktime_InterpretDate
606 * Converts ktime_date to an afs_int32
608 * akdate - date to be converted/interpreted
610 * returns KTIMEDATE_NEVERDATE - if never flag was set, or
611 * date converted to afs_int32.
615 ktime_InterpretDate(struct ktime_date * akdate)
622 if (akdate->mask & KTIMEDATE_NOW)
624 if (akdate->mask & KTIMEDATE_NEVER)
625 return KTIMEDATE_NEVERDATE;
627 tbit = 1 << 30; /* start off at max signed result */
628 tresult = 0; /* result to return */
630 temp = tresult + tbit; /* see if adding this bit keeps us < akdate */
631 tsp = localtime(&temp);
634 if (tsp->tm_mon == 0) {
639 if (KDateCmp(akdate, tsp) >= 0) {
640 /* if temp still represents earlier than date than we're searching
641 * for, add in bit as increment, otherwise use old value and look
642 * for smaller increment */
645 tbit = tbit >> 1; /* try next bit */