[BACK]Return to day.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / calendar

Diff for /src/usr.bin/calendar/day.c between version 1.1 and 1.6

version 1.1, 1996/12/05 06:04:39 version 1.6, 1998/11/08 04:31:13
Line 97 
Line 97 
   
         for (i = 0; i < 7; i++) {          for (i = 0; i < 7; i++) {
                 tm.tm_wday = i;                  tm.tm_wday = i;
                 strftime(buf, sizeof(buf), "%a", &tm);                  l = strftime(buf, sizeof(buf), "%a", &tm);
                 for (l = strlen(buf);                  for (; l > 0 && isspace((int)buf[l - 1]); l--)
                      l > 0 && isspace((int)buf[l - 1]);  
                      l--)  
                         ;                          ;
                 buf[l] = '\0';                  buf[l] = '\0';
                 if (ndays[i].name != NULL)                  if (ndays[i].name != NULL)
Line 109 
Line 107 
                         errx(1, "cannot allocate memory");                          errx(1, "cannot allocate memory");
                 ndays[i].len = strlen(buf);                  ndays[i].len = strlen(buf);
   
                 strftime(buf, sizeof(buf), "%A", &tm);                  l = strftime(buf, sizeof(buf), "%A", &tm);
                 for (l = strlen(buf);                  for (; l > 0 && isspace((int)buf[l - 1]); l--)
                      l > 0 && isspace((int)buf[l - 1]);  
                      l--)  
                         ;                          ;
                 buf[l] = '\0';                  buf[l] = '\0';
                 if (fndays[i].name != NULL)                  if (fndays[i].name != NULL)
Line 124 
Line 120 
   
         for (i = 0; i < 12; i++) {          for (i = 0; i < 12; i++) {
                 tm.tm_mon = i;                  tm.tm_mon = i;
                 strftime(buf, sizeof(buf), "%b", &tm);                  l = strftime(buf, sizeof(buf), "%b", &tm);
                 for (l = strlen(buf);                  for (; l > 0 && isspace((int)buf[l - 1]); l--)
                      l > 0 && isspace((int)buf[l - 1]);  
                      l--)  
                         ;                          ;
                 buf[l] = '\0';                  buf[l] = '\0';
                 if (nmonths[i].name != NULL)                  if (nmonths[i].name != NULL)
Line 136 
Line 130 
                         errx(1, "cannot allocate memory");                          errx(1, "cannot allocate memory");
                 nmonths[i].len = strlen(buf);                  nmonths[i].len = strlen(buf);
   
                 strftime(buf, sizeof(buf), "%B", &tm);                  l = strftime(buf, sizeof(buf), "%B", &tm);
                 for (l = strlen(buf);                  for (; l > 0 && isspace((int)buf[l - 1]); l--)
                      l > 0 && isspace((int)buf[l - 1]);  
                      l--)  
                         ;                          ;
                 buf[l] = '\0';                  buf[l] = '\0';
                 if (fnmonths[i].name != NULL)                  if (fnmonths[i].name != NULL)
Line 155 
Line 147 
         time_t now;          time_t now;
 {  {
         tp = localtime(&now);          tp = localtime(&now);
         if (isleap(tp->tm_year + 1900)) {          if (isleap(tp->tm_year + TM_YEAR_BASE)) {
                 yrdays = DAYSPERLYEAR;                  yrdays = DAYSPERLYEAR;
                 cumdays = daytab[1];                  cumdays = daytab[1];
         } else {          } else {
Line 173 
Line 165 
         setnnames();          setnnames();
 }  }
   
 /* convert Day[/Month][/Year] into unix time (since 1970)  /* convert [Year][Month]Day into unix time (since 1970)
  * Day: two digits, Month: two digits, Year: digits   * Year: two or four digits, Month: two digits, Day: two digits
  */   */
 time_t Mktime (date)  time_t Mktime (date)
     char *date;      char *date;
Line 187 
Line 179 
     tp = localtime(&t);      tp = localtime(&t);
   
     len = strlen(date);      len = strlen(date);
       if (len < 2)
           return((time_t)-1);
     tm.tm_sec = 0;      tm.tm_sec = 0;
     tm.tm_min = 0;      tm.tm_min = 0;
     tm.tm_hour = 0;      /* Avoid getting caught by a timezone shift; set time to noon */
       tm.tm_isdst = 0;
       tm.tm_hour = 12;
     tm.tm_wday = 0;      tm.tm_wday = 0;
     tm.tm_mday = tp->tm_mday;      tm.tm_mday = tp->tm_mday;
     tm.tm_mon = tp->tm_mon;      tm.tm_mon = tp->tm_mon;
     tm.tm_year = tp->tm_year;      tm.tm_year = tp->tm_year;
   
       /* Day */
       tm.tm_mday = atoi(date + len - 2);
   
     /* day */      /* Month */
     *(date+2) = NULL;  
     tm.tm_mday = atoi(date);  
   
     /* month */  
     if (len >= 4) {      if (len >= 4) {
         *(date+5) = NULL;          *(date + len - 2) = '\0';
         tm.tm_mon = atoi(date+3) - 1;          tm.tm_mon = atoi(date + len - 4) - 1;
     }      }
   
     /* Year */      /* Year */
     if (len >= 7) {      if (len >= 6) {
         tm.tm_year = atoi(date+6);          *(date + len - 4) = '\0';
           tm.tm_year = atoi(date);
   
         /* tm_year up 1900 ... */          /* tm_year up TM_YEAR_BASE ... */
         if (tm.tm_year > 1900)          if (tm.tm_year < 69)            /* Y2K */
             tm.tm_year -= 1900;                  tm.tm_year += 2000 - TM_YEAR_BASE;
           else if (tm.tm_year < 100)
                   tm.tm_year += 1900 - TM_YEAR_BASE;
           else if (tm.tm_year > TM_YEAR_BASE)
                   tm.tm_year -= TM_YEAR_BASE;
     }      }
   
 #if DEBUG  #if DEBUG
Line 232 
Line 231 
  * following a line that is matched, that starts with "whitespace", is shown   * following a line that is matched, that starts with "whitespace", is shown
  * along with the matched line.   * along with the matched line.
  */   */
 int  struct match *
 isnow(endp, monthp, dayp, varp)  isnow(endp)
         char    *endp;          char    *endp;
         int     *monthp;  
         int     *dayp;  
         int     *varp;  
 {  {
         int day, flags = 0, month = 0, v1, v2;          int day, flags = 0, month = 0, v1, v2;
           int monthp, dayp, varp;
           struct match *matches;
   
         /*          /*
          * CONVENTION           * CONVENTION
Line 254 
Line 252 
         /* read first field */          /* read first field */
         /* didn't recognize anything, skip it */          /* didn't recognize anything, skip it */
         if (!(v1 = getfield(endp, &endp, &flags)))          if (!(v1 = getfield(endp, &endp, &flags)))
                 return (0);                  return (NULL);
   
         /* Easter or Easter depending days */          /* Easter or Easter depending days */
         if (flags & F_EASTER)          if (flags & F_EASTER)
Line 298 
Line 296 
                 if (flags & F_ISMONTH) {                  if (flags & F_ISMONTH) {
                         day = v1;                          day = v1;
                         month = v2;                          month = v2;
                         *varp = 0;                          varp = 0;
                 }                  }
   
                 /* {Month} {Weekday,Day} ...  */                  /* {Month} {Weekday,Day} ...  */
Line 307 
Line 305 
                         month = v1;                          month = v1;
                         /* if no recognizable day, assume the first */                          /* if no recognizable day, assume the first */
                         day = v2 ? v2 : 1;                          day = v2 ? v2 : 1;
                         *varp = 0;                          varp = 0;
                 }                  }
         }          }
   
         /* convert Weekday into *next*  Day,          /* convert Weekday into *next*  Day,
          * e.g.: 'Sunday' -> 22           * e.g.: 'Sunday' -> 22
          *       'SunayLast' -> ??           *       'SundayLast' -> ??
          */           */
         if (flags & F_ISDAY) {          if (flags & F_ISDAY) {
 #if DEBUG  #if DEBUG
             fprintf(stderr, "\nday: %d %s month %d\n", day, endp, month);              fprintf(stderr, "\nday: %d %s month %d\n", day, endp, month);
 #endif  #endif
   
             *varp = 1;              varp = 1;
             /* variable weekday, SundayLast, MondayFirst ... */              /* variable weekday, SundayLast, MondayFirst ... */
             if (day < 0 || day >= 10) {              if (day < 0 || day >= 10) {
   
                 /* negative offset; last, -4 .. -1 */                  /* negative offset; last, -4 .. -1 */
                 if (day < 0) {                  if (day < 0) {
                     v1 = day/10 - 1;          /* offset -4 ... -1 */                      v1 = day/10 - 1;          /* offset -4 ... -1 */
                     day = 10 + (day % 10);    /* day 1 ... 7 */                      day = 10 + (day % 10);    /* day 1 ... 7 */
   
                     /* day, eg '22th' */                      /* which weekday the end of the month is (1-7) */
                     v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);                      v2 = (cumdays[month + 1] - tp->tm_yday +
                           tp->tm_wday + 371) % 7 + 1;
   
                     /* (month length - day) / 7 + 1 */                      /* and subtract enough days */
                     if (((int)((cumdays[month+1] -                      day = cumdays[month + 1] - cumdays[month] +
                                cumdays[month] - v2) / 7) + 1) == -v1)                          (v1 + 1) * 7 - (v2 - day + 7) % 7;
                         /* bingo ! */  #if DEBUG
                         day = v2;                      fprintf(stderr, "\nMonth %d ends on weekday %d\n", month, v2);
   #endif
                     /* set to yesterday */  
                     else  
                         day = tp->tm_mday - 1;  
                 }                  }
   
                 /* first, second ... +1 ... +5 */                  /* first, second ... +1 ... +5 */
                 else {                  else {
                     v1 = day/10;        /* offset: +1 (first Sunday) ... */                      v1 = day/10;        /* offset */
                     day = day % 10;                      day = day % 10;
   
                     /* day, eg '22th' */                      /* which weekday the first of the month is */
                     v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);                      v2 = (cumdays[month] - tp->tm_yday +
                           tp->tm_wday + 372) % 7 + 1;
                     /* Hurrah! matched */  
                     if ( ((v2 - 1 + 7) / 7) == v1 )                      /* and add enough days */
                         day = v2;                      day = 1 + (v1 - 1) * 7 + (day - v2 + 7) % 7;
   #if DEBUG
                     /* set to yesterday */                      fprintf(stderr, "\nMonth %d starts on weekday %d\n", month, v2);
                     else  #endif
                         day = tp->tm_mday - 1;  
                 }  
             }              }
   
             /* wired */  
             else {  
                 day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);  
                 *varp = 1;  
             }              }
               else
                       day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
         }          }
   
         if (!(flags & F_EASTER)) {          if (!(flags & F_EASTER)) {
             *monthp = month;              monthp = month;
             *dayp = day;              dayp = day;
             day = cumdays[month] + day;              day = cumdays[month] + day;
         }          }
         else {          else {
             for (v1 = 0; day > cumdays[v1]; v1++)              for (v1 = 0; day > cumdays[v1]; v1++)
                 ;                  ;
             *monthp = v1 - 1;              monthp = v1 - 1;
             *dayp = day - cumdays[v1 - 1];              dayp = day - cumdays[v1 - 1];
             *varp = 1;              varp = 1;
         }          }
   
 #if DEBUG  #if DEBUG
         fprintf(stderr, "day2: day %d(%d) yday %d\n", *dayp, day, tp->tm_yday);          fprintf(stderr, "day2: day %d(%d) yday %d\n", dayp, day, tp->tm_yday);
 #endif  #endif
         /* if today or today + offset days */          /* if today or today + offset days */
         if (day >= tp->tm_yday - f_dayBefore &&          if ((day >= tp->tm_yday - f_dayBefore &&
             day <= tp->tm_yday + offset + f_dayAfter)              day <= tp->tm_yday + offset + f_dayAfter) ||
                 return (1);  
   
         /* if number of days left in this year + days to event in next year */          /* if number of days left in this year + days to event in next year */
         if (yrdays - tp->tm_yday + day <= offset + f_dayAfter ||             (yrdays - tp->tm_yday + day <= offset + f_dayAfter ||
             /* a year backward, eg. 6 Jan and 10 days before -> 27. Dec */              /* a year backward, eg. 6 Jan and 10 days before -> 27. Dec */
             tp->tm_yday + day - f_dayBefore < 0              tp->tm_yday + day - f_dayBefore < 0
             )              )) {
                 return (1);                  if ((matches = malloc(sizeof(struct match))) == NULL)
         return (0);                          errx(1,"cannot allocate memory");
                   matches->month = monthp;
                   matches->day   = dayp;
                   matches->var   = varp;
                   matches->year  = tp->tm_year;   /* XXX */
                   matches->next  = NULL;
                   return (matches);
           }
           return (NULL);
 }  }
   
   

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.6