Exemplo n.º 1
0
static void show_small_month( int month, int year, const int x0, int y0)
{
   long jd1, jd2;
   int n_days, starting_loc, i, j, day;
   char buff[100];

   while( month <= 0)
      {
      month += 12;
      year--;
      }
   while( month >= 13)
      {
      month -= 12;
      year++;
      }
   jd1 = dmy_to_day( 0, month, (long)year, CALENDAR_JULIAN_GREGORIAN);
   jd2 = dmy_to_day( 0, month + 1, (long)year, CALENDAR_JULIAN_GREGORIAN);
   n_days = (int)( jd2 - jd1);
   starting_loc = (int)( (jd1 + 2L) % 7L);
   strcpy( buff, months[month - 1]);
   sprintf( buff + strlen( buff), " %d", year);
   y0 -= 12;
   printf( "%d %d moveto (%s) show\n",
            x0 + (28 - (int)strlen( buff)) * TEXT_XOFFSET / 2, y0, buff);
   for( i = 0; i < 6; i++)
      {
      y0 -= 12;
      memset( buff, ' ', 20);
      for( j = 0; j < 7; j++)
         {
         day = i * 7 + j - starting_loc + 1;
         if( day >= 1 && day <= n_days)
            {
            buff[j * 3] = (char)( '0' + day / 10);
            buff[j * 3 + 1] = (char)( '0' + day % 10);
            if( day < 10)
               buff[j * 3] = ' ';
            }
         }
      for( j = 20; j && buff[j - 1] == ' '; j--)
         ;
      buff[j] = buff[20] ='\0';
      if( j)
         printf( "%d %d moveto (%s) show\n", x0, y0, buff);
      }
   if( show_jd_values)
      printf( "%d %d moveto ((JD %ld.5)) show\n", x0, y0 - 12, jd1 - 1);
}
Exemplo n.º 2
0
static void show_month_text( const int month, const int year)
{
   char buff[80];

   sprintf( buff, "%s %d", months[month - 1], year);
   if( show_jd_values)
      sprintf( buff + strlen( buff), " (JD %ld.5)",
                  dmy_to_day( 0, month, (long)year, CALENDAR_JULIAN_GREGORIAN) - 1);
   printf( "%d %d moveto (%s) show\n",
            X0 + 7 * XSIZE / 2 - (int)strlen( buff) * 20 / 3,
            TOP_OF_DAYOFWK + 5, buff);
}
Exemplo n.º 3
0
double get_lunar_transit_time( const int year, const int month,
   const int day, const double latitude, const double longitude,
   const int time_zone, const int dst, const int real_transit)
{
   const long jd0 = dmy_to_day( day, month, year, 0);
   const long day_of_year = jd0 - dmy_to_day( 1, 1, year, 0);
   double jd = (double)jd0;
   static char *vsop_data = NULL;
   int dst_hours = (dst ? is_dst( 720, (int)day_of_year, year) : 0);

   if( !vsop_data)
      vsop_data = load_file_into_memory( "vsop.bin", NULL);
   if( !vsop_data)
      return( -2.);
   jd -= (double)time_zone / 24.;      /* convert local to UT */
   jd -= dst_hours / 24.;

   jd = look_for_transit_time( 10, jd,
            latitude * pi / 180., longitude * pi / 180.,
            vsop_data, real_transit);
   jd += (double)time_zone / 24.;      /* convert UT back to local */
   jd += dst_hours / 24.;
   return( jd - (double)jd0 + .5);
}
Exemplo n.º 4
0
int calendar( const int month, const int year)
{
   int i, j;
   const long jd1 = dmy_to_day( 0, month, (long)year, CALENDAR_JULIAN_GREGORIAN);
   const long jd2 = dmy_to_day( 0, month + 1, (long)year, CALENDAR_JULIAN_GREGORIAN);
   const int n_days = (int)( jd2 - jd1), starting_loc = (int)( (jd1 + 1L) % 7L);
   char buff[100];
   char lines_used[35], phases_shown[35];
   char **dates = grab_dates( month, year);


   printf( "%%!PS-Adobe-2.0\n");
   printf( "%%%%Pages: %d\n", (dollhouse || single_page ? 1 : 7));
   printf( "%%%%PageOrder: Ascend\n");
// printf( "%%%%BoundingBox: 48 69 556 746\n");
   printf( "%%%%Creator: calendar.cpp\n");
   printf( "%%%%Copyright: none\n");
   printf( "%%%%Title: Calendar for %s %d\n", months[month - 1], year);
   printf( "%%%%Version: none\n");
   printf( "%%%%DocumentData: Clean7Bit\n");
   printf( "%%%%EndComments\n");
   printf( "%%%%BeginDefaults\n");
   printf( "%%%%PageResources: font Times-Roman\n");
   printf( "%%%%PageResources: font Times-Italic\n");
   printf( "%%%%PageResources: font Courier-Bold\n");
   printf( "%%%%EndDefaults\n");
   printf( "/blood {\n");
   printf( "/aa 2 def /bb 1.7 def\n");
   printf( "aa 0 rmoveto bb 0 rlineto 0 aa rlineto aa 0 rlineto 0 bb rlineto\n");
   printf( "0 aa sub 0 rlineto 0 aa rlineto 0 bb sub 0 rlineto\n");
   printf( "0 0 aa sub rlineto 0 aa sub 0 rlineto\n");
   printf( "0 0 bb sub rlineto aa 0 rlineto 0 0 aa sub rlineto aa 3 mul 0 rmoveto\n");
   printf( "} def\n\n");
   printf( "/calendar {\n");
   printf( "612 0 translate 90 rotate\n");
   for( i = 0; i <= 5; i++)             /* horizontal lines separating weeks */
      printf( "%d %d moveto %d %d lineto\n", X0, Y0 + i * YSIZE,
                                           XEND, Y0 + i * YSIZE);
   printf( "%d %d moveto %d %d lineto\n", X0, TOP_OF_DAYOFWK,
                                        XEND, TOP_OF_DAYOFWK);

   for( i = 0; i <= 7; i++)       /* vertical lines */
      printf( "%d %d moveto %d %d lineto\n", X0 + i * XSIZE, Y0,
                                             X0 + i * XSIZE, YEND);
   printf( "/defaultfontsize  { 12 scalefont } def\n");
   printf( "/Times-Roman findfont defaultfontsize setfont\n");
   for( i = 0; i < 35; i++)
      lines_used[i] = phases_shown[i] = 0;

   for( i = 1; i <= n_days; i++)
      {
      int xcell = (i + starting_loc) % 7, curr_font = FONT_UNSET;
      int ycell = 4 - (i + starting_loc) / 7, double_cell = 0, x0, y0;
      int cell_idx;

      if( starting_loc == 6)        /* first of month is on a sunday */
         ycell++;
      if( !ycell && i + 7 <= n_days)     /* two days in one cell */
         {
         sprintf( buff, "%d/%d", i, i + 7);
         double_cell = 1;
         }
      else
         sprintf( buff, "%d", i);
      x0 = X0 + xcell * XSIZE;
      y0 = Y0 + (ycell + 1) * YSIZE;
      if( ycell >= 0)
         {
         int xboxsize = TEXT_XOFFSET + 8 * strlen( buff);

         printf( "%d %d moveto (%s) show\n",
                     x0 + TEXT_XOFFSET, y0 - TEXT_YOFFSET, buff);
         printf( "%d %d moveto %d %d rlineto %d %d rlineto\n",
               x0, y0 - (TEXT_YOFFSET + 2),
               xboxsize, 0, 0, TEXT_YOFFSET + 2);
         }
      else
         {
         double_cell = 1;
         ycell = 0;
         y0 = Y0 + YSIZE;
         }
      cell_idx = (4 - ycell) * 7 + xcell;
      for( j = 0; dates[j]; j++)
         if( dates[j][0] == i)
            {
            char *text_to_show = dates[j] + 1;
            const int is_lunar_phase = (*text_to_show == '*');
            const int font_to_use = (is_lunar_phase ? FONT_ITALIC : FONT_PLAIN);
            int xloc = X0 + xcell * XSIZE + TEXT_XOFFSET +
                           ((is_lunar_phase && !double_cell) ? 24 : 0);
            int yloc = (is_lunar_phase ?
                         y0 - TEXT_YOFFSET * (1 + double_cell)
                                        - phases_shown[cell_idx] * 11 :
                         y0 - YSIZE + 3 + lines_used[cell_idx] * 11);

            if( curr_font != font_to_use)
               {
               curr_font = font_to_use;
               if( curr_font == FONT_ITALIC)
                  printf( "/Times-Italic findfont 9 scalefont setfont\n");
               if( curr_font == FONT_PLAIN)
                  printf( "/Times-Roman findfont 9 scalefont setfont\n");
               }
            if( is_lunar_phase)     /* lunar phase */
               {
               text_to_show++;
               }

            printf( "%d %d moveto ", xloc, yloc);
            if( double_cell)
               printf( "(\050%d\051 ) show ", i);
            if( *text_to_show == 'B' && text_to_show[1] == ' ')
               {
               printf( "blood ");
               text_to_show += 2;
               }
            printf( "(%s) show\n", text_to_show);
            if( is_lunar_phase)
               phases_shown[cell_idx]++;
            else
               lines_used[cell_idx]++;
            }
      if( !lines_used[cell_idx])         /* mark it as being a "used" cell */
         lines_used[cell_idx] = 1;
      if( curr_font != FONT_UNSET)
         printf( "/Times-Roman findfont defaultfontsize setfont\n");
      }

   for( i = 0; i < 7; i++)
      {
      const char *day_of_week_text[7] = { "Sunday", "Monday", "Tuesday",
                           "Wednesday", "Thursday", "Friday", "Saturday" };

      printf( "%d %d moveto (%s) show\n",
                              X0 + i * XSIZE + XSIZE / 2 -
                              (int)strlen( day_of_week_text[i]) * 4,
                              Y0 + 5 * YSIZE + 5,
                              day_of_week_text[i]);
      }

   printf( "/Times-Roman findfont 24 scalefont setfont\n");

   show_month_text( month, year);

   printf( "/Courier-Bold findfont 8 scalefont setfont\n");

               /* show two preceding & two (or more) following months: */
   for( i = j = 0; i < 35; i++)
      if( !lines_used[i])
         {
         show_small_month( month + j - 2, year,
                     X0 + (i % 7) * XSIZE + 1, Y0 + YSIZE * (5 - i / 7));
         j++;
         if( j == 2)    /* avoid duplicating the 'main' month */
            j = 3;
         }
   printf( "stroke showpage } def\n");
   if( dollhouse)
      for( i = 0; dollhouse_trailer_data[i]; i++)
         printf( "%s\n", dollhouse_trailer_data[i]);
   else
      for( i = 0; (single_page ? i < 3 : trailer_data[i] != NULL); i++)
         printf( "%s\n", trailer_data[i]);
   return( 0);
}
Exemplo n.º 5
0
int main( int argc, char **argv)
{
   int i, j, k, julian = 0, sat_no = 15, n_days = 30, gap;
   int n_events = 0, n_sun, n_earth;
   double t1, t2;
   long jd;
   EVENT *e;
   FILE *ofile = NULL, *data_file = NULL;
   FILE *vsop_file;
   char *vsop_tbuff;

   vsop_file = fopen( "vsop.bin", "rb");
   if( !vsop_file)
      {
      printf( "Couldn't open vsop.bin");
      return( -3);
      }
   vsop_tbuff = (char *)malloc( VSOP_CHUNK);
   vsop_data = (char FAR *)FMALLOC( VSOP_CHUNK * 22U);
   for( i = 0; i < 22; i++)
      {
      fread( vsop_tbuff, VSOP_CHUNK, 1, vsop_file);
      memcpy( vsop_data + (unsigned)i * VSOP_CHUNK, vsop_tbuff, VSOP_CHUNK);
      }
   fclose( vsop_file);
   free( vsop_tbuff);

   if( argc < 4)
      {
      printf( "JEVENT calculates Jovian satellite events (transits, eclipses,\n");
      printf( "shadows,  occultations) as seen from Earth.  It requires, as a\n");
      printf( "minimum,  a day,  month,  and year.  Given,  say,  the command\n");
      printf( "\nJEVENT 18 4 1993\n\n");
      printf( "JEVENT will calculate all events from 18 Apr 1993 for the next\n");
      printf( "thirty days.  You can add on the following parameters:\n\n");
      printf( "   -j        Use Julian calendar\n");
      printf( "   -d(#)     Calculate for (#) days instead of 30\n");
      printf( "   -f(name)  Put results in ASCII file (name) as well as on screen\n");
      return( -2);
      }
   for( i = 0; i < argc; i++)
      if( argv[i][0] == '-')
         switch( argv[i][1])
            {
            case 'j': case 'J':
               julian = 1;
               break;
            case 'q': case 'Q':
               quiet = 1;
               break;
            case 's': case 'S':
               sat_no = atoi( argv[i] + 2);
               break;
            case 'd': case 'D':
               n_days = atoi( argv[i] + 2);
               break;
            case 'f': case 'F':
               ofile = fopen( argv[i] + 2, "wb");
               break;
            case 'r': case 'R':
               data_file = fopen( argv[i] + 2, "ab");
               printf( "Appending data to %s\n", argv[i] + 2);
               break;
            default:
               break;
            }
   e = (EVENT *)calloc( MAX_EVENTS, sizeof( EVENT));
   if( !e)
      return( -1);
   jd = dmy_to_day( 0, atoi( argv[2]), atol( argv[3]), julian);
   t1 = (double)jd - .5 + atof( argv[1]);
   t2 = t1 + (double)n_days;
   printf( "JD %lf to %lf\n", t1, t2);
   for( i = 0; i < 4; i++)
      if( sat_no & (1 << i))
         {
         printf( "Sat %d from sun\n", i + 1);
         n_sun = find_events( i + 1, t1 - 1., t2 + 1., 0, e + n_events);
         printf( "Sat %d from earth\n", i + 1);
         n_earth = find_events( i + 1, t1 - 1., t2 + 1., 1, e + n_events + n_sun);
         printf( "Finding hidden events\n");
         k = n_events + n_sun;
         for( j = n_events; j < n_events + n_sun; j++)
            while( k < n_events + n_sun + n_earth && e[k].t < e[j].t)
               {
               if( e[k + 1].t > e[j].t)
                  if( !(e[j].event_type & IN_FRONT))
                     e[j].event_type |= EVENT_UNSEEN;
               k += 2;
               }
         k = n_events;
         for( j = n_events + n_sun; j < n_events + n_sun + n_earth; j++)
            while( k < n_events + n_sun && e[k].t < e[j].t)
               {
               if( e[k + 1].t > e[j].t)
                  if( !(e[j].event_type & IN_FRONT))
                     e[j].event_type |= EVENT_UNSEEN;
               k += 2;
               }
         n_events += n_earth + n_sun;
         }
   printf( "Sorting %d events\n", n_events);
   for( gap = 1; gap < n_events / 3; gap = gap * 3 + 1)
      ;
   while( gap)
      {
      for( i = 0; i < gap; i++)
         for( j = i; j + gap < n_events; j += gap)
            if( e[j].t > e[j + gap].t)
               {
               EVENT temp;

               memcpy( &temp, e + j + gap, sizeof( EVENT));
               memcpy( e + j + gap, e + j, sizeof( EVENT));
               memcpy( e + j, &temp, sizeof( EVENT));
               if( j >= gap)
                  j -= gap + gap;
               }
      gap /= 3;
      }

   if( !quiet)
      {
      printf( "Final results:\n");
      for( i = 0; i < n_events; i++)
         if( e[i].t > t1 && e[i].t < t2)
            show_event( stdout, e + i);
      }

   if( ofile)
      {
      for( i = 0; i < n_events; i++)
         if( e[i].t > t1 && e[i].t < t2)
            show_event( ofile, e + i);
      fclose( ofile);
      }

   if( data_file)
      {
      for( i = 0; i < n_events; i++)
         if( e[i].t > t1 && e[i].t < t2)
            if( !( e[i].event_type & EVENT_UNSEEN))
               {
               char buff[5];

                        /* store minutes from 2000.0 */
               *(long *)buff = (long)( (e[i].t - 2451545.0) * 1440.);
               buff[4] = (char)( (e[i].event_type & 7) | (e[i].sat << 3));
               fwrite( buff, 1, 5, data_file);
               }
      fclose( data_file);
      }
   return( 0);
}
Exemplo n.º 6
0
double DLL_FUNC get_time_from_string( double initial_jd,
         const char *time_str, const int time_format, int *is_ut)
{
   const double J2000 = 2451544.5;         /* 1.0 Jan 2000 = JD 2451544.5 */
   const int calendar = (time_format & CALENDAR_MASK);
            /* Certain solar-lunar calendars can have 13 months in a year: */
   const int max_month =
             ((calendar == CALENDAR_HEBREW || calendar == CALENDAR_CHINESE)
                    ? 13 : 12);
   int iday, month, hour, minute, n_bytes;
   int ival, i, colon_found = 0, is_bc = 0;
   int am_pm_indicator = AM_PM_UNSET;
   long year;
   double sec, dday;
   double rval = 0., offset = 0., tval;
   char buff[80];
   char symbol = 0;
   char *str = buff;

   if( is_ut)
      *is_ut = 0;
   while( *time_str == ' ')         /* skip leading spaces */
      time_str++;
   if( strlen( time_str) >= sizeof( buff) || !*time_str)
      return( initial_jd);       /* check/avoid possible buffer overflow */
             /* Ensure spaces between letters and digits.  For example,   */
             /* if time_str="11Nov 1918",  set str="11 Nov 1918".         */
             /* 'Q' is an exception to avoid '1q' becoming '1 q'.         */
   *str = tolower( *time_str++);
   i = 1;
   while( (size_t)i < sizeof( buff) - 1 && *time_str)
      {
      if( (isalpha( *time_str) && isdigit( time_str[-1]))
                         || (isdigit( *time_str) && isalpha( time_str[-1])))
         if( *time_str != 'q' && *time_str != 'Q')
            str[i++] = ' ';
      str[i++] = (char)tolower( *time_str++);
      }
   str[i] = '\0';
   remove_trailing_spaces( str);
   while( *str && (tval = collect_time_offset( str)) != 0.)
      {
      remove_trailing_spaces( str);
      offset += tval;
      }

   i = strlen( str);
   if( i > 1)
      {
      const int phase = get_phase_idx( str + i - 2);

      if( phase != PHASE_IDX_UNDEFINED)
         {
         str[i - 2] = '\0';
         rval = get_time_from_string( initial_jd, str, time_format, NULL);
         if( rval && is_ut)
            *is_ut = 1;
         return( set_from_lunar( phase, rval) + offset);
         }
      }

   is_bc = check_for_bc( str);

   if( *str == 'j')           /* decimal JD */
      {                       /* may begin with 'j' or 'jd' */
      if( str[1] == 'd')
         str++;
      rval = atof( str + 1);
      if( rval && is_ut)
         *is_ut = 1;
      }

   if( *str == 'y')      /* decimal years */
      rval = J2000 + (atof( str + 1) - 2000.) * 365.25;

   if( !memcmp( str, "mjd", 3))                 /* modified JD */
      {
      rval = atof( str + 3) + 2400000.5;
      if( is_ut)
         *is_ut = 1;
      }

   if( !memcmp( str, "now", 3))
      {
      static const double jan_1970 = 2440587.5;

      str += 3;
      while( *str == ' ')
         str++;
      initial_jd = jan_1970 + (double)time( NULL) / 86400.;
      }

   if( !*str)
      rval = initial_jd;

   if( rval)
      return( rval + offset);

               /* The common European format of separating day/month/year */
               /* with .s causes trouble,  because the code wants to see  */
               /* those as decimal numbers.  So if the input string starts */
               /* with three integers separated by dots,  we change both   */
               /* dots to '/' characters, then proceed normally:           */
   if( sscanf( str, "%d.%d.%d%n", &iday, &month, &hour, &n_bytes) == 3)
      for( i = 0; i < n_bytes; i++)
         if( str[i] == '.')
            str[i] = '/';

   sec = split_time( initial_jd, &year, &month, &iday, &hour,
                                         &minute, calendar);

               /* FITS times are always in the form YYYY-MM-DDTHH:MM:SS, */
               /* sometimes followed by .S.  This is handled separately, */
               /* in part to ensure that the month and day don't get     */
               /* swapped around:  they are _always_ in that order. Also */
               /* note that after spaces are added and the 'T' lowercased, */
               /* it actually reads YYYY-MM-DD t HH:MM:SS.                 */
   i = strlen( str);
   if( i > 18 && str[11] == 't')
      if( str[4] == '-' && str[7] == '-' && str[15] == ':')
         {
         symbol = 'f';
         sscanf( str, "%ld-%d-%d", &year, &month, &iday);
         }

   if( i >= 4)
      {
      const char *search_text[4] = { " am", " a.m.", " pm", " p.m." };
      int j;

      for( j = 0; j < 4; j++)
         {
         const int slen = 3 + ((j & 1) << 1);

         if( !strcmp( str + i - slen, search_text[j]))
            {
            i -= slen;
            str[i] = '\0';
            am_pm_indicator = (j >= 2 ? AM_PM_SET_TO_PM :
                                        AM_PM_SET_TO_AM);
            j = 4;   /* break out of loop */
            }
         }
      }

            /* If the input text ends with something containing ':'s,     */
            /* assume there is a time to be extracted.  Back up along the */
            /* string,  looking for the start of the time string (which   */
            /* may be the beginning of the string,  or just after a space, */
            /* or (for FITS input) just after a 'T'... for simplicity,     */
            /* that last test just checks for any alphabetical char.)      */
   for( i = strlen( str); i && str[i - 1] != ' ' && !isalpha( str[i - 1]); i--)
      if( str[i - 1] == ':')
         colon_found = 1;

   if( strcmp( str + i, ":"))
      {
      const int saved_hour = hour;

      minute = hour = 0;
      sec = 0.;
      if( colon_found)
         {
         double dminute = 0.;

         if( str[i] != ':')
            {
            sscanf( str + i, "%d:%lf:%lf", &hour, &dminute, &sec);
            sec += dminute * 60.;
            if( am_pm_indicator == AM_PM_SET_TO_AM)
               if( hour == 12)
                  hour = 0;
            if( am_pm_indicator == AM_PM_SET_TO_PM)
               if( hour != 12)
                  hour += 12;
            }
         else      /* :MM:SS means "leave the hour unchanged" */
            {
            hour = saved_hour;
            sscanf( str + i + 1, "%lf:%lf", &dminute, &sec);
            sec += dminute * 60.;
            }
         }
      }
   if( colon_found)           /* lop the time off, leaving only the date: */
      str[i ? i - 1 : 0] = '\0';

   dday = (double)iday;
   i = 0;
   if( *str && symbol != 'f')
      {
      for( i = 1; str[i] && !strchr( "-:/ ", str[i]); i++)
         ;
      symbol = str[i];
      }
   switch( symbol)
      {
      case 'f':               /* FITS-format time: see above */
         break;
      case ':':               /* time of day */
         break;
      case '-':               /* dash-delimited such as '2009-01-20' */
      case ' ':               /* space-delimited format such as "25 dec 1980" */
      case '/':               /* common day/month/year dividing symbol */
         {
         int month_found = 0, n_fields_found = 2;
         int year_found = 0, day_found = 0;
         char tstr[80];
         double ivals[3];

         memcpy( tstr, str, i);
         tstr[i] = '\0';
         ival = month_name_to_index( tstr);
         if( ival)         /* month given first, such as 'jan 25' */
            {
            month_found = 1;
            ivals[0] = (double)ival;
            }
         else
            {
            ivals[0] = atof( tstr);
            if( strchr( tstr, '.'))   /* decimal day given */
               day_found = 1;
            }
         str += i + 1;
         for( i = 0; str[i] && str[i] != symbol && str[i] != ' '; i++)
            ;
         memcpy( tstr, str, i);
         tstr[i] = '\0';
         str += i;
         ival = month_name_to_index( tstr);
         if( ival)         /* month given second, such as '25-jan' */
            {
            month_found = 2;
            ivals[1] = (double)ival;
            }
         else
            {
            ivals[1] = atof( tstr);
            if( strchr( tstr, '.'))   /* decimal day given */
               day_found = 2;
            }

         if( *str == symbol)     /* maybe a third field was entered, but */
            {                       /* could be a time;  check for a ':' */
            str++;
            if( sscanf( str, "%79s", tstr) == 1)
               if( (ival = month_name_to_index( tstr)) != 0)
                  {
                  month_found = 3;
                  n_fields_found = 3;
                  ivals[2] = (double)ival;
                  str += strlen( tstr);
                  }
            if( n_fields_found == 2)
               if( sscanf( str, "%lf%n", &ivals[2], &n_bytes) == 1)
                  if( str[n_bytes] != ':')
                     {
                     str += n_bytes;
                     if( strchr( tstr, '.'))
                        day_found = 3;
                     n_fields_found = 3;
                     }
            }
                     /* if one of the fields is negative, or if it's    */
                     /* greater than 32 and is the largest entry,  it   */
                     /* can be assumed to be the year:                  */
         for( i = 0; i < n_fields_found; i++)
            if( ivals[i] < 0.)
               {
               year_found = i + 1;   /* if we see a negative number, */
               i = n_fields_found;   /* we can stop looking further: */
               }
            else if( ivals[i] > 32.)
               if( !year_found || ivals[i] > ivals[year_found - 1])
                  year_found = i + 1;
         if( year_found || n_fields_found == 2)
            for( i = 0; i < n_fields_found; i++)
               if( ivals[i] > (double)max_month + .0001 && ivals[i] < 32.
                                      && i + 1 != year_found)
                  day_found = i + 1;

         if( n_fields_found == 2)
            {
            if( month_found)
               {
               double dval = ivals[2 - month_found];

               month = (int)ivals[month_found - 1];
               if( dval > .999 && dval < 32.)
                  dday = dval;
               else
                  year = (int)dval;
               }
            else if( year_found)         /* year/day of year format: */
               {
               year = (int)ivals[year_found - 1];
               month = 1;
               dday  = ivals[2 - year_found];
               }
            else if( day_found)     /* day/month, order is clear from */
               {                    /* the day being > 12 or having a decimal*/
               dday = ivals[day_found - 1];
               month = (int)ivals[2 - day_found];
               }
            else    /* can't tell what's day/month/year solely from input: */
               if( (time_format & FULL_CTIME_MONTH_DAY) || symbol == 'f')
                  {
                  month = (int)ivals[0];
                  dday = (int)ivals[1];
                  }
               else              /* day/month */
                  {
                  month = (int)ivals[1];
                  dday = (int)ivals[0];
                  }
            }
         else        /* three fields entered: */
            {
            const int year_first = (time_format & FULL_CTIME_YEAR_FIRST);

            if( !year_found)
               {
               if( !month_found)
                  {
                  if( !day_found || day_found == 2)
                     {
                     year_found = (year_first ? 1 : 3);
                     if( !day_found)   /* must rely solely on time format */
                        {              /* settings;  no fields autofound  */
                        day_found = (year_first ? 2 : 1);
                        if( (time_format & FULL_CTIME_MONTH_DAY) || symbol == 'f')
                           day_found++;      /* ymd or mdy case */
                        }
                     }
                  else     /* if day is 1st or last, year is last or 1st */
                     year_found = 4 - day_found;
                  }
               else if( !day_found)    /* only the month was found: */
                  {
                  if( month_found == 2)
                     year_found = (year_first ? 1 : 3);
                  else   /* if month is 1st or last, year must be last/1st */
                     year_found = 4 - month_found;
                  }
               }
            else        /* year_found... */
               if( !month_found && !day_found)  /* ...but nothing else */
                  {
                  if( (time_format & FULL_CTIME_MONTH_DAY) || symbol == 'f')
                     month_found = (year_found == 1 ? 2 : 1);
                  else
                     day_found = (year_found == 1 ? 2 : 1);
                  }
                  /* We now have the year nailed down.  If either the day */
                  /* or month is still not nailed down, we can find it    */
                  /* easily, since the 'found' values must sum up to 6:   */

            if( !day_found)
               day_found = 6 - year_found - month_found;
            else if( !month_found)
               month_found = 6 - year_found - day_found;
            year = (int)floor( ivals[year_found - 1] + .5);
            dday = ivals[day_found - 1];
            month = (int)( ivals[month_found - 1] + .5);
            }

         if( year > 0 && year < 100 && !is_bc)
            if( time_format & FULL_CTIME_TWO_DIGIT_YEAR)
               year += (year < 40 ? 2000 : 1900);
         }
         break;
      case '\0':       /* no dividing symbols found */
         if( *str)
            {
            ival = month_name_to_index( str);
            if( ival)
               month = ival;
            else if( (ival = day_of_week_name_to_index( str)) >= 0)
               {
               ival -= ((long)(initial_jd + 1.5)) % 7;
               if( ival < -3)
                  ival += 7;
               else if( ival > 3)
                  ival -= 7;
               dday += ival;
               }
            else
               {
               n_bytes = 0;
               if( sscanf( str, "%d%n", &ival, &n_bytes) == 1)
                  {
                  double tval = 0.;

                  str += n_bytes;
                  if( *str == '.')     /* also a fractional part to this: */
                     {
                     int n_bytes_in_fraction;

                     sscanf( str, "%lf%n", &tval, &n_bytes_in_fraction);
                     str += n_bytes_in_fraction;
                     }
                  tval += (double)ival;
                  switch( n_bytes)
                     {
                     case 1:
                     case 2:                 /* reset day */
                        dday = tval;
                        break;
                     case 3:                 /* reset day of year */
                        dday = tval;
                        month = 1;
                        break;
                     case 4:                 /* reset year, which may be */
                     case 5:                 /* four or five digits long */
                        if( (double)ival == tval)
                           {                    /* set 1 Jan of the year */
                           dday = 1.;
                           month = 1;
                           year = ival;
                           }
                        else        /* true decimal year */
                           return( J2000 + (tval - 2000.) * 365.25 + offset);
                        break;
                     case 7:               /* JD */
                        if( is_ut)
                           *is_ut = 1;
                        return( tval + offset);
                     case 6:     /* YYMMDD(.DD)   */
                     case 8:     /* YYYYMMDD(.DD) */
                        year = ival / 10000L;
                        if( n_bytes == 6)
                           year += (year < 40 ? 2000 : 1900);
                        month = (ival / 100) % 100L;
                        dday = (double)( ival % 100L);
                        dday += tval - (double)ival;
                        break;
                     }
                  }
               else        /* couldn't make sense of input text */
                  return( 0.);
               }
            }
         break;
      default:
         return( 0.);
//       break;
      }

   if( is_bc)
      year = 1 - year;
   iday = (int)dday;
   dday -= (double)iday;
   rval = (double)dmy_to_day( iday, month, year, calendar) + dday - .5 +
                 (double)hour / 24. + (double)minute / 1440. + sec / 86400.;
   return( rval + offset);
}