⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 darkcal.cpp

📁 This is a pgm can be used for astronomy
💻 CPP
📖 第 1 页 / 共 2 页
字号:
  else if ( DST_END == dstDay )
    p = append( p, "STime", 5 );

  return nextColumn(p, 10-(p-pStart), (p==pStart));
}

//----------------------------------------------------------------------------
// The main program function
//
void calcAndPrint(CalData& cd)
{
  // calc. start and end days
  //
  long jd_start = DateOps::dmyToDay( 1, cd.month, cd.year );
  long jd_end = ( cd.month < 12 ) ?
      DateOps::dmyToDay( 1, cd.month + 1, cd.year ) :
      DateOps::dmyToDay( 1, 1, cd.year + 1 );

  int end = int(jd_end - jd_start);

  // fill in data for month in question
  //
  TimePair sunRS[DAYS], moonRS[DAYS], astTwi[DAYS];
  double jd[DAYS];
  static const double hourFraction = 1./24.;

  double tzAdj = (double)cd.loc.timeZone() * hourFraction;
  long dstStart = DateOps::dstStart( cd.year );
  long dstEnd = DateOps::dstEnd( cd.year );

  #if defined( PROGRESS_BAR )
    fprintf( stderr, "working" );
  #endif

  for( int i=0; i<=end+1; i++ )
  {
    long day = jd_start + i;

    // automatically adjust for DST if enabled
    // This 'rough' method will be off by one on moon rise/set between
    //   midnight and 2:00 on "clock change" days. (sun & astTwi never
    //   occur at these times.)
    //
    double dstAdj =
        ( false == g_ignoreDst && day>=dstStart && day<dstEnd) ?
        hourFraction : 0.;

    jd[i] = (double)day - (tzAdj + dstAdj) - .5;

    // calculate rise/set times for the sun
    RiseSet::getTimes( sunRS[i], RiseSet::SUN, jd[i], cd.loc );

    // calculate rise/set times for Astronomical Twilight
    RiseSet::getTimes( astTwi[i], RiseSet::ASTRONOMICAL_TWI, jd[i], cd.loc );

    // calculate rise/set time for Luna )
    RiseSet::getTimes( moonRS[i], RiseSet::MOON, jd[i], cd.loc );

    #if defined( PROGRESS_BAR )
      fputc( '.', stderr );
    #endif
  }
  fputc( '\n', stderr );

  printHeading(cd);

  // print data for each day
  //
  char buf[256];
  for( int i=0; i<end; i++ ) {

    if (g_html) {
      if ( !(i&1) )
        fprintf( g_fp, "<TR CLASS=\"bar\">\n  <TD>" );
      else
        fprintf( g_fp, "<TR>\n  <TD>" );
    }

    // print day
    char* p = printDay( buf, jd_start, i, false );

    // print darkest hours
    p = printDarkness(p, i, astTwi, moonRS);

    // check for lunar & solar quarters and DST, print if found
    DST dstDay = DST_NONE;
    if ( dstStart == jd_start+i )
      dstDay = DST_START;
    else if ( dstEnd == jd_start+i )
      dstDay = DST_END;

    p = printEvents(p, i, jd, cd.loc, dstDay);

    // print rise/set times for Luna
    p = printTime( p, moonRS[i].TP_RISE );
    p = printTime( p, moonRS[i].TP_SET );

    // print set time for the sun
    p = printTime( p, sunRS[i].TP_SET );

    // print end of Astronomical Twilight */
    p = printTime( p, astTwi[i].TP_END );

    // next day
    p = printDay( p, jd_start, i+1, i == end-1 );
    if (!g_tabDelimited && !g_html)
      *p++ = ' ';

    // print start of Astronomical Twilight */
    p = printTime( p, astTwi[i+1].TP_START );

    // print rise time for the sun     (last column)
    p = printTime( p, sunRS[i+1].TP_RISE, false );

    if (g_html)
      strcpy( p, "</TD>\n</TR>\n" );
    else {
      *p++ = '\n';
      *p = 0;
    }

    fputs( buf, g_fp );
  }
  if (g_html)
    fputs( "</TABLE>\n</BODY>\n</HTML>\n", g_fp );
}

//----------------------------------------------------------------------------
// print usage and exit
//
void usage(const char* pn, bool x = true)
{
  fprintf( stderr,
      "usage: %s <month> <year> [-d] [-h] [-t] [-u]\n"
      "       -d = ignore daylight savings time\n"
      "       -h = HTML table output\n"
      "       -t = tab-delimited output\n"
      "       -u = output time in UTC\n\n"
      "Notes:\n"
      " - Command-line options override config file settings.\n"
      " - To produce a sample config file with all options listed, run the\n"
      "   program without a %s.cfg file in the current directory.\n"
      " - This program currently only supports the Gregorian calendar.", pn, pn );
  exit(-1);
}

