comp.lang.ada
 help / color / mirror / Atom feed
* Conversion to UNIX time
@ 2000-07-19  0:00 Tennisbb
  2000-07-19  0:00 ` Stephen Leake
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Tennisbb @ 2000-07-19  0:00 UTC (permalink / raw)


My project has decided to name recording files (of which there will probably be
MANY) by a number of fields, the first two of which will be Start Time and End
Time in 8-digit Hex UNIX time.  Despite our wide use of re-use, I can't locate
any routines to convert Calendar Time to an 8-digit Hex string, and wondered if
anyone has any procedures they'd be willing to share...  The reason for the Hex
times is to reduce the search time later.  

Thanks,
Laura 




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

* Re: Conversion to UNIX time
  2000-07-19  0:00 Conversion to UNIX time Tennisbb
@ 2000-07-19  0:00 ` Stephen Leake
  2000-07-20  0:00   ` tmoran
  2000-07-20  0:00 ` Keith Thompson
  2000-07-28  0:00 ` Robert I. Eachus
  2 siblings, 1 reply; 6+ messages in thread
From: Stephen Leake @ 2000-07-19  0:00 UTC (permalink / raw)


tennisbb@aol.com (Tennisbb) writes:

> My project has decided to name recording files (of which there will probably be
> MANY) by a number of fields, the first two of which will be Start Time and End
> Time in 8-digit Hex UNIX time.  Despite our wide use of re-use, I can't locate
> any routines to convert Calendar Time to an 8-digit Hex string, and wondered if
> anyone has any procedures they'd be willing to share...  The reason for the Hex
> times is to reduce the search time later.  

Below is code to produce hex strings from unsigned integers. If you
can convert time to an unsigned integer, you're all set.

-- 
-- Stephe

-- Abstract:
--
-- Generic plain Hexadecimal image
--
generic
   Width : Natural;
   type Number_Type is mod <>;
function Sal.Generic_Hex_Image (Item : in Number_Type) return String;
-- Return a hexadecimal image of Item, padded with leading zeros to Width.
-- If Width is too small for Item, leading digits are silently truncated.

-- Abstract:
--
-- see spec
--
function Sal.Generic_Hex_Image (Item : in Number_Type) return String
is
   Temp : Number_Type := Item;
   Nibble : Number_Type;
   Image : String (1 .. Width);
begin
   for I in reverse Image'Range loop
      Nibble := Temp mod 16;
      Temp := Temp / 16;
      if Nibble > 9 then
         Image (I) := Character'Val (Character'Pos ('A') + Nibble - 10);
      else
         Image (I) := Character'Val (Character'Pos ('0') + Nibble);
      end if;
   end loop;
   return Image;
end Sal.Generic_Hex_Image;




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

* Re: Conversion to UNIX time
  2000-07-19  0:00 ` Stephen Leake
@ 2000-07-20  0:00   ` tmoran
  0 siblings, 0 replies; 6+ messages in thread
From: tmoran @ 2000-07-20  0:00 UTC (permalink / raw)


>If you can convert time to an unsigned integer, you're all set.
  Here's code to produce the a day number, with 1 being January 1,
in Ada.Calendar.Year_Number'first.
(Julian_Day(A_Time) - Julian_Day(First_Unix_Date))*24*60*60+Ada.Calendar.Seconds
with appropriate type conversions should give you a (large) unsigned integer.

type Day_Count is range
  -366*(1+Ada.Calendar.Year_Number'last - Ada.Calendar.Year_Number'first)
  ..
  366*(1+Ada.Calendar.Year_Number'last - Ada.Calendar.Year_Number'first);

subtype Is_Leap_Year is Boolean;
Days_Before : constant array(Is_Leap_Year, Ada.Calendar.Month_Number) of Day_Count
    := (False => (0,31,59,90,120,151,181,212,243,273,304,334),
        True  => (0,31,60,91,121,152,182,213,244,274,305,335));

subtype Day_Number_In_Era is Day_Count range 1 .. Day_Count'last;
    --   1 => 1/1/Ada.Calendar.Year_Number'first

function Julian_Day(T : Ada.Calendar.Time) return Day_Number_In_Era is
    This_Year : constant Integer := Integer(Ada.Calendar.Year(T));
    Base_Year : constant Integer := Integer(Ada.Calendar.Year_Number'first);
    Leap_Year : constant Boolean :=
        (This_Year mod 4) = 0
         and ((This_Year mod 400) = 0 or (not (This_Year mod 100 = 0)));
begin
    return Day_Count(This_Year - Base_Year)*365
         + Day_Count((This_Year-1)/4 - (Base_Year-1)/4)
         - Day_Count((This_Year-1)/100 - (Base_Year-1)/100)
         + Day_Count((This_Year-1)/400 - (Base_Year-1)/400)
         + Day_Count(Ada.Calendar.Day(T))
         + Days_Before(Leap_Year, Ada.Calendar.Month(T));
end Julian_Day;




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

* Re: Conversion to UNIX time
  2000-07-19  0:00 Conversion to UNIX time Tennisbb
  2000-07-19  0:00 ` Stephen Leake
