Back to index

lightning-sunbird  0.9+nobinonly
caldate.c
Go to the documentation of this file.
00001 /*
00002  *     Copyright (c) 1986-2000, Hiram Clawson 
00003  *     All rights reserved.
00004  *
00005  *     Redistribution and use in source and binary forms, with or
00006  *     without modification, are permitted provided that the following
00007  *     conditions are met:
00008  *
00009  *            Redistributions of source code must retain the above
00010  *            copyright notice, this list of conditions and the
00011  *            following disclaimer.
00012  *
00013  *            Redistributions in binary form must reproduce the
00014  *            above copyright notice, this list of conditions and
00015  *            the following disclaimer in the documentation and/or
00016  *            other materials provided with the distribution.
00017  *
00018  *            Neither name of The Museum of Hiram nor the names of
00019  *            its contributors may be used to endorse or promote products
00020  *            derived from this software without specific prior
00021  *            written permission. 
00022  *
00023  *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
00024  *     CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
00025  *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00026  *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00027  *     IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00028  *     INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029  *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00030  *     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00031  *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00032  *     STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00033  *     IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00034  *     THE POSSIBILITY OF SUCH DAMAGE. 
00035  */
00036 
00037 #include "astime.h"  /*     time structures      */
00038 
00058 long caldat( date )
00059 struct ut_instant * date;
00060 {
00061        double frac;
00062        long jd;
00063        long ka;
00064        long kb;
00065        long kc;
00066        long kd;
00067        long ke;
00068        long ialp;
00069 
00070        jd = (long) (date->j_date + 0.5);  /* integer julian date */
00071        frac = date->j_date + 0.5 - (double) jd + 1.0e-10; /* day fraction */
00072        ka = (long) jd;
00073        if ( jd >= 2299161L )
00074        {
00075               ialp = ( (double) jd - 1867216.25 ) / ( 36524.25 );
00076               ka = jd + 1L + ialp - ( ialp >> 2 );
00077        }
00078        kb = ka + 1524L;
00079        kc =  ( (double) kb - 122.1 ) / 365.25;
00080        kd = (double) kc * 365.25;
00081        ke = (double) ( kb - kd ) / 30.6001;
00082        date->day = kb - kd - ((long) ( (double) ke * 30.6001 ));
00083        if ( ke > 13L )
00084               date->month = ke - 13L;
00085        else
00086               date->month = ke - 1L;
00087        if ( (date->month == 2) && (date->day > 28) )
00088               date->day = 29;
00089        if ( (date->month == 2) && (date->day == 29) && (ke == 3L) )
00090               date->year = kc - 4716L;
00091        else if ( date->month > 2 )
00092               date->year = kc - 4716L;
00093        else
00094               date->year = kc - 4715L;
00095        date->i_hour = date->d_hour = frac * 24.0;       /* hour */
00096        date->i_minute = date->d_minute =
00097               ( date->d_hour - (double) date->i_hour ) * 60.0; /* minute */
00098        date->i_second = date->d_second =
00099               ( date->d_minute - (double) date->i_minute ) * 60.0;/* second */
00100        date->weekday = (jd + 1L) % 7L;    /* day of week */
00101        if ( date->year == ((date->year >> 2) << 2) )
00102               date->day_of_year =
00103                      ( ( 275 * date->month ) / 9)
00104                      - ((date->month + 9) / 12)
00105                      + date->day - 30;
00106        else
00107               date->day_of_year =
00108                      ( ( 275 * date->month ) / 9)
00109                      - (((date->month + 9) / 12) << 1)
00110                      + date->day - 30;
00111        return( date->year );
00112 }
00113 
00131 double juldat( date )
00132 struct ut_instant * date;
00133 {
00134        double frac, gyr;
00135        long iy0, im0;
00136        long ia, ib;
00137        long jd;
00138 
00139        /* decimal day fraction     */
00140        frac = (( double)date->i_hour/ 24.0)
00141               + ((double) date->i_minute / 1440.0)
00142               + (date->d_second / 86400.0);
00143        /* convert date to format YYYY.MMDDdd     */
00144        gyr = (double) date->year
00145               + (0.01 * (double) date->month)
00146               + (0.0001 * (double) date->day)
00147               + (0.0001 * frac) + 1.0e-9;
00148        /* conversion factors */
00149        if ( date->month <= 2 )
00150        {
00151               iy0 = date->year - 1L;
00152               im0 = date->month + 12;
00153        }
00154        else
00155        {
00156               iy0 = date->year;
00157               im0 = date->month;
00158        }
00159        ia = iy0 / 100L;
00160        ib = 2L - ia + (ia >> 2);
00161        /* calculate julian date    */
00162        if ( date->year < 0L )
00163               jd = (long) ((365.25 * (double) iy0) - 0.75)
00164                      + (long) (30.6001 * (im0 + 1L) )
00165                      + (long) date->day + 1720994L;
00166        else
00167               jd = (long) (365.25 * (double) iy0)
00168                      + (long) (30.6001 * (double) (im0 + 1L))
00169                      + (long) date->day + 1720994L;
00170        if ( gyr >= 1582.1015 )     /* on or after 15 October 1582     */
00171               jd += ib;
00172        date->j_date = (double) jd + frac + 0.5;
00173        jd = (long) (date->j_date + 0.5);
00174        date->weekday = (jd + 1L) % 7L;
00175        return( date->j_date );
00176 }      /*     end of double juldat( date )       */