comp.lang.ada
 help / color / mirror / Atom feed
* Ada.Calendar and number of seconds in a day
@ 2004-12-06 13:31 Adrien Plisson
  2004-12-06 13:43 ` Adrien Plisson
  0 siblings, 1 reply; 2+ messages in thread
From: Adrien Plisson @ 2004-12-06 13:31 UTC (permalink / raw)


hello,

i'm using the package Ada.Calendar and have a little problem with the type 
Day_Duration. it is defined as:
subtype Day_Duration is Duration range 0.0 ..  86_400.0;
this makes 86401 different seconds.

but if i count them right, i have:
60 s/min * 60 min/h * 24 h/day = 86400
this makes 86400 different seconds in a day.

so, logically i should have:
Time_Of( 2004, 12, 31, 86400 ) = Time_Of( 2005, 1, 1, 0.0 );

tested with GNAT, the above equality test is true, but if i store the time in 
separate components this way:

type Date is record
     Year : Year_Number;
     Month : Month_Number;
     Day : Day_Number;
     Seconds : Day_Duration;
end record;

then I may encounter a problem since:
Date'(2004, 12, 31, 86400) /= Date'(2005, 1, 1, 0.0);

so what does this extra second mean in this package ?
shouldn't Day_Duration be "subtype Day_Duration is Duration range 0.0 .. 
86_399.0;" ?

thanks for your enlightenment.


below is a small program to test all this:
..........................................
with Ada.Calendar,
      Ada.Text_IO;
procedure Main is
     use Ada.Calendar;
	
     -- a Date type which holds a date in separate components
     type Date is record
         Year : Year_Number;
         Month : Month_Number;
         Day : Day_Number;
         Seconds : Day_Duration;
     end record;

     -- simple equality test for Date
     function "="( Left, Right : in Date ) return Boolean is
     begin
         return (Left.Year = Right.Year) and (Left.Month = Right.Month) and 
(Left.Day = Right.Day) and (Left.Seconds = Right.Seconds);
     end "=";

     -- convert a Date into a Time
     function To_Time( D : in Date ) return Time is
     begin
         return Time_Of( D.Year, D.Month, D.Day, D.Seconds );
     end To_Time;


     -- return a string from a Time for suitable display
     function String_Time( T : in Time ) return String is
         Year : Year_Number;
         Month : Month_Number;
         Day : Day_Number;
         Seconds : Day_Duration;
     begin
         Split( T, Year, Month, Day, Seconds );
         return  Year_Number'Image( Year ) & "/" &
                          Month_Number'Image( Month ) & "/" &
                          Day_Number'Image( Day ) & ", " &
                          Day_Duration'Image( Seconds );
     end String_Time;

     -- return a string from a Date for suitable display
     function String_Date( D : in Date ) return String is
     begin
         return  Year_Number'Image( D.Year ) & "/" &
                 Month_Number'Image( D.Month ) & "/" &
                 Day_Number'Image( D.Day ) & ", " &
                 Day_Duration'Image( D.Seconds );
     end String_Date;


     -- these are the 2 offending dates
     T1 : Date := Date'( 2004, 12, 31, 86400.0 );
     T2 : Date := Date'( 2005, 01, 01,     0.0 );

begin
     -- test with Time
     Ada.Text_IO.Put_Line( "T1: " & String_Time( To_Time( T1 ) ) );
     Ada.Text_IO.Put_Line( "T2: " & String_Time( To_Time( T2 ) ) );

     if To_Time( T1 ) = To_Time( T2 ) then
         Ada.Text_IO.Put_Line( "T1 and T2 are equal in Time" );
     else
         Ada.Text_IO.Put_Line( "T1 and T2 differ in Time" );
     end if;

     -- test with Date
     Ada.Text_IO.Put_Line( "T1: " & String_Date( T1 ) );
     Ada.Text_IO.Put_Line( "T2: " & String_Date( T2 ) );

     if T1 = T2 then
         Ada.Text_IO.Put_Line( "T1 and T2 are equal in Date" );
     else
         Ada.Text_IO.Put_Line( "T1 and T2 differ in Date" );
     end if;
end Main;
..........................................

-- 
rien



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

* Re: Ada.Calendar and number of seconds in a day
  2004-12-06 13:31 Ada.Calendar and number of seconds in a day Adrien Plisson
@ 2004-12-06 13:43 ` Adrien Plisson
  0 siblings, 0 replies; 2+ messages in thread
From: Adrien Plisson @ 2004-12-06 13:43 UTC (permalink / raw)


ooops...

i just read further the RM, and all this is specified...
sorry for bothering you with this !

-- 
rien (really sorry)

ps: please excuse me...



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

end of thread, other threads:[~2004-12-06 13:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-06 13:31 Ada.Calendar and number of seconds in a day Adrien Plisson
2004-12-06 13:43 ` Adrien Plisson

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