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>
20 /* some date parsing routines */
27 static char *day[] = {
37 /* free token list returned by parseLine */
40 LocalFreeTokens(alist)
44 for (; alist; alist = nlist) {
56 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
63 LocalParseLine(char *aline, struct token **alist)
68 struct token *first, *last;
72 inToken = 0; /* not copying token chars at start */
77 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
79 inToken = 0; /* end of this token */
81 ttok = (struct token *)malloc(sizeof(struct token));
83 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
84 strcpy(ttok->key, tbuffer);
94 /* an alpha character */
99 if (tptr - tbuffer >= sizeof(tbuffer))
100 return -1; /* token too long */
104 /* last token flushed 'cause space(0) --> true */
113 /* keyword database for periodic date parsing */
114 static struct ptemp {
126 "sunday", 0x10000,}, {
127 "monday", 0x10001,}, {
128 "tuesday", 0x10002,}, {
129 "wednesday", 0x10003,}, {
130 "thursday", 0x10004,}, {
132 "friday", 0x10005,}, {
133 "saturday", 0x10006,}, {
143 * atime - time in seconds (Unix std)
145 * return value - ptr to time in text form. Ptr is to a static string.
149 ktime_DateOf(afs_int32 atime)
151 static char tbuffer[30];
157 tbuffer[24] = 0; /* get rid of new line */
159 strcpy(tbuffer, "BAD TIME");
164 * parse 12:33:12 or 12:33 or 12 into ktime structure
166 * astr - ptr to string to be parsed
167 * ak - ptr to struct for return value.
169 * 0 - ak holds parsed time.
170 * -1 - error in format
174 ParseTime(struct ktime *ak, char *astr)
181 field = 0; /* 0=hour, 1=min, 2=sec */
184 ak->mask |= (KTIME_HOUR | KTIME_MIN | KTIME_SEC);
187 if (tc == 0 || tc == ':') {
199 } else if (!isdigit(tc))
200 return -1; /* syntax error */
207 if (ak->hour >= 24 || ak->min >= 60 || ak->sec >= 60)
213 ktime_Str2int32(char *astr)
217 memset(&tk, 0, sizeof(tk));
218 if (ParseTime(&tk, astr))
219 return (-1); /* syntax error */
221 return ((tk.hour * 60 + tk.min) * 60 + tk.sec);
224 /* ktime_ParsePeriodic
225 * Parses periodic date of form
226 * now | never | at [time spec] | every [time spec]
227 * where [time spec] is a ktime string.
229 * adate - string to be parsed
230 * ak - ptr to structure for returned ktime
232 * 0 - parsed ktime in ak
233 * -1 - specification error
236 /* -1 means error, 0 means now, otherwise returns time of next event */
238 ktime_ParsePeriodic(char *adate, struct ktime *ak)
244 memset(ak, 0, sizeof(*ak));
245 code = LocalParseLine(adate, &tt);
248 for (; tt; tt = tt->next) {
249 /* look at each token */
250 if (strcmp(tt->key, "now") == 0) {
251 ak->mask |= KTIME_NOW;
254 if (strcmp(tt->key, "never") == 0) {
255 ak->mask |= KTIME_NEVER;
258 if (strcmp(tt->key, "at") == 0)
260 if (strcmp(tt->key, "every") == 0)
262 if (isdigit(tt->key[0])) {
264 code = ParseTime(ak, tt->key);
269 /* otherwise use keyword table */
270 for (tp = ptkeys;; tp++) {
271 if (tp->key == NULL) {
274 if (strcmp(tp->key, tt->key) == 0)
277 /* now look at tp->value to see what we've got */
278 if ((tp->value >> 16) == 1) {
280 ak->mask |= KTIME_DAY;
281 ak->day = tp->value & 0xff;
283 if ((tp->value >> 16) == 2) {
285 if ((tp->value & 0xff) == 1) {
287 if (!(ak->mask & KTIME_HOUR))
292 else if (ak->hour != 12)
295 /* am is almost a noop, except that we map 12:01 am to 0:01 */
306 /* ktime_DisplayString
307 * Display ktime structure as English that could go into the ktime parser
309 * aparm - ktime to be converted to string
310 * astring - ptr to string, for the result
312 * 0 - astring contains ktime string.
315 ktime_DisplayString(struct ktime *aparm, char *astring)
319 if (aparm->mask & KTIME_NEVER) {
320 strcpy(astring, "never");
322 } else if (aparm->mask & KTIME_NOW) {
323 strcpy(astring, "now");
326 strcpy(astring, "at");
327 if (aparm->mask & KTIME_DAY) {
328 strcat(astring, " ");
329 strcat(astring, day[aparm->day]);
331 if (aparm->mask & KTIME_HOUR) {
332 if (aparm->hour > 12)
333 sprintf(tempString, " %d", aparm->hour - 12);
334 else if (aparm->hour == 0)
335 strcpy(tempString, " 12");
337 sprintf(tempString, " %d", aparm->hour);
338 strcat(astring, tempString);
340 if (aparm->mask & KTIME_MIN) {
341 sprintf(tempString, ":%02d", aparm->min);
342 strcat(astring, tempString);
344 if ((aparm->mask & KTIME_SEC) && aparm->sec != 0) {
345 sprintf(tempString, ":%02d", aparm->sec);
346 strcat(astring, tempString);
348 if (aparm->mask & KTIME_HOUR) {
349 if (aparm->hour >= 12)
350 strcat(astring, " pm");
352 strcat(astring, " am");
358 /* get next time that matches ktime structure after time afrom */
360 ktime_next(struct ktime * aktime, afs_int32 afrom)
362 /* try by guessing from now */
364 time_t start; /* time to start looking */
365 time_t probe; /* a placeholder to use for advancing day to day */
366 time_t time_next; /* actual UTC time of probe, with time of day set */
368 struct ktime_date tdate;
370 start = afrom + time(0); /* time to start search */
371 tmask = aktime->mask;
373 /* handle some special cases */
374 if (tmask & KTIME_NEVER)
376 if (tmask & KTIME_NOW)
379 /* Use probe to fill in members of *tsp. Add 23 hours each iteration until
380 * time_next is correct. Only add 23 hrs to avoid skipping spring
381 * daylight savings time day */
382 for (probe = start;; probe += (23 * 3600)) {
383 tsp = localtime(&probe); /* find out what UTC time "probe" is */
385 tdate.year = tsp->tm_year;
386 tdate.month = tsp->tm_mon + 1;
387 tdate.day = tsp->tm_mday;
389 KTIMEDATE_YEAR | KTIMEDATE_MONTH | KTIMEDATE_DAY | KTIMEDATE_HOUR
390 | KTIMEDATE_MIN | KTIMEDATE_SEC;
391 tdate.hour = aktime->hour; /* edit in our changes */
392 tdate.min = aktime->min;
393 tdate.sec = aktime->sec;
394 time_next = ktime_InterpretDate(&tdate); /* Convert back to UTC time */
395 if (time_next < start)
396 continue; /* "probe" time is already past */
397 if ((tmask & KTIME_DAY) == 0) /* don't care about day, we're done */
399 tsp = localtime(&time_next);
400 if (tsp->tm_wday == aktime->day)
401 break; /* day matches, we're done */
407 /* compare date in both formats, and return as in strcmp */
410 KTimeCmp(struct ktime *aktime, struct tm *atm)
414 /* don't compare day of the week, since we can't tell the
415 * order in a cyclical set. Caller must check for equality, if
417 tmask = aktime->mask;
418 if (tmask & KTIME_HOUR) {
419 if (aktime->hour > atm->tm_hour)
421 if (aktime->hour < atm->tm_hour)
424 if (tmask & KTIME_MIN) {
425 if (aktime->min > atm->tm_min)
427 if (aktime->min < atm->tm_min)
430 if (tmask & KTIME_SEC) {
431 if (aktime->sec > atm->tm_sec)
433 if (aktime->sec < atm->tm_sec)
440 /* compare date in both formats, and return as in strcmp */
442 KDateCmp(struct ktime_date *akdate, struct tm *atm)
444 if (akdate->year > atm->tm_year)
446 if (akdate->year < atm->tm_year)
448 if (akdate->month > atm->tm_mon)
450 if (akdate->month < atm->tm_mon)
452 if (akdate->day > atm->tm_mday)
454 if (akdate->day < atm->tm_mday)
456 if (akdate->mask & KTIMEDATE_HOUR) {
457 if (akdate->hour > atm->tm_hour)
459 if (akdate->hour < atm->tm_hour)
462 if (akdate->mask & KTIMEDATE_MIN) {
463 if (akdate->min > atm->tm_min)
465 if (akdate->min < atm->tm_min)
468 if (akdate->mask & KTIMEDATE_SEC) {
469 if (akdate->sec > atm->tm_sec)
471 if (akdate->sec < atm->tm_sec)
478 * parse date string into ktime_date structure
480 * adate - string to be parsed
481 * akdate - ptr to ktime_date for result
483 * 0 - akdate contains converted date
484 * -1 - parsing failure
488 ktime_ParseDate(char *adate, struct ktime_date *akdate)
491 afs_int32 month, day2, year, hour, min, sec;
495 lcstring(never, adate, sizeof(never));
496 if (strcmp(never, "never") == 0)
497 akdate->mask = KTIMEDATE_NEVER;
498 else if (strcmp(never, "now") == 0)
499 akdate->mask = KTIMEDATE_NOW;
505 /* Old ambiguous mm/dd/yy hh:mm:ss format */
508 sscanf(adate, "%d / %d / %d %d : %d : %d%1s", &month, &day2, &year,
509 &hour, &min, &sec, &c[0]);
513 sscanf(adate, "%d / %d / %d %d : %d%1s", &month, &day2, &year,
518 sscanf(adate, "%d / %d / %d%1s", &month, &day2, &year, &c[0]);
525 /* New ISO 8601 (subset) format */
528 hour = min = sec = 0;
530 sscanf(adate, "%d-%d-%d %d:%d:%d%1s", &year, &month, &day2,
531 &hour, &min, &sec, c);
532 if (code != 3 && code != 5 && code != 6)
539 if ((year < 0) || (month < 1) || (month > 12) || (day2 < 1) || (day2 > 31) || /* more or less */
540 (hour < 0) || (hour > 23) || (min < 0) || (min > 59) || (sec < 0)
545 year += 100; /* make 1/1/1 => Jan 1, 2001 */
546 else if (year >= 1900)
547 year -= 1900; /* allow 1/1/2001 to work */
549 return -2; /* only allow 2 or 4 digit years */
552 KTIMEDATE_YEAR | KTIMEDATE_MONTH | KTIMEDATE_DAY | KTIMEDATE_HOUR |
553 KTIMEDATE_MIN | KTIMEDATE_SEC;
556 akdate->month = month;
562 /* done successfully */
567 * Converts a ktime date string into an afs_int32
569 * adate - ktime date string
570 * aint32 - ptr to afs_int32
572 * 0 - aint32 contains converted date.
576 ktime_DateToInt32(char *adate, afs_int32 * aint32)
578 struct ktime_date tdate;
583 if (sscanf(adate, "%lu%1s", &l, c) == 1 && l > 200000000)
586 /* parse the date into a ktime_date structure */
587 code = ktime_ParseDate(adate, &tdate);
589 return code; /* failed to parse */
590 *aint32 = ktime_InterpretDate(&tdate); /* interpret as seconds since 1970 */
596 /* get useful error message to print about date input format */
598 ktime_GetDateUsage(void)
600 return "date format is '(yyyy-mm-dd | mm/dd/yy) [hh:mm]', using a 24 hour clock";
604 /* ktime_InterpretDate
605 * Converts ktime_date to an afs_int32
607 * akdate - date to be converted/interpreted
609 * returns KTIMEDATE_NEVERDATE - if never flag was set, or
610 * date converted to afs_int32.
614 ktime_InterpretDate(struct ktime_date * akdate)
621 if (akdate->mask & KTIMEDATE_NOW)
623 if (akdate->mask & KTIMEDATE_NEVER)
624 return KTIMEDATE_NEVERDATE;
626 tbit = 1 << 30; /* start off at max signed result */
627 tresult = 0; /* result to return */
629 temp = tresult + tbit; /* see if adding this bit keeps us < akdate */
630 tsp = localtime(&temp);
633 if (tsp->tm_mon == 0) {
638 if (KDateCmp(akdate, tsp) >= 0) {
639 /* if temp still represents earlier than date than we're searching
640 * for, add in bit as increment, otherwise use old value and look
641 * for smaller increment */
644 tbit = tbit >> 1; /* try next bit */