comp.lang.ada
 help / color / mirror / Atom feed
* ADA.CALENDAR and midnight
@ 2004-08-05 10:00 Brian May
  2004-08-05 11:06 ` Frank Piron
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Brian May @ 2004-08-05 10:00 UTC (permalink / raw)


Hello,

According to documentation I have:

subtype DAY_DURATION is DURATION range 0.0 .. 86_400.0;

How is midnight meant to be represented?

I would have assumed it would be 0.0, the next day, but preliminary
tests, unless I am confused, seem to show it is represented as
86_400.0 (using gnat compiler for Windows)? The following quote from
the documentation would seem to imply I am correct; "SECONDS
indicating the number of seconds past midnight", but I am getting
behaviour which suggests otherwise.

Why are both 0.0 and 86_400.0 defined to be legal values?  Aren't they
both the same thing (but one day apart)?
-- 
Brian May <bam@snoopy.apana.org.au>



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: ADA.CALENDAR and midnight
  2004-08-05 10:00 ADA.CALENDAR and midnight Brian May
@ 2004-08-05 11:06 ` Frank Piron
  2004-08-05 11:19 ` Nick Roberts
  2004-08-05 13:01 ` Ada.Calendar " Jacob Sparre Andersen
  2 siblings, 0 replies; 10+ messages in thread
From: Frank Piron @ 2004-08-05 11:06 UTC (permalink / raw)


Hi,

Thu, 05 Aug 2004 20:00:52 +1000  Brian May <bam@snoopy.apana.org.au> wrote:

> Hello,
>
> According to documentation I have:
>
> subtype DAY_DURATION is DURATION range 0.0 .. 86_400.0;
>
> How is midnight meant to be represented?
>
> I would have assumed it would be 0.0, the next day, but preliminary
> tests, unless I am confused, seem to show it is represented as
> 86_400.0 (using gnat compiler for Windows)? The following quote from
> the documentation would seem to imply I am correct; "SECONDS
> indicating the number of seconds past midnight", but I am getting
> behaviour which suggests otherwise.
>
> Why are both 0.0 and 86_400.0 defined to be legal values?  Aren't they
> both the same thing (but one day apart)?

Any instance of type DAY_DURATION represents a timespan.
"midnight" means a point in time (more precisely a discrite,
infinite subset of all points in time).

If you would take away 86_400.0 from DAY_DURATION then you would
have a unique representation for every point in time p as:

p = midnight + dd (dd a DAY_DURATION instance)

on the one hand. But on the other hand the timespan of a full day
could not be represented by an instance of DAY_DURATION.

-- 
Crisis? What Crisis?
----------------------------------------------------
f_r_a_n_k_a_t_k_o_n_a_d_d_o_t_n_e_t



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: ADA.CALENDAR and midnight
  2004-08-05 10:00 ADA.CALENDAR and midnight Brian May
  2004-08-05 11:06 ` Frank Piron
@ 2004-08-05 11:19 ` Nick Roberts
  2004-08-05 11:46   ` Brian May
  2004-08-05 13:01 ` Ada.Calendar " Jacob Sparre Andersen
  2 siblings, 1 reply; 10+ messages in thread
From: Nick Roberts @ 2004-08-05 11:19 UTC (permalink / raw)


On Thu, 05 Aug 2004 20:00:52 +1000, Brian May <bam@snoopy.apana.org.au>  
wrote:

> According to documentation I have:
>
> subtype DAY_DURATION is DURATION range 0.0 .. 86_400.0;
>
> How is midnight meant to be represented?
>
> I would have assumed it would be 0.0, the next day, but preliminary
> tests, unless I am confused, seem to show it is represented as
> 86_400.0 (using gnat compiler for Windows)? The following quote from
> the documentation would seem to imply I am correct; "SECONDS
> indicating the number of seconds past midnight", but I am getting
> behaviour which suggests otherwise.
>
> Why are both 0.0 and 86_400.0 defined to be legal values?  Aren't
> they both the same thing (but one day apart)?

RM95 (TC1) 9.6 (25) states:

    If Time_Of is called with a seconds value of 86_400.0, the value
    returned is equal to the value of Time_Of for the next day with a
    seconds value of 0.0. The value returned by the function Seconds or
    through the Seconds parameter of the procedure Split is always less
    than 86_400.0.

So I would say that an implementation which returns 86_400.0 is
faulty.

