[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.23 and 1.37

version 1.23, 2010/04/28 18:20:15 version 1.37, 2019/08/12 20:03:28
Line 39 
Line 39 
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
 #include <time.h>  #include <time.h>
 #include <tzfile.h>  
   
 #include "pathnames.h"  #include "pathnames.h"
 #include "calendar.h"  #include "calendar.h"
Line 78 
Line 77 
 static struct fixs fnmonths[13];      /* full national months names */  static struct fixs fnmonths[13];      /* full national months names */
 static struct fixs nmonths[13];       /* short national month names */  static struct fixs nmonths[13];       /* short national month names */
   
   void
   fill_print_date(struct match *m, struct tm *tm)
   {
           if (strftime(m->print_date, sizeof(m->print_date),
               daynames ? "%a %b %d" : "%b %d", tm) == 0)
                   m->print_date[sizeof(m->print_date) - 1] = '\0';
   }
   
 void  void
 setnnames(void)  setnnames(void)
Line 89 
Line 95 
         for (i = 0; i < 7; i++) {          for (i = 0; i < 7; i++) {
                 tm.tm_wday = i;                  tm.tm_wday = i;
                 l = strftime(buf, sizeof(buf), "%a", &tm);                  l = strftime(buf, sizeof(buf), "%a", &tm);
                 for (; l > 0 && isspace((int)buf[l - 1]); l--)                  for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--)
                         ;                          ;
                 buf[l] = '\0';                  buf[l] = '\0';
                 if (ndays[i].name != NULL)                  free(ndays[i].name);
                         free(ndays[i].name);  
                 if ((ndays[i].name = strdup(buf)) == NULL)                  if ((ndays[i].name = strdup(buf)) == NULL)
                         err(1, NULL);                          err(1, NULL);
                 ndays[i].len = strlen(buf);                  ndays[i].len = strlen(buf);
   
                 l = strftime(buf, sizeof(buf), "%A", &tm);                  l = strftime(buf, sizeof(buf), "%A", &tm);
                 for (; l > 0 && isspace((int)buf[l - 1]); l--)                  for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--)
                         ;                          ;
                 buf[l] = '\0';                  buf[l] = '\0';
                 if (fndays[i].name != NULL)                  free(fndays[i].name);
                         free(fndays[i].name);  
                 if ((fndays[i].name = strdup(buf)) == NULL)                  if ((fndays[i].name = strdup(buf)) == NULL)
                         err(1, NULL);                          err(1, NULL);
                 fndays[i].len = strlen(buf);                  fndays[i].len = strlen(buf);
Line 112 
Line 116 
         for (i = 0; i < 12; i++) {          for (i = 0; i < 12; i++) {
                 tm.tm_mon = i;                  tm.tm_mon = i;
                 l = strftime(buf, sizeof(buf), "%b", &tm);                  l = strftime(buf, sizeof(buf), "%b", &tm);
                 for (; l > 0 && isspace((int)buf[l - 1]); l--)                  for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--)
                         ;                          ;
                 buf[l] = '\0';                  buf[l] = '\0';
                 if (nmonths[i].name != NULL)                  free(nmonths[i].name);
                         free(nmonths[i].name);  
                 if ((nmonths[i].name = strdup(buf)) == NULL)                  if ((nmonths[i].name = strdup(buf)) == NULL)
                         err(1, NULL);                          err(1, NULL);
                 nmonths[i].len = strlen(buf);                  nmonths[i].len = strlen(buf);
   
                 l = strftime(buf, sizeof(buf), "%B", &tm);                  l = strftime(buf, sizeof(buf), "%B", &tm);
                 for (; l > 0 && isspace((int)buf[l - 1]); l--)                  for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--)
                         ;                          ;
                 buf[l] = '\0';                  buf[l] = '\0';
                 if (fnmonths[i].name != NULL)                  free(fnmonths[i].name);
                         free(fnmonths[i].name);  
                 if ((fnmonths[i].name = strdup(buf)) == NULL)                  if ((fnmonths[i].name = strdup(buf)) == NULL)
                         err(1, NULL);                          err(1, NULL);
                 fnmonths[i].len = strlen(buf);                  fnmonths[i].len = strlen(buf);
Line 158 
Line 160 
         tp->tm_isdst = 0;          tp->tm_isdst = 0;
         tp->tm_hour = 12;          tp->tm_hour = 12;
         *now = mktime(tp);          *now = mktime(tp);
         if (isleap(tp->tm_year + TM_YEAR_BASE))          if (isleap(tp->tm_year + 1900))
                 cumdays = daytab[1];                  cumdays = daytab[1];
         else          else
                 cumdays = daytab[0];                  cumdays = daytab[0];
         /* Friday displays Monday's events */          /* Friday displays Monday's events */
         offset = tp->tm_wday == 5 ? 3 : 1;          offset = tp->tm_wday == 5 ? 3 : 1;
         if (f_SetdayAfter)          if (f_Setday)
                 offset = 0;     /* Except not when range is set explicitly */                  offset = 0;     /* Except not when range is set explicitly */
         header[5].iov_base = dayname;          header[5].iov_base = dayname;
   
Line 191 
Line 193 
         len = strlen(date);          len = strlen(date);
         if (len < 2)          if (len < 2)
                 return((time_t)-1);                  return((time_t)-1);
           bzero(&tm, sizeof tm);
         tm.tm_sec = 0;          tm.tm_sec = 0;
         tm.tm_min = 0;          tm.tm_min = 0;
         /* Avoid getting caught by a timezone shift; set time to noon */          /* Avoid getting caught by a timezone shift; set time to noon */
Line 215 
Line 218 
                 *(date + len - 4) = '\0';                  *(date + len - 4) = '\0';
                 tm.tm_year = atoi(date);                  tm.tm_year = atoi(date);
   
                 /* tm_year up TM_YEAR_BASE ... */  
                 if (tm.tm_year < 69)            /* Y2K */                  if (tm.tm_year < 69)            /* Y2K */
                         tm.tm_year += 2000 - TM_YEAR_BASE;                          tm.tm_year += 100;
                 else if (tm.tm_year < 100)                  else if (tm.tm_year > 1900)
                         tm.tm_year += 1900 - TM_YEAR_BASE;                          tm.tm_year -= 1900;
                 else if (tm.tm_year > TM_YEAR_BASE)  
                         tm.tm_year -= TM_YEAR_BASE;  
         }          }
   
 #if DEBUG  #if DEBUG
         printf("Mktime: %d %d %d %s\n", (int)mktime(&tm), (int)t, len,          printf("Mktime: %d %lld %d %s\n", (int)mktime(&tm), (long long)t, len,
             asctime(&tm));              asctime(&tm));
 #endif  #endif
         return(mktime(&tm));          return(mktime(&tm));
 }  }
   
 void  static void
 adjust_calendar(int *day, int *month)  adjust_calendar(int *day, int *month)
 {  {
         switch (calendar) {          switch (calendar) {
Line 290 
Line 290 
         /* adjust bodun rate */          /* adjust bodun rate */
         if (bodun && !bodun_always)          if (bodun && !bodun_always)
                 bodun = !arc4random_uniform(3);                  bodun = !arc4random_uniform(3);
   
         /* Easter or Easter depending days */          /* Easter or Easter depending days */
         if (flags & F_SPECIAL)          if (flags & F_SPECIAL)
                 vwd = v1;                  vwd = v1;
Line 323 
Line 323 
                 if (month == -1) {                  if (month == -1) {
                         month = tp->tm_mon + 1;                          month = tp->tm_mon + 1;
                         interval = MONTHLY;                          interval = MONTHLY;
                 } else if (calendar)                  } else {
                         adjust_calendar(&day, &month);                          if ((month > 12) || (month < 1))
                 if ((month > 12) || (month < 1))                                  return (NULL);
                         return (NULL);                          if (calendar)
                                   adjust_calendar(&day, &month);
                   }
         }          }
   
         /* 2. {Monthname} XYZ ... */          /* 2. {Monthname} XYZ ... */
Line 371 
Line 373 
                 else {                  else {
                         /* F_ISDAY set, v2 > 12, or no way to tell */                          /* F_ISDAY set, v2 > 12, or no way to tell */
                         month = v1;                          month = v1;
                           if ((month > 12) || (month < 1))
                                   return (NULL);
                         /* if no recognizable day, assume the first */                          /* if no recognizable day, assume the first */
                         day = v2 ? v2 : 1;                          day = v2 ? v2 : 1;
                         if ((flags & F_ISDAY)) {                          if ((flags & F_ISDAY)) {
Line 431 
Line 435 
                          */                           */
                                 if (tp->tm_yday > 300 && tmtmp.tm_mon <= 1)                                  if (tp->tm_yday > 300 && tmtmp.tm_mon <= 1)
                                         variable_weekday(&vwd, tmtmp.tm_mon + 1,                                          variable_weekday(&vwd, tmtmp.tm_mon + 1,
                                             tmtmp.tm_year + TM_YEAR_BASE + 1);                                              tmtmp.tm_year + 1900 + 1);
                                 else                                  else
                                         variable_weekday(&vwd, tmtmp.tm_mon + 1,                                          variable_weekday(&vwd, tmtmp.tm_mon + 1,
                                             tmtmp.tm_year + TM_YEAR_BASE);                                              tmtmp.tm_year + 1900);
                                 day = cumdays[tmtmp.tm_mon + 1] + vwd;                                  day = cumdays[tmtmp.tm_mon + 1] + vwd;
                                 tmtmp.tm_mday = vwd;                                  tmtmp.tm_mday = vwd;
                         }                          }
                         v2 = day - tp->tm_yday;                          v2 = day - tp->tm_yday;
                         if ((v2 > v1) || (v2 < 0)) {                          if ((v2 > v1) || (v2 < 0)) {
                                 if ((v2 += isleap(tp->tm_year + TM_YEAR_BASE) ? 366 : 365)                                  if ((v2 += isleap(tp->tm_year + 1900) ? 366 : 365)
                                     <= v1)                                      <= v1)
                                         tmtmp.tm_year++;                                          tmtmp.tm_year++;
                                 else if(!bodun || (day - tp->tm_yday) != -1)                                  else if(!bodun || (day - tp->tm_yday) != -1)
Line 459 
Line 463 
                         }                          }
   
                         (void)mktime(&tmtmp);                          (void)mktime(&tmtmp);
                         if (strftime(tmp->print_date,                          fill_print_date(tmp, &tmtmp);
                             sizeof(tmp->print_date),  
                         /*    "%a %b %d", &tm);  Skip weekdays */  
                             "%b %d", &tmtmp) == 0)  
                                 tmp->print_date[sizeof(tmp->print_date) - 1] = '\0';  
   
                         tmp->var   = varp;                          tmp->var   = varp;
                         tmp->next  = NULL;                          tmp->next  = NULL;
                         return(tmp);                          return(tmp);
Line 516 
Line 515 
                                 if (vwd) {                                  if (vwd) {
                                         v1 = vwd;                                          v1 = vwd;
                                         variable_weekday(&v1, tmtmp.tm_mon + 1,                                          variable_weekday(&v1, tmtmp.tm_mon + 1,
                                             tmtmp.tm_year + TM_YEAR_BASE);                                              tmtmp.tm_year + 1900);
                                         tmtmp.tm_mday = v1;                                          tmtmp.tm_mday = v1;
                                 } else                                  } else
                                         tmtmp.tm_mday = dayp;                                          tmtmp.tm_mday = dayp;
Line 527 
Line 526 
                                 if (flags & F_SPECIAL) {                                  if (flags & F_SPECIAL) {
                                         tmtmp.tm_mon = 0;       /* Gee, mktime() is nice */                                          tmtmp.tm_mon = 0;       /* Gee, mktime() is nice */
                                         tmtmp.tm_mday = spev[v1].getev(tmtmp.tm_year +                                          tmtmp.tm_mday = spev[v1].getev(tmtmp.tm_year +
                                             TM_YEAR_BASE) + vwd;                                              1900) + vwd;
                                 } else if (vwd) {                                  } else if (vwd) {
                                         v1 = vwd;                                          v1 = vwd;
                                         variable_weekday(&v1, tmtmp.tm_mon + 1,                                          variable_weekday(&v1, tmtmp.tm_mon + 1,
                                             tmtmp.tm_year + TM_YEAR_BASE);                                              tmtmp.tm_year + 1900);
                                         tmtmp.tm_mday = v1;                                          tmtmp.tm_mday = v1;
                                 } else {                                  } else {
                                 /* Need the following to keep Feb 29 from                                  /* Need the following to keep Feb 29 from
Line 548 
Line 547 
                                 tdiff = difftime(ttmp, f_time)/ SECSPERDAY;                                  tdiff = difftime(ttmp, f_time)/ SECSPERDAY;
                                 if (tdiff <= offset + f_dayAfter ||                                  if (tdiff <= offset + f_dayAfter ||
                                     (bodun && tdiff == -1)) {                                      (bodun && tdiff == -1)) {
                                         if (tdiff >=  0 ||                                          if (((tmtmp.tm_mon == month) ||
                                             (bodun && tdiff == -1)) {                                               (flags & F_SPECIAL) ||
                                                (interval == WEEKLY)) &&
                                               (tdiff >=  0 ||
                                               (bodun && tdiff == -1))) {
                                         if ((tmp = malloc(sizeof(struct match))) == NULL)                                          if ((tmp = malloc(sizeof(struct match))) == NULL)
                                                 err(1, NULL);                                                  err(1, NULL);
                                         tmp->when = ttmp;                                          tmp->when = ttmp;
                                         if (strftime(tmp->print_date,                                          fill_print_date(tmp, &tmtmp);
                                             sizeof(tmp->print_date),  
                                         /*    "%a %b %d", &tm);  Skip weekdays */  
                                             "%b %d", &tmtmp) == 0)  
                                                 tmp->print_date[sizeof(tmp->print_date) - 1] = '\0';  
                                         tmp->bodun = bodun && tdiff == -1;                                          tmp->bodun = bodun && tdiff == -1;
                                         tmp->var   = varp;                                          tmp->var   = varp;
                                         tmp->next  = NULL;                                          tmp->next  = NULL;

Legend:
Removed from v.1.23  
changed lines
  Added in v.1.37