comp.lang.ada
 help / color / mirror / Atom feed
From: James Rogers <jimmaureenrogers@worldnet.att.net>
Subject: Re: Ada95 calendar package question
Date: Wed, 27 Jun 2001 13:01:46 GMT
Date: 2001-06-27T13:01:46+00:00	[thread overview]
Message-ID: <3B39D9C1.9B00424E@worldnet.att.net> (raw)
In-Reply-To: 9hc5n4$jl$1@infosun2.rus.uni-stuttgart.de


Peter Hermann wrote:
> 
> James Rogers <jimmaureenrogers@worldnet.att.net> wrote:
> > Vladimir Bednikov wrote:
> >> Mon, Tue, Wed, Thu, Fri, Sat and Sun for days of the week and
> >> Jan, Feb, Mar, Apr .... for the month of the year.
> > -- This generic package works when Sunday is declared to be the
> > -- first day of the week. It must be the first value of the
> 
> Sorry James, but:
> I very much prefer Monday as the first day of the week,
> simply because it is the first day of the week  :-)

Ah, more cultural differences. American calendars usually start the
week with Sunday. There are two solutions to the problem.
Simply alter the algorithm to add one to the number generated before
calculating the enumeration value. Follow that up by using the
updated version listed below. Upon further testing I found the
calculation had a Year 2000 defect in it.

I translated the original formulas from an ancient C text. Those
formulas (formulae ?) were only good through 1999.

-----------------------------------------------------------------------
-- Day of the Week Package for any date after January 1, 1901
--
-- This generic package works when Sunday is declared to be the
-- first day of the week. It must be the first value of the 
-- enumerated type used in the actual generic parameter.
-----------------------------------------------------------------------
with Ada.Calendar;

generic
type weekdays is (<>);
package Day_Conversion is
   function Day_Of_Week (Date : Ada.Calendar.Time) return weekdays;
end Day_Conversion;

-----------------------------------------------------------------------
-- Day of the Week Package for any date after January 1, 1901
-----------------------------------------------------------------------

package body Day_Conversion is
function Day_Of_Week (Date : Ada.Calendar.Time) return weekdays is
	year_diff : Natural;
	The_Month : Ada.Calendar.Month_Number;
	The_Year  : Ada.Calendar.Year_Number;
	The_Day   : Ada.Calendar.Day_Number;
	The_Day_Number : Natural := 0;
	Month_Days : array(Ada.Calendar.Month_Number) of Natural := 
	    (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 );
begin
	The_Month := Ada.Calendar.Month(Date);
	The_Year := Ada.Calendar.Year(Date);
	The_Day  := Ada.Calendar.Day(Date);
	Year_Diff := The_Year - 1901;
	The_Day_Number := (Year_Diff * 365) + (( Year_Diff / 4 )
			+ Natural(Month_Days(The_Month)) 
			+ Natural(The_Day) - 1);

	if The_Year >= 2000 then
		The_Day_Number := The_Day_Number - ((The_Year - 2000)/ 100);
		The_Day_Number := The_Day_Number + ((The_Year - 2000) / 400);  
	end if;  
	if (The_Year mod 4 = 0) and (The_Month > 2) then
	   The_Day_Number := The_Day_Number + 1;
	end if;
	return Weekdays'Val((The_Day_Number + 2) mod 7);
end Day_Of_Week;
end Day_Conversion;

I apologize for the original error.

Jim Rogers
Colorado Springs, Colorado USA



  reply	other threads:[~2001-06-27 13:01 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-27  0:54 Ada95 calendar package question Vladimir Bednikov
2001-06-27  3:44 ` James Rogers
2001-06-27  8:32   ` Peter Hermann
2001-06-27 13:01     ` James Rogers [this message]
2001-06-27 14:01       ` Wes Groleau
2001-06-27 13:24     ` Marin David Condic
2001-06-27 13:59       ` Wes Groleau
2001-06-27 14:36         ` Marin David Condic
2001-06-27 15:17           ` Ted Dennison
2001-06-27 15:43           ` Frank
2001-06-27 16:07             ` Aron Felix Gurski
2001-06-27 15:59           ` Philip Anderson
2001-06-27 16:20             ` Ted Dennison
2001-06-27 17:39             ` M. A. Alves
2001-06-27 18:23             ` Wes Groleau
2001-06-27 14:38         ` Noé Lavallée
2001-06-27 14:32       ` Ian Wild
2001-06-27 22:37       ` Ehud Lamm
2001-06-28 13:17         ` Marin David Condic
2001-06-28 14:33           ` Wes Groleau
2001-06-28 20:38             ` Ehud Lamm
2001-06-28 11:27       ` Alfred Hilscher
2001-06-28 14:17         ` Ted Dennison
2001-06-28 14:49           ` Marin David Condic
2001-06-28 16:28             ` M. A. Alves
2001-06-27 14:56   ` Larry Kilgallen
2001-06-27 13:14 ` Marin David Condic
2001-06-27 14:24 ` Samuel T. Harris
2001-06-28  1:57 ` Jeffrey Carter
     [not found] <Pine.LNX.4.21.0106271731050.8027-100000@borba.ncc.up.pt>
2001-06-27 16:44 ` David C. Hoos
2001-06-28  8:29   ` Philip Anderson
  -- strict thread matches above, loose matches on Subject: below --
2001-06-27 20:39 Beard, Frank
2001-06-27 21:25 ` Larry Hazel
2001-06-27 21:55 Beard, Frank
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox