From: Adrien Plisson <aplisson-news@stochastique.net>
Subject: Ada.Calendar and number of seconds in a day
Date: Mon, 06 Dec 2004 14:31:26 +0100
Date: 2004-12-06T14:31:26+01:00 [thread overview]
Message-ID: <41b45e45$0$9310$ba620e4c@news.skynet.be> (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
next reply other threads:[~2004-12-06 13:31 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-12-06 13:31 Adrien Plisson [this message]
2004-12-06 13:43 ` Ada.Calendar and number of seconds in a day Adrien Plisson
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox