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 03:44:28 GMT
Date: 2001-06-27T03:44:28+00:00	[thread overview]
Message-ID: <3B395724.6B4B9B67@worldnet.att.net> (raw)
In-Reply-To: 9hbb9n$g14$1@fang.dsto.defence.gov.au

Vladimir Bednikov wrote:
> 
> Hi all,
> 
> I'm using gnat3.13p on an sgi with gcc2.8.1 and am wondering if there is a
> way of
> getting the day of week and month in text format. I.E.
> Mon, Tue, Wed, Thu, Fri, Sat and Sun for days of the week and
> Jan, Feb, Mar, Apr .... for the month of the year.

Ada does not provide such conversions as part of the Ada.Calendar
package. Which representation for days of the week and month names
is appropriate in all situations and cultures?

Ada does allow you to calculate the results yourself.

Converting month numbers to month names is simple. Create an enumerated
type containing the month names you want, then convert the 
Ada.Calendar.Month_Number to the corresponding month name:

type months is (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep,
                Oct, Nov, Dec);
Month_Name : Months := Months'Val(Ada.Calendar.Month(Time_Value));

In this case Time_Value is a variable of type Ada.Calendar.Time.

Converting to the names of the days of the week is a bit more work.
The following generic package works for all the dates I have tested.
Note that it expects an enumerated type for the generic parameter.
The function Day_Of_Week will work only when the first day of the
week is a representation of Sunday

-----------------------------------------------------------------------
-- 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 )
	                - (Year_Diff / 100) + (Year_Diff / 400) 
			+ Natural(Month_Days(The_Month)) 
			+ Natural(The_Day) - 1);
	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;

Jim Rogers
Colorado Springs, Colorado USA



  reply	other threads:[~2001-06-27  3:44 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 [this message]
2001-06-27  8:32   ` Peter Hermann
2001-06-27 13:01     ` James Rogers
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