//----------------------------------------------------------------------------
//**** main ****
//----------------------------------------------------------------------------
int main( int argc, char* *argv )
{
  // check / get arguments
  //
  if ( argc < 3 )
    usage(argv[0]);
  if ( '-' == argv[1][0] )
    usage( argv[0] );

  CalData cd;

  char cfgFile[256];
  char etcCfgFile[256];
  char* pCfgFile = cfgFile;
  sprintf( cfgFile, "%s%s", argv[0], CFG_EXT );
  sprintf( etcCfgFile, "C:\\etc\\%s", cfgFile );

  ConfigFile cf( pCfgFile );
  if ( ConfigFile::OK != cf.status() ) {
    pCfgFile = etcCfgFile;
    // try etc
    if ( ConfigFile::OK != cf.filename( pCfgFile ) ) {
      fprintf( stderr,
          "\nWarning: unable to find %s or %s:\n\n"
          "- I'll try to create a 'template' file for you in the current directory.\n"
          "- Edit this file to reflect your location.\n ", cfgFile, etcCfgFile );
      FILE* fp = fopen( cfgFile, "w" );
      if ( NULL != fp ) {
        fprintf( fp,
            "# %s\n# Note: East and North are positive\n"
            "# e.g., Philadelphia, PA, US is latitude -75.16, longitude 39.95,\n"
            "#       and timeZone -5\n"
            "longitude=0.0\nlatitude=0.0\ntimeZone=0\n\n"
            "# Set this to true to ignore Daylight time:\n"
            "ignoreDST=false\n\n"
            "# Set this to true to use UTC (this overrides timeZone & DST)\n"
            "useUTC=false\n\n"
            "# Set this to true to output to an HTML file:\n"
            "htmlOutput=false\n\n"
            "# Set this to true to produce a text file with tab-delimited fields:\n"
            "# (Note: html & tabs are mutually exclusive!)\n"
            "tabDelimited=false\n", cfgFile );
        fclose( fp );
        exit(1);
      }
    }
  }
  printf( "Using %s.\n", pCfgFile );

  if ( cf.value( "longitude" ) )
    cd.loc.setLongitude( cf.dblValue( "longitude" ) );

  if ( cf.value( "latitude" ) )
    cd.loc.setLatitude( cf.dblValue( "latitude" ) );

  if ( cf.value( "timeZone" ) )
    cd.loc.setTimeZone( cf.intValue( "timeZone" ) );

  if ( cf.value( "htmlOutput" ) )
    g_html = cf.boolValue( "htmlOutput" );

  if ( cf.value( "ignoreDST" ) )
    g_ignoreDst = cf.boolValue( "ignoreDST" );

  if ( cf.value( "tabDelimited" ) )
    g_tabDelimited = cf.boolValue( "tabDelimited" );

  if ( cf.boolValue( "useUTC" ) ) {
    cd.loc.setTimeZone(0);
    g_ignoreDst = true;
  }

  cd.month = atoi( argv[1] );
  cd.year = atoi( argv[2] );
  if ( cd.month < 1 || cd.month > 12 || cd.year <= 0)
    usage(argv[0]);

  if ( argc > 3 ) {
    for (int i = 3; i < argc; i++ ) {
      if ( '-' != argv[i][0] )
        usage( argv[0] );
      else if ( 'd' == argv[i][1] )
        g_ignoreDst = true;
      else if ( 'h' == argv[i][1] )
        g_html = true;
      else if ( 't' == argv[i][1] )
        g_tabDelimited = true;
      else if ( 'u' == argv[i][1] ) {
        cd.loc.setTimeZone(0);
        g_ignoreDst = true;
      }
      else
        usage( argv[0] );
    }
  }

  if ( g_tabDelimited && g_html ) {
    fprintf( stderr, "Error: html and tabDelimited cannot both be specified.\n" );
    exit(-1);
  }

#if DEBUG
  if (g_tabDelimited)
    fprintf( stderr, "tab delimited\n");
  if (g_html)
    fprintf( stderr, "HTML output\n");
  if (g_ignoreDst) {
    if ( 0 == cd.loc.timeZone() )
      fprintf( stderr, "use UTC\n");
    else
      fprintf( stderr, "ignore DST\n");
  }
#endif
  if ( 0. == cd.loc.longitude() && 0. == cd.loc.latitude() )
    printf( "Latitude & Longitude are both set to 0. Is this what you intended?\n" );

  const char* pExt = 0;
  if (g_html)
    pExt = "html";
  else if (g_tabDelimited)
    pExt = "txt";

  if ( 0 != pExt ) {
    char filename[32];
    sprintf( filename, "%s%d.%s", monthNames[cd.month-1], cd.year, pExt );
    FILE* fp = fopen( filename, "w" );
    if ( NULL != fp ) {
      g_fp = fp;
      fprintf( stderr, "Writing output to %s\n", filename );
    }
  }

  calcAndPrint( cd );

  if ( stdout != g_fp )
    fclose( g_fp );

  return 0;
}
//----------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -