comp.lang.ada
 help / color / mirror / Atom feed
From: Nick Roberts <nick.roberts@acm.org>
Subject: Re: ada calendar
Date: Tue, 09 Dec 2003 04:48:10 +0000
Date: 2003-12-09T04:48:10+00:00	[thread overview]
Message-ID: <br3k6i$2797k0$1@ID-25716.news.uni-berlin.de> (raw)
In-Reply-To: <uoeuj5d35.fsf@wanadoo.fr>

Pascal Obry wrote:

> Marius Amado Alves <amado.alves@netcabo.pt> writes:
> 
>>You can do it with Ada.Calendar:
>>
>>  Your_Function (Time_Of
>>    (Year_Number'Value (S (7 .. 10)),
>>     Month_Number'Value (S (4 .. 5)),
>>     Day_Number'Value (S (1 .. 2))));
>>
>>where S is your string containing "27/06/2003" or similar.
> 
> There is no guarantee that S start to 1. It should be using S'First as
> offset to compute all slices.

True, but there's an alternative technique, and possibly a better one in 
this kind of case, using implicit array conversion:

    subtype Date_String is String(1..10);
    X: constant Date_String := S; -- checks length and slides if necessary
    ...
    if X(3) /= '/' or X(6) /= '/' then raise ...; end if;
    Your_Function( Time_Of( Year_Number'Value ( X(7..10) ),
                            Month_Number'Value( X(4..5)  ),
                            Day_Number'Value  ( X(1..2)  ) ) );

Obviously, if possible, it is desirable to declare S itself of a subtype 
such as Date_String.

This technique shows the person reading (reviewing) the code that X does 
indeed start at 1. If the declaration of Date_String is distant from the 
use of a variable of this subtype, you can always use an 'if' test (or 
pragma Assert, or a comment) to reiterate that the bounds are 1 and 10.

-- 
Nick Roberts




  reply	other threads:[~2003-12-09  4:48 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-12-06 15:53 ada calendar shoko
2003-12-06 17:01 ` Marius Amado Alves
2003-12-08 19:22   ` Pascal Obry
2003-12-09  4:48     ` Nick Roberts [this message]
2003-12-09 13:23       ` Wes Groleau
2003-12-07  2:49 ` Jeffrey Carter
2003-12-07 12:36   ` Duncan Sands
  -- strict thread matches above, loose matches on Subject: below --
2003-12-05 21:00 shoko
2003-12-05 22:34 ` Stephen Leake
2003-12-06  0:11   ` Nick Roberts
2003-12-08  7:49     ` Reinert Korsnes
2003-12-09  7:40       ` tmoran
2003-12-09 15:59         ` Nick Roberts
2003-12-10  8:16           ` Robert I. Eachus
2003-12-10 12:27             ` Thomas Wolf
2003-12-18 19:03               ` Randy Brukardt
2003-12-05 22:55 ` tmoran
2003-12-06  0:01   ` Marius Amado Alves
2003-12-06  0:21     ` tmoran
2003-12-06 15:08       ` Marius Amado Alves
2003-12-06 16:10 ` David C. Hoos
replies disabled

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