-- 
Nick Roberts



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: ADA.CALENDAR and midnight
  2004-08-05 11:19 ` Nick Roberts
@ 2004-08-05 11:46   ` Brian May
  2004-08-05 12:01     ` Jano
  0 siblings, 1 reply; 10+ messages in thread
From: Brian May @ 2004-08-05 11:46 UTC (permalink / raw)


>>>>> "Nick" == Nick Roberts <nick.roberts@acm.org> writes:

    Nick> If Time_Of is called with a seconds value of 86_400.0, the value
    Nick> returned is equal to the value of Time_Of for the next day with a
    Nick> seconds value of 0.0. The value returned by the function Seconds or
    Nick> through the Seconds parameter of the procedure Split is always less
    Nick> than 86_400.0.

    Nick> So I would say that an implementation which returns 86_400.0 is
    Nick> faulty.

Hmmm.. Have to double check.

I have the following function (spot the bug!), for displaying times in
hh:mm:ss format.

>-------function To_String(The_Time : in Time) return String is
>------->-------Duration : Integer;
>------->-------H : Integer range 1..24;
>------->-------M : Integer range 0..60;
>------->-------S : Integer range 0..60;
>-------begin
>------->-------Duration := Integer(Seconds(The_Time));

>------->-------S := Duration mod 60;
>------->-------Duration := (Duration-S)/60;

>------->-------M := Duration mod 60;
>------->-------Duration := (Duration-M)/60;

>------->-------H := Duration;
>------->-------
>------->-------return Strip(Integer'Image(H))&":"
>------->------->-------&Strip(Integer'Image(M))&":"
>------->------->-------&Strip(Integer'Image(S));
>-------end To_String;

The first call is To_String(Clock + offset). When an exception occurs,
it is called as To_String(Clock) for the log message. Strip is just a
function that strips off leading white space.

This worked until around midnight. Predictably, I got a constraint
exception when assigning the value to H, because my limits are wrong.
However, when printing the exception message, this function is called
again[1], and returned the result of "24:0:0". I am kind of confused
this is possible, unless Duration was initially 86_400.0[2].

Maybe somewhere my logic is flawed, I will investigate again tomorrow.

Notes:
[1] hmmm... what happens if you get a recursive exception in Ada?
    I probably should protect my logging functions a bit better.
[2] Can someone confirm my assumption that Integer(86_399.9) = 86_399?
-- 
Brian May <bam@snoopy.apana.org.au>



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: ADA.CALENDAR and midnight
  2004-08-05 11:46   ` Brian May
@ 2004-08-05 12:01     ` Jano
  2004-08-05 12:15       ` Jean-Pierre Rosen
  0 siblings, 1 reply; 10+ messages in thread
From: Jano @ 2004-08-05 12:01 UTC (permalink / raw)


Brian May wrote:

> [2] Can someone confirm my assumption that Integer(86_399.9) = 86_399?

Conversion between decimal and integer types does rounding. At least in 
gnat.



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: ADA.CALENDAR and midnight
  2004-08-05 12:01     ` Jano
@ 2004-08-05 12:15       ` Jean-Pierre Rosen
  2004-08-05 23:18         ` Brian May
  0 siblings, 1 reply; 10+ messages in thread
From: Jean-Pierre Rosen @ 2004-08-05 12:15 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 763 bytes --]


"Jano" <notelacreas@porfavor.no> a �crit dans le message de news:2nelqrF21jaU1@uni-berlin.de...
> Brian May wrote:
>
> > [2] Can someone confirm my assumption that Integer(86_399.9) = 86_399?
>
> Conversion between decimal and integer types does rounding. At least in
> gnat.
4.6 (32):
If the target type is an integer type and the operand type is real, the result is rounded to the nearest integer (away from zero if
exactly halfway between two integers).

If you want truncation, either subtract 0.5 or use the 'Floor attribute (or 'Truncation, depending on the behaviour you want for
negative values).

-- 
---------------------------------------------------------
           J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr





^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Ada.Calendar and midnight
  2004-08-05 10:00 ADA.CALENDAR and midnight Brian May
  2004-08-05 11:06 ` Frank Piron
  2004-08-05 11:19 ` Nick Roberts
@ 2004-08-05 13:01 ` Jacob Sparre Andersen
  2 siblings, 0 replies; 10+ messages in thread
From: Jacob Sparre Andersen @ 2004-08-05 13:01 UTC (permalink / raw)


Brian May wrote:

> According to documentation I have:
> 
> subtype Day_Duration is Duration range 0.0 .. 86_400.0;
> 
> How is midnight meant to be represented?

As 0.0 or 86_400.0 depending on which one of them you are talking
about.

> Why are both 0.0 and 86_400.0 defined to be legal values?  Aren't
> they both the same thing (but one day apart)?

Yes.

It is not possible (in Ada) to define intervals that do not include
their end-points.

Jacob
-- 
     My brain needs a "back" button so I can
         remember where I left my coffee mug.



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: ADA.CALENDAR and midnight
  2004-08-05 12:15       ` Jean-Pierre Rosen
@ 2004-08-05 23:18         ` Brian May
  2004-08-06  5:59           ` Martin Dowie
  2004-08-06  7:34           ` Gautier
  0 siblings, 2 replies; 10+ messages in thread
From: Brian May @ 2004-08-05 23:18 UTC (permalink / raw)


