[comp.sys.sun] more on calentool 1.4 bug

mkhaw@teknowledge-vaxc.arpa (Mike Khaw) (01/16/89)

This is a followup to a previous message about calentool 1.4 getting
"today's date" wrong as of Jan 1, 1989.

Further investigation shows that the problem is confusion between time
beginning at 1/1/85 (the origin for the year_days[] array in utils.c) and
time beginning at 1/1/87 (the origin for the ymw_array[] initialized in
utils.c).  Calentool uses year_days[] to calculate days since the
beginning of time and uses the result to index into the ymw_array to get
year, month, date, day-of-week.

The get_today() function in utils.c uses year_days[] with an attempted
correction for time originating at 1/1/87, but the correction is not valid
for all subsequent years, as some sample manual calculations will show.

The simple fix is to make year_days begin at 1/1/87.

The following set of diffs contains that fix, as well as 2 bug fixes.

The first was documented as a bug in the README files (selecting a
week-at-a-glance display from a month display when the first day of the
month is a Sunday gives you the preceding week, for any week in that
month), and required only a simple "if" statement.

The second is an undocumented bug that is only partially corrected in this
patch:  The man file claims that selecting from the "Year Menu" changes
the display to the same month in a different year, but the code actually
changes the display to the January of the selected year.  The change in
this patch almost makes it work right except when the "current day"
selected is the first of the month in March through December in a non-leap
year and you select a leap-year from the "Year Menu" ... you get the
preceding month in the selected leap-year.

[[ Originally I wasn't going to post this patch, but it contains other bug
fixes besides the one for the leap-year botch.  I have it on good
authority that version 2.1 will be ready for distribution "real soon now".
--wnl ]]

<--- cut here --->
*** notify.c-	Thu Jan  5 18:29:41 1989
--- notify.c	Thu Jan  5 18:30:01 1989
***************
*** 114,123 ****
  struct inputevent *event;
  {
  	int old_state;

  	if (mainsw_state == DISPLAYING_DAY) 
                  close_day(); 
!         current_day = year_days[value];
  	old_state = mainsw_state;
          mainsw_state = DISPLAYING_MONTH; 
          draw_month(current_day); 
--- 114,129 ----
  struct inputevent *event;
  {
  	int old_state;
+ 	int	year_index, day_of_year;

+ 	for (year_index = 0; year_days[year_index + 1] < current_day;
+ 	  ++year_index)
+ 		;
+ 	day_of_year = current_day - year_days[year_index];
+ 
  	if (mainsw_state == DISPLAYING_DAY) 
                  close_day(); 
!         current_day = year_days[value] + day_of_year;
  	old_state = mainsw_state;
          mainsw_state = DISPLAYING_MONTH; 
          draw_month(current_day); 
*** selected.c-	Thu Jan  5 18:30:37 1989
--- selected.c	Thu Jan  5 18:30:46 1989
***************
*** 224,229 ****
--- 224,231 ----
  			new_day = current_day;			/* Compute Monday of selected week. */
  			while (ymw_array[new_day-1].month == ymw_array[current_day].month)
  				new_day--;
+ 			if (ymw_array[new_day].dow == 0)
+ 				++new_day;
  			while (ymw_array[new_day].dow > 1)
  				new_day--;
  			current_day = new_day + 7 * week_index;
*** utils.c-	Thu Jan  5 18:28:20 1989
--- utils.c	Thu Jan  5 18:28:53 1989
***************
*** 90,97 ****

  /* cummulative number of days since Jan 1, 1985 by years to 1999 */
  /* (takes into account leap years in 1988, 1992, 1996) */
! int year_days[] = {0, 365, 730, 1096, 1461, 1826, 2191, 2557, 2922,
! 			3287, 3652, 4018, 4383, 4748, 5113};

  get_today()
  {
--- 90,97 ----

  /* cummulative number of days since Jan 1, 1985 by years to 1999 */
  /* (takes into account leap years in 1988, 1992, 1996) */
! int year_days[] = {0, 365, 731, 1096, 1461, 1826, 2192, 2557, 2922,
! 			3287, 3653, 4018, 4383, 4748, 5114};

  get_today()
  {
***************
*** 101,107 ****

  	gettimeofday(&tv, 0);
  	tm = localtime(&tv.tv_sec);
! 	t = tm->tm_yday + year_days[tm->tm_year-85] - year_days[START_YEAR-85] - 1;
  	return(t);
  }

--- 101,107 ----

  	gettimeofday(&tv, 0);
  	tm = localtime(&tv.tv_sec);
! 	t = tm->tm_yday + year_days[tm->tm_year-START_YEAR];
  	return(t);
  }

<--- cut here --->

Mike Khaw
-- 
internet: mkhaw@teknowledge.arpa
uucp:	  {uunet|sun|ucbvax|decwrl|ames|hplabs}!mkhaw%teknowledge.arpa
hardcopy: Teknowledge Inc, 1850 Embarcadero Rd, POB 10119, Palo Alto, CA 94303