@ 2000-07-20  0:00 ` Keith Thompson
  2000-07-28  0:00 ` Robert I. Eachus
  2 siblings, 0 replies; 6+ messages in thread
From: Keith Thompson @ 2000-07-20  0:00 UTC (permalink / raw)


tennisbb@aol.com (Tennisbb) writes:
> My project has decided to name recording files (of which there will
> probably be MANY) by a number of fields, the first two of which will
> be Start Time and End Time in 8-digit Hex UNIX time.  Despite our
> wide use of re-use, I can't locate any routines to convert Calendar
> Time to an 8-digit Hex string, and wondered if anyone has any
> procedures they'd be willing to share...  The reason for the Hex
> times is to reduce the search time later.

You might try interfacing to the standard C function mktime() (or
using an existing binding if you can find one).  There are a few
things to watch out for: tm_year is year - 1900, and tm_mon is in the
range 0..11, not 1..12.  If you're on a Unix system, see the mktime(3)
man page for details.

Note that the C standard does not guarantee that time_t measures
seconds since 1970-01-01 00:00:00 UTC, though I think POSIX does.

If you're interested in the current time rather than necessarily
converting from an existing Calendar.Time value, you might consider
interfacing directly to C's time() function.

-- 
Keith Thompson (The_Other_Keith) kst@cts.com  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Welcome to the last year of the 20th century.




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

* Re: Conversion to UNIX time
  2000-07-28  0:00 ` Robert I. Eachus
@ 2000-07-27  0:00   ` Keith Thompson
  0 siblings, 0 replies; 6+ messages in thread
From: Keith Thompson @ 2000-07-27  0:00 UTC (permalink / raw)


"Robert I. Eachus" <rieachus@earthlink.net> writes:
[...]
>     Having said that, Use Unchecked_Conversion to convert the time
> representation to an integer or better a modular type.  Next instantiate
> Ada.Text_IO.Integer_IO or Modular_IO for that type.  Now call Put (to
> string) with Base = 16.  Edit the return string to remove the leading "
> 16#" and trailing "#".  Sounds very complex, but you can do the call to
> Unchecked_Conversion nested, then subscript the returned value:
> 
>    Temp: String(1..13);
>  begin
>    Mod_IO.Put(Temp, My_UC(Start_Time), 16);
>    FileName(X..X+7) := Temp(5..12);

Why use Unchecked_Conversion?  If Start_Time is of an integer type,
wouldn't an ordinary type conversion make more sense?

Start_Time is probably a time_t, which on many systems is a 32-bit
signed integer.  (But not all; I work on systems where time_t is
64 bits -- as it's likely to be on all systems by 2038.)

-- 
Keith Thompson (The_Other_Keith) kst@cts.com
<http://www.ghoti.net/~kst> San Diego Supercomputer Center <*>
<http://www.sdsc.edu/~kst> Welcome to the last year of the 20th
century.




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

* Re: Conversion to UNIX time
  2000-07-19  0:00 Conversion to UNIX time Tennisbb
  2000-07-19  0:00 ` Stephen Leake
  2000-07-20  0:00 ` Keith Thompson
@ 2000-07-28  0:00 ` Robert I. Eachus
  2000-07-27  0:00   ` Keith Thompson
  2 siblings, 1 reply; 6+ messages in thread
From: Robert I. Eachus @ 2000-07-28  0:00 UTC (permalink / raw)


Tennisbb wrote:
> 
> My project has decided to name recording files (of which there will probably be
> MANY) by a number of fields, the first two of which will be Start Time and End
> Time in 8-digit Hex UNIX time.  Despite our wide use of re-use, I can't locate
> any routines to convert Calendar Time to an 8-digit Hex string, and wondered if
> anyone has any procedures they'd be willing to share...  The reason for the Hex
> times is to reduce the search time later.

    I could write all of this in a few lines, but you had better do it,
because the declarations
are going to reflect information you did not give.

    Having said that, Use Unchecked_Conversion to convert the time
representation to an integer or better a modular type.  Next instantiate
Ada.Text_IO.Integer_IO or Modular_IO for that type.  Now call Put (to
string) with Base = 16.  Edit the return string to remove the leading "
16#" and trailing "#".  Sounds very complex, but you can do the call to
Unchecked_Conversion nested, then subscript the returned value:

   Temp: String(1..13);
 begin
   Mod_IO.Put(Temp, My_UC(Start_Time), 16);
   FileName(X..X+7) := Temp(5..12);




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

end of thread, other threads:[~2000-07-28  0:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-07-19  0:00 Conversion to UNIX time Tennisbb
2000-07-19  0:00 ` Stephen Leake
2000-07-20  0:00   ` tmoran
2000-07-20  0:00 ` Keith Thompson
2000-07-28  0:00 ` Robert I. Eachus
2000-07-27  0:00   ` Keith Thompson

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