/*-----------------------------------------------------------------------------

    FUNCTION: jd_to_ddtime

              Converts a julian date structure into a double count of
              the number of days elapsed since the given year_base.

    RETURNS:  decimal days-equivalent of julian date structure

    USED BY:  REFABSBT, REFABS, TIMSLIP, NAV2MAT

*/

#include "time_.h"     /* YMDHMS_TIME_TYPE, TIMDIF(), CUM_MONTH_DAYS */
#include "jdtime.h"    /* JD_TIME_TYPE */
#include "ioserv.h"    /* yr4digit() */

#if PROTOTYPE_ALLOWED
double jd_to_ddtime(JD_TIME_TYPE *j, int year_base)
#else
double jd_to_ddtime(j, year_base)
JD_TIME_TYPE *j;
int year_base;
#endif
{
   YMDHMS_TIME_TYPE t, ref;

   t.year = yr4digit(j->year);
   ref.year = year_base;
   t.month = ref.month = t.day = ref.day = 1;
   t.hour = ref.hour = t.minute = ref.minute = t.second = ref.second = 0;

   return((j->julian_day-1) + j->hour / 24.0 + j->minute / 1440.0
      + (j->second + TIMDIF(&ref,&t)) / 86400.0);
}

/*-----------------------------------------------------------------------------

    FUNCTION: jd_timcmp

              Compares 2 julian date structures.

    RETURNS: -1 if t1 < t2
              0 if t1 = t2
              1 if t1 > t2
*/

#if PROTOTYPE_ALLOWED
int jd_timcmp(JD_TIME_TYPE *t1, JD_TIME_TYPE *t2)
#else
int jd_timcmp(t1, t2)
JD_TIME_TYPE *t1, *t2;
#endif
{
   if (t1->year < t2->year) return(-1);
   if (t1->year > t2->year) return(1);
   if (t1->julian_day < t2->julian_day) return(-1);
   if (t1->julian_day > t2->julian_day) return(1);
   if (t1->hour < t2->hour) return(-1);
   if (t1->hour > t2->hour) return(1);
   if (t1->minute < t2->minute) return(-1);
   if (t1->minute > t2->minute) return(1);
   if (t1->second < t1->second) return(-1);
   if (t1->second > t2->second) return(1);
   return(0);
}

/*-----------------------------------------------------------------------------

    FUNCTION: jd_to_ymdhms_time

              Converts a julian date structure to a YMDHMS time structure.

    RETURNS:  0 if valid time
              1 otherwise
*/

#if PROTOTYPE_ALLOWED
int jd_to_ymdhms_time(YMDHMS_TIME_TYPE *y, JD_TIME_TYPE *j)
#else
int jd_to_ymdhms_time(y, j)
YMDHMS_TIME_TYPE *y;
JD_TIME_TYPE     *j;
#endif
{
   y->year   = yr4digit(j->year);
   y->hour   = j->hour;
   y->minute = j->minute;
   y->second = j->second;

   y->month  = j->julian_day / 29;
   if (!(y->year % 4) && ((y->year % 100) || !(y->year % 400)))
   {
      if (j->julian_day > (CUM_MONTH_DAYS[y->month+1] + (j->julian_day > 59)))
         (y->month)++;
      y->day = j->julian_day - (CUM_MONTH_DAYS[y->month] + (j->julian_day > 60));
   }
   else
   {
      if (j->julian_day > CUM_MONTH_DAYS[y->month+1]) 
         (y->month)++;
      y->day    = j->julian_day - CUM_MONTH_DAYS[y->month];
   }
   return(invalid_time(y));
}

/*-----------------------------------------------------------------------------

    FUNCTION: ymdhms_to_jdtime

              Converts a ymdhms time structure to a julian date structure

    RETURNS:  0 if valid time
              1 otherwise
*/

#if PROTOTYPE_ALLOWED
int ymdhms_to_jdtime(JD_TIME_TYPE *j, YMDHMS_TIME_TYPE *y)
#else
int ymdhms_to_jdtime(j, y)
JD_TIME_TYPE     *j;
YMDHMS_TIME_TYPE *y;
#endif
{
   if (invalid_time(y)) return(1);
   j->year       = yr4digit(y->year);
   j->julian_day = CUM_MONTH_DAYS[y->month] + y->day +
      (!(y->year %4) && ((y->year % 100) || !(y->year % 400)) && y->month > 2);
   j->hour       = y->hour;
   j->minute     = y->minute;
   j->second     = y->second;
   return(0);
}

