[Python-checkins] python/nondist/sandbox/datetime datetime.c,1.19,1.20

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Thu, 21 Nov 2002 15:12:59 -0800


Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv5497

Modified Files:
	datetime.c 
Log Message:
ord_to_ymd():  Take out the days-in-{4,100,400}-years calculations, and
capture their values in macros.  It wasn't worth it in the Python version
of the code, because that got to exploit that the calls were done only
once per Python session (at module init time).  In C, though, the calls
were getting done on every call of ord_to_ymd.


Index: datetime.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.c,v
retrieving revision 1.19
retrieving revision 1.20
diff -C2 -d -r1.19 -r1.20
*** datetime.c	21 Nov 2002 23:06:40 -0000	1.19
--- datetime.c	21 Nov 2002 23:12:56 -0000	1.20
***************
*** 124,147 ****
  }
  
  /* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */
  static void
  ord_to_ymd(long    ordinal, long *year, long *month, long *day)
  {
- 	int di400y = days_before_year(401);
- 	int di100y = days_before_year(101);
- 	int di4y = days_before_year(5);
  	int n, n1, n4, n100, n400, leapyear, preceding;
  
  	assert(ordinal >= 1);
  	--ordinal;
! 	n400 = ordinal / di400y;
! 	n = ordinal % di400y;
  	*year = n400 * 400 + 1;
  
! 	n100 = n / di100y;
! 	n = n % di100y;
  
! 	n4 = n / di4y;
! 	n = n % di4y;
  
  	n1 = n / 365;
--- 124,148 ----
  }
  
+ #define DI4Y	1461	/* days_before_year(5); days in 4 years */
+ #define DI100Y	36524	/* days_before_year(101); days in 100 years */
+ #define DI400Y	146097	/* days_before_year(401); days in 400 years  */
+ 
  /* ordinal -> year, month, day, considering 01-Jan-0001 as day 1. */
  static void
  ord_to_ymd(long    ordinal, long *year, long *month, long *day)
  {
  	int n, n1, n4, n100, n400, leapyear, preceding;
  
  	assert(ordinal >= 1);
  	--ordinal;
! 	n400 = ordinal / DI400Y;
! 	n = ordinal % DI400Y;
  	*year = n400 * 400 + 1;
  
! 	n100 = n / DI100Y;
! 	n = n % DI100Y;
  
! 	n4 = n / DI4Y;
! 	n = n % DI4Y;
  
  	n1 = n / 365;