< previous page page_205 next page >

Page 205
Listing 8.4 iso_double_to_duration ()
/* idbl2dur.c
   +-------------------------------------------+
   |   Copyright (C) 1995 by Lance W. Latham.  |
   |         All rights reserved.              |
   +-------------------------------------------+
   Routine converts a type double to an elapsed time, expressed as an
   ISO_DURATION structure.


   INPUTS:
   1. diff, type double, assumed to be the difference between two Julian
      Day numbers. Valid values are greater than or equal to zero.


   OUTPUTS:
   1. Returned by routine, an ISO_DURATION structure containing the integer
      part of the input as a DATE_INFO structure (year, month and day),
      using a standard year of 360 days, a standard month of 30 days and
      a unit day. The fractional part of the input is contained as a
      time, expressed as a TIME_INFO structure (hours, minutes and seconds).


   NOTES:
   1. The routine intercepts negative values of the input 'diff' and
      converts them to positive numbers by inverting the sign. No
      other validation is performed, and no warning is issued in this
      case.
   2. This routine converts a difference between two Julian day numbers,
      expressed as a type double, into a duration of elapsed time, expressed
      as an ISO_DURATION structure. To convert a Julian Day number to its
      equivalent date and time, use the ISO_DATE_TIME structure and the
      routine in this series, 'iso_double_to_date_time ()'.
   3. The number of significant digits carried for type double can affect
      the conversion from type double. In some cases, the time can be
      converted to a value with 60 seconds, by the rounding process required
      to handle the variation. This can occur in 2 situations:


      3.1. A time of (h, m, 0) is converted as (h, m-1, 60)
      3.2. A time of (h, 0, 0) is converted as (h-1, 59, 60).


      In both cases, the proper number of units exist, and detection of the
      value of '60' in the seconds position is sufficient to correct the
      converted time by incrementing successively higher units.
      This process of roll over terminates at the year level, where the
      code looks ahead to see if the year is already at INT_MAX for the
      system. If it is, the year is NOT incremented, to avoid the roll over
      into negative values (assuming two's complement representation). In
      actual practice, this situation should not arise with legitimate
      values.
   4. The maximum value of year is pegged at INT_MAX. The value of 'diff'
      can be arbitrarily large, and exceed this value. E.g., the Julian Day
      number of 13689325 is equivalent to A.D. 31 December 32767. On a
      16-bit integer system, no JD# can legitimately be larger. This
      practical restriction does not prevent 'type double' values of
      this magnitude from being generated or used as arguments.
*/


 
< previous page page_205 next page >