GAO Associates 
87 Andrews Way
Plymouth, MA 02360-1641


This page contains source code lists of routines in ASCII text format that may be 
of interest to  programmers who code in the basic language.  The routines
are in Visual Basic format and have all been tested for validity, however GAOA 
assumes no responsibility for their use by anyone, since they are provided free of 
charge.  The routines are copyright free and may be used for 
personal or business purposes.  The only limitations are that they cannot be sold
as individual items, but can be included as components is applications that are sold.
Also, if any of the routines are redistributed, it is requested that the user indicate their
source as GAO Associates.
More functions and subroutines will be added here on a regular basis.


The source code listing below can simply be copied using any word processor that can
export  text such as WordPerfect, or a simple text processor like Wordpad. 
An equation for generating a value for PI
In programs the require the value for pi, one can either write out the number and
equate it to a global constant, or generate with the following expression
        PI = 4 * ATN(1)
where ATN is the ordinary arctangent function (radian measure) which is included
in all programming languages.


General Date Computation Routines
The two functions and one subroutine that follow can all be used together for handling
time and date problems.  The function "date_to_jd(year, month, day+fraction)" converts
input consisting of 3 variables to the full julian day number.  This number is used for setting time
differentials of various types.  For example, to get the exact time span between any two dates,
including time of day, input year (must be error free), month, and day where day can include
a fraction (e.g. for the 13th at 08:30, the input day is approximately 13.3541667), find the julian
day number for each date/time of interest and the difference between them is the desired time
span in days and fraction of a day.


Function DateToJd(yr as integer, mo as integer, day as double,  hr as double, min as double, sec as double)
  'Converts calendar date to full Julian day number
  'can be used over any Gregorian calendar date 
  'yr must be in 4 digit format

  
  Dim jd as double, d as double y as integer, m as integer, a as integer, b as integer
  
  'compute full value of day.  note, non-zero input for hr, min, or sec have no effect
  'Example full value input for day could include any fraction of day e.g. 23.45876
  'in this case input would be ( yr, mo, 23.45876, 0, 0, 0 )  with appropriate values for yr and mo

   d = d + hr / 24 + min / 1440 + sec / 86400

  If mo <= 2 Then
    y = yr - 1
    m = mo + 12
  Else
    y = yr
    m = mo
  End If
  a = Fix(y / 100)
  b = 2 - a + Fix(a / 4)
  jd = Fix(365.25 * (y + 4716)) + Fix(30.6001 * (m + 1))
  jd = jd + d + b - 1524.5
  DateToJd = jd
End Function


Sub JdToDate(jd#, yr%, mo%, da%, hr%, min%, sec!)
  'Note old notation for double#, single!, integer%, long& used here
  'Converts jd to date/time
  'jd need not be an integer and can include fraction of day
  'works for any positive julian day number
  'produces positive as well as negative (BC) years
  '*************************************************
  Dim jdc#, z&, f#, alph&, a&, b&, c&
  Dim d&, e%, dom#, hrs#, mins#

  jdc = jd + 0.5
  z = Fix(jdc)
  f = jdc - z
  alph = Fix((z - 1867216.25) / 36524.25)
  a = z + 1 + alph - Fix(alph / 4)
  b = a + 1524
  c = Fix((b - 122.1) / 365.25)
  d = Fix(365.25 * c)
  e = Fix((b - d) / 30.6001)
  dom = b - d - Fix(30.6001 * e) + f
  da = Fix(dom)       'day out
  hrs = (dom - da) * 24
  hr = Fix(hrs)       'hour out
  mins = (hrs - hr) * 60
  min = Fix(mins)     'minute out
  sec = (mins - min) * 60
  If e < 14 Then
    mo = e - 1        'month out
  Else
    mo = e - 13
  End If
  If mo > 2 Then       'year out
    yr = c - 4716
  Else
    yr = c - 4715
  End If
End Sub