>>>>> "Jean-Pierre" == Jean-Pierre Rosen <rosen@adalog.fr> writes:

    Jean-Pierre> 4.6 (32): If the target type is an integer type and
    Jean-Pierre> the operand type is real, the result is rounded to
    Jean-Pierre> the nearest integer (away from zero if exactly
    Jean-Pierre> halfway between two integers).

    Jean-Pierre> If you want truncation, either subtract 0.5 or use
    Jean-Pierre> the 'Floor attribute (or 'Truncation, depending on
    Jean-Pierre> the behaviour you want for negative values).

Thanks for the assistance. It appears I am getting confused between
differences in different languages...

Going back to me to_string function, are there any packages (GPL
compatible licence preferred) for displaying formatted output?  For
instance, my function would display "12:0:0", it would be preferred if
I could display it as "12:00:00". I have a package called
"Formatted_Output" by "Eugene Nonko, cm@liceum.secna.ru", but the
license is unclear. I am not convinced the approach taken is ideal
either. Ideally, I would prefer not to have to reinvent the wheel.
-- 
Brian May <bam@snoopy.apana.org.au>



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: ADA.CALENDAR and midnight
  2004-08-05 23:18         ` Brian May
@ 2004-08-06  5:59           ` Martin Dowie
  2004-08-06  7:34           ` Gautier
  1 sibling, 0 replies; 10+ messages in thread
From: Martin Dowie @ 2004-08-06  5:59 UTC (permalink / raw)


"Brian May" <bam@snoopy.apana.org.au> wrote in message
news:sa4smb1cgxp.fsf@snoopy.apana.org.au...
> Thanks for the assistance. It appears I am getting confused between
> differences in different languages...
>
> Going back to me to_string function, are there any packages (GPL
> compatible licence preferred) for displaying formatted output?  For
> instance, my function would display "12:0:0", it would be preferred if
> I could display it as "12:00:00". I have a package called
> "Formatted_Output" by "Eugene Nonko, cm@liceum.secna.ru", but the
> license is unclear. I am not convinced the approach taken is ideal
> either. Ideally, I would prefer not to have to reinvent the wheel.

Yup, check out Ada.Calendar.* esp Ada.Calender.Formatting.

See http://www.martin.dowie.btinternet.co.uk/

These have the same API as the packages (hopefully) being made
standard in Ada0Y

Cheers

-- Martin





^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: ADA.CALENDAR and midnight
  2004-08-05 23:18         ` Brian May
  2004-08-06  5:59           ` Martin Dowie
@ 2004-08-06  7:34           ` Gautier
  1 sibling, 0 replies; 10+ messages in thread
From: Gautier @ 2004-08-06  7:34 UTC (permalink / raw)


Brian May :

> Going back to me to_string function, are there any packages (GPL
> compatible licence preferred) for displaying formatted output?  For
> instance, my function would display "12:0:0", it would be preferred if
> I could display it as "12:00:00". I have a package called
> "Formatted_Output" by "Eugene Nonko, cm@liceum.secna.ru", but the
> license is unclear. I am not convinced the approach taken is ideal
> either. Ideally, I would prefer not to have to reinvent the wheel.

For obtaining all heading '0's you need there is a simple trick
(here, a Time_log function of mine, also 16-bit and Ada83 compatible):

with Calendar;

function Time_log return String is
  use Calendar;
  subtype Sec_int is Long_Integer; -- must contain 86_400
  T    : Time:= Clock;
  m, s : Sec_int;

begin
  s := Sec_int( Seconds(T) );
  m := s / 60;

  declare
    -- + 100: trick for obtaining 0x
    sY : constant String:= Integer'Image( Year(T));
    sM : constant String:= Integer'Image( Month(T) + 100);
    sD : constant String:= Integer'Image(  Day(T)  + 100);
    shr: constant String:= Sec_int'Image( m  /  60 + 100);
    smn: constant String:= Sec_int'Image( m mod 60 + 100);
    ssc: constant String:= Sec_int'Image( s mod 60 + 100);

  begin
    return
      sY( sY'Last-3 .. sY'Last ) & '/' &  -- not Year 10'000 compliant.
      sM( sM'Last-1 .. sM'Last ) & '/' &
      sD( sD'Last-1 .. sD'Last ) &
      "   " &
      shr( shr'Last-1 .. shr'Last ) & ':' &
      smn( smn'Last-1 .. smn'Last ) & ':' &
      ssc( ssc'Last-1 .. ssc'Last );
  end;

end Time_log;
HTH
________________________________________________________
Gautier  --  http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2004-08-06  7:34 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-05 10:00 ADA.CALENDAR and midnight Brian May
2004-08-05 11:06 ` Frank Piron
2004-08-05 11:19 ` Nick Roberts
2004-08-05 11:46   ` Brian May
2004-08-05 12:01     ` Jano
2004-08-05 12:15       ` Jean-Pierre Rosen
2004-08-05 23:18         ` Brian May
2004-08-06  5:59           ` Martin Dowie
2004-08-06  7:34           ` Gautier
2004-08-05 13:01 ` Ada.Calendar " Jacob Sparre Andersen

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