コード例 #1
0
ファイル: event.c プロジェクト: inscriptionweb/pikrellcam
void
sun_times_init(void)
	{
	struct tm	*tm = &pikrellcam.tm_local;
	int		n, year, month, mday;

	if (   sscanf(pikrellcam.latitude, "%lf", &sun.latitude) == 1
	    && sscanf(pikrellcam.longitude, "%lf", &sun.longitude) == 1
	   )
		{
		n = strlen(pikrellcam.latitude);
		if (pikrellcam.latitude[n - 1] == 'S' || pikrellcam.latitude[n - 1] == 's')
			sun.latitude = -sun.latitude;
		n = strlen(pikrellcam.longitude);
		if (pikrellcam.longitude[n - 1] == 'W' || pikrellcam.longitude[n - 1] == 'w')
			sun.longitude = -sun.longitude;

		year  = tm->tm_year + 1900;
		month = tm->tm_mon  + 1;
		mday  = tm->tm_mday + 1;

		sun.sun_valid = sun_rise_set(year, month, mday,
						sun.longitude, sun.latitude,
						&sun.d_sunrise, &sun.d_sunset);
		sun.civil_valid = civil_twilight(year, month, mday,
						sun.longitude, sun.latitude,
						&sun.d_dawn, &sun.d_dusk);
		sun.nautical_valid = nautical_twilight(year, month, mday,
						sun.longitude, sun.latitude,
						&sun.d_nautical_dawn, &sun.d_nautical_dusk);

		sun.d_sunrise = TMOD(sun.d_sunrise + tm->tm_gmtoff / 3600);
		sun.sunrise = sun.d_sunrise * 60;

		sun.d_sunset  = TMOD(sun.d_sunset  + tm->tm_gmtoff / 3600);
		sun.sunset = sun.d_sunset * 60;

		sun.d_dawn = TMOD(sun.d_dawn + tm->tm_gmtoff / 3600);
		sun.dawn = sun.d_dawn * 60;

		sun.d_dusk = TMOD(sun.d_dusk + tm->tm_gmtoff / 3600);
		sun.dusk = sun.d_dusk * 60;

		sun.d_nautical_dawn = TMOD(sun.d_nautical_dawn + tm->tm_gmtoff / 3600);
		sun.nautical_dawn = sun.d_nautical_dawn * 60;

		sun.d_nautical_dusk = TMOD(sun.d_nautical_dusk + tm->tm_gmtoff / 3600);
		sun.nautical_dusk = sun.d_nautical_dusk * 60;

		log_printf_no_timestamp("sunrise/sunset times: %s  dawn/dusk times: %s\n",
			sun.sun_valid ? "invalid" : "valid",
			sun.civil_valid ? "invalid" : "valid");
		log_printf_no_timestamp("  dawn:    %d:%02d\n", sun.dawn / 60, sun.dawn % 60);
		log_printf_no_timestamp("  sunrise: %d:%02d\n", sun.sunrise / 60, sun.sunrise % 60);
		log_printf_no_timestamp("  sunset:  %d:%02d\n", sun.sunset / 60, sun.sunset % 60);
		log_printf_no_timestamp("  dusk:    %d:%02d\n", sun.dusk / 60, sun.dusk % 60);
		}
	}
コード例 #2
0
ファイル: datatypes.cpp プロジェクト: skeskinen/Kastelu
std::tuple<Time,Time,bool> sunriset_today(void)
{
	double lon = 25.183, lat = 61.567;
	std::time_t now = time(0);
	std::tm tm = *localtime(&now);
	int year = 1900 + tm.tm_year, mon = tm.tm_mon + 1, day = tm.tm_mday;
	double rise, set;
	bool rs;
	int zo = zone_utc_plus();

	rs = (bool) sun_rise_set(year, mon, day, lon, lat, &rise, &set);
	Time r, s;
	if(rs == 0) {
		rise += zo; set += zo;
		if(set>24) set -= 24;
		r = Time(rise*3600);
		s = Time(set*3600);
	} else { //yoton yo
		r = Time(0);
		s = Time(0);
	}
	return std::make_tuple(r,s,rs);
}
コード例 #3
0
ファイル: main.c プロジェクト: themitch/suncalc
int main(int argc, char *argv[])
{
  int i, j;
  int year,month,day;
  double lon = -77.06945;
  double lat = 38.794433;
  int coords_set = 0;
  double temp;
  int local = 1;
  int hemisphere;
  int mode = MODE_USAGE;
  int offset_hour = 0; 
  int offset_min = 0;
  int offset_sec = 0;
  int verbose = 0;

  time_t tt;
  struct tm *tm;

  tt = time(NULL);
  ctime(&tt);
  tm = localtime(&tt);

  year = 1900 + tm->tm_year;
  month = 1+ tm->tm_mon;
  day =  1+ tm->tm_mday;
  timezone_name = tm->tm_zone;
  timezone_offset = tm->tm_gmtoff;
  
  for (i=1; i< argc; i++) {
    if (!strcmp("-V", argv[i])) {
      printf("sunwait version 0.1\n");
      exit(0);
    }
    if (!strcmp("-z", argv[i])) {
      local = 0;
    }
    if (!strcmp("-h", argv[i])) {
      print_usage();
      exit(0); 
    }
    if (!strcmp("-v", argv[i])) {
      verbose++;
    }
    if (!strcmp("-p", argv[i])) {
      mode = MODE_PRINT;
    }

    if (!strcmp("-y", argv[i])) {
      i++;
      year = atoi(argv[i]);
    }
    if (!strcmp("-m", argv[i])) {
      i++;
      month = atoi(argv[i]);
    }
    if (!strcmp("-d", argv[i])) {
      i++;
      day = 1 + atoi(argv[i]);
    }

    if (2 == sscanf(argv[i], "%lf%1[Nn]", &temp, &hemisphere)) {
      lat = temp; 
      coords_set |= LAT_SET;
    }
    if (2 == sscanf(argv[i], "%lf%1[Ss]", &temp, &hemisphere)) {
      lat = -temp;
      coords_set |= LAT_SET;
    }
    if (2 == sscanf(argv[i], "%lf%1[Ww]", &temp, &hemisphere)) {
      lon = -temp;
      coords_set |= LON_SET;
    }
    /* this looks different from the others because 77E 
       parses as scientific notation */
    if (1 == sscanf(argv[i], "%lf%", &temp, &hemisphere)
	&& (argv[i][strlen(argv[i])-1] == 'E' ||
	    argv[i][strlen(argv[i])-1] == 'e')) {
      lon = temp;
      coords_set |= LON_SET;
    }

    for (j=0; options[j].label; j++) {
      if (strstr(argv[i], options[j].label)) {
	mode = (mode &~ options[j].mask) | options[j].value;
      }
    }

    if (('+' == argv[i][0] || '-' == argv[i][0]) &&
	('0' <= argv[i][1] && '9' >= argv[i][1])) {
      /* perl would be nice here */
      char* temp;
      temp = 1+argv[i];
      offset_min = strtol(temp, &temp, 10);
      if (':' == *temp) {
	offset_hour = offset_min;
	offset_min = strtol(temp+1, &temp, 10);
      }
      if (':' == *temp) {
	offset_sec = strtol(temp+1, &temp, 10);
      }
      if ('-' == argv[i][0]) {
	offset_hour *= -1;
	offset_min *= -1;
	offset_sec *= -1;
      }
    }
  }

  if (coords_set != (LAT_SET | LON_SET)) {
      fprintf(stderr, "warning: latitude or longitude not set\n\tdefault coords of Alexandria, Virgina, USA used\n");
  }
    if ((mode & MODE_MASK) && (mode & EVENT_MASK)) {
    int sit;
    double up, down, now, interval, offset;
    switch (mode & MODE_MASK) {
    case MODE_USAGE:
      print_usage();
      exit(0);
      break;
    case MODE_SUN:
      sit   = sun_rise_set         ( year, month, day, lon, lat,
				    &up, &down );
      break;
    case MODE_CIVIL:
      sit  = civil_twilight       ( year, month, day, lon, lat,
				    &up, &down );
      break;
    case MODE_NAUT:
      sit = nautical_twilight    ( year, month, day, lon, lat,
				    &up, &down );
      break;
    case MODE_ASTR:
      sit = astronomical_twilight( year, month, day, lon, lat,
				    &up, &down );
      break;
    }
    
    if (0 != sit) {
      fprintf(stderr, "Event does not occur today\n");
      exit(1);
    }
	
    up = TMOD(up+timezone_offset/3600);
    down = TMOD(down+timezone_offset/3600);
    now = tm->tm_hour/1.0 + tm->tm_min/60.0 + tm->tm_sec/3600.0; 
    offset = offset_hour/1.0 + offset_min/60.0 + offset_sec/3600.0; 

    if (EVENT_RISE == (mode & EVENT_MASK)) {
		printf("%2.2d:%2.2d\n", HOURS(up+offset), MINUTES(up+offset));	
    } else if (EVENT_SET == (mode & EVENT_MASK)){
		printf("%2.2d:%2.2d\n", HOURS(down+offset), MINUTES(down+offset));	
    } else { /* mode is EVENT_STATE */
		if(now>up && now<down) { 
			printf("up\n");
		} else {
			printf("down\n");
		}		
	}

    if (0 > interval) {
      if (0 < verbose) {
		fprintf(stderr, "Warning: event already passed for today, waiting till tomorrow.\n");
      }
    }

    if (1 < verbose) {
      fprintf(stderr, "Up %f\n", up);
      fprintf(stderr, "Down %f\n", down);
      fprintf(stderr, "Now %f\n", now);
      fprintf(stderr, "Offset %f\n", offset);
    }

  }
  
  if (MODE_PRINT == (MODE_MASK & mode))
    print_everything(year, month, day, lat, lon, tm, local);


  if (MODE_USAGE == (MODE_MASK & mode))
    print_usage();

  exit(0);
}
コード例 #4
0
ファイル: TimeRange.cpp プロジェクト: DjMomo/calaos_base
void TimeRange::computeSunSetRise(int year, int month, int day,
                                  int &rise_hour, int &rise_min,
                                  int &set_hour, int &set_min)
{
    if (year == cyear && month == cmonth && day == cday &&
        (sunrise_hour_cache != 0 || sunrise_min_cache != 0 ||
         sunset_hour_cache != 0 || sunset_min_cache != 0))
    {
        rise_hour = sunrise_hour_cache;
        rise_min = sunrise_min_cache;
        set_hour = sunset_hour_cache;
        set_min = sunset_min_cache;

        return;
    }

    double longitude;
    double latitude;
    Params opt;

    get_config_options(opt);
    if (!opt.Exists("longitude") || !opt.Exists("latitude"))
    {
        longitude = 2.548828;
        latitude = 46.422713;

        cError() <<  "Horaire: To use sunset/sunrise, you have to set your longitude/latitude in configuration!";
        cError() <<  "Horaire: Please go to the webpage of the server to set these parameters.";
    }
    else
    {
        from_string(get_config_option("longitude"), longitude);
        from_string(get_config_option("latitude"), latitude);
    }

    double rise, set;
    int res;

    cInfo() <<  "Horaire: Computing sunrise/sunset for date " <<
                day << "/" << month << "/" << year;
    res = sun_rise_set(year, month, day, longitude, latitude, &rise, &set);

    if (res != 0)
    {
        rise_hour = 0;
        rise_min = 0;
        set_hour = 0;
        set_min = 0;

        cError() <<  "Horaire: Error in sunset/sunrise calculation!";

        return;
    }

    long tzOffset = getTimezoneOffset();
    rise_min = minutes(rise + minutes((double)tzOffset / 3600.0));
    rise_hour = hours(rise + (double)tzOffset / 3600.0);
    set_min = minutes(set + minutes((double)tzOffset / 3600.0));
    set_hour = hours(set + (double)tzOffset / 3600.0);

    std::stringstream streamrise, streamset;
    streamrise << std::setfill('0') << std::setw(2) << rise_hour << ":" << rise_min;
    streamset << std::setfill('0') << std::setw(2) << set_hour << ":" << set_min;
    cInfo() <<  "Horaire: sunrise is at " << streamrise.str() << " and sunset is at " <<
                streamset.str();

    sunrise_hour_cache = rise_hour;
    sunrise_min_cache = rise_min;
    sunset_hour_cache = set_hour;
    sunset_min_cache = set_min;
    cyear = year;
    cmonth = month;
    cday = day;
}
コード例 #5
0
ファイル: nextwall.c プロジェクト: abelit/nextwall
/**
  Return the local brightness value.

  This function uses sunriset.h to calculate sunrise, sunset, and civil
  twilight times.

  @param[in] lat The latitude of the current location.
  @param[in] lon The longitude of the current location.
  @return Returns 0 for night, 1 for twilight, or 2 for day, depending on
          local time. Returns -1 if brightness could not be determined.
 */
int get_local_brightness(double lat, double lon) {
    struct tm ltime;
    time_t now;
    double htime, sunrise, sunset, civ_start, civ_end;
    int year, month, day, gmt_offset_h, rs, civ;
    char sr_s[6], ss_s[6], civ_start_s[6], civ_end_s[6];

    time(&now);
    localtime_r(&now, &ltime);
    year = ltime.tm_year + 1900;
    month = ltime.tm_mon + 1;
    day = ltime.tm_mday;

    /* GMT offset in hours with local time zone */
    gmt_offset_h = ltime.tm_gmtoff / 3600;

    /* Current time in hours */
    htime = (double)ltime.tm_hour + (double)ltime.tm_min / 60.0;

    /* Set local sunrise, sunset, and civil twilight times */
    rs = sun_rise_set(year, month, day, lon, lat, &sunrise, &sunset);
    civ  = civil_twilight(year, month, day, lon, lat, &civ_start, &civ_end);

    switch (rs) {
        case 0:
            sunrise += gmt_offset_h;
            sunset += gmt_offset_h;
            eprintf("Sun rises %s, sets %s %s\n", hours_to_hm(sunrise, sr_s),
                    hours_to_hm(sunset, ss_s), ltime.tm_zone);
            break;
        case +1:
            eprintf("Sun above horizon\n");
            return 2;
            break;
        case -1:
            eprintf("Sun below horizon\n");
            return 0;
            break;
    }

    switch (civ) {
        case 0:
            civ_start += gmt_offset_h;
            civ_end += gmt_offset_h;
            eprintf("Civil twilight starts %s, ends %s %s\n",
                    hours_to_hm(civ_start, civ_start_s),
                    hours_to_hm(civ_end, civ_end_s), ltime.tm_zone);
            break;
        case +1:
            eprintf("Never darker than civil twilight\n");
            break;
        case -1:
            eprintf("Never as bright as civil twilight\n");
            break;
    }

    if (rs == 0 && civ == 0) {
        if (sunrise < htime && htime < sunset)
            return 2;
        else if (htime < civ_start || htime > civ_end)
            return 0;
        else
            return 1;
    }
    else {
        return -1;
    }
}
コード例 #6
0
ファイル: sunlights.c プロジェクト: Qsdm/sunlights
int main(int argc, char *argv[]) {
    double lon;
    double lat;
    int year,month,day;
    int offset_min,duration;
    int sit;
    char* durationFile;
    double up, down, now, interval, offset;

    time_t tt;
    struct tm *tm;

    /*  5  inputs plus the command is needed to run */
    if(argc!=5) {
        print_usage();
        exit(-1);
    }

    while (1) {
        tt = time(NULL);
        ctime(&tt);
        tm = localtime(&tt);

        /* get inputs passed in via command line */
        lat=strtod(argv[1],NULL);
        lon=strtod(argv[2],NULL);
        offset_min=atoi(argv[3]);
        durationFile=argv[4];


        /*  Setup time */
        year = 1900 + tm->tm_year;
        month = 1+ tm->tm_mon;
        day =  1+ tm->tm_mday;
        timezone_name = tm->tm_zone;
        timezone_offset = tm->tm_gmtoff;

        /*  get sunrise and sunset values  */
        sit = sun_rise_set( year, month, day, lon, lat, &up, &down );
        up = TMOD(up+timezone_offset/3600);
        down = TMOD(down+timezone_offset/3600);
        printf ("==> Sunrise: %5.2f, Sunset: %5.2f GMT\n", up, down);

        printf("get Duration values from file %s \n",durationFile);
        duration=getDurationByMonth(month,durationFile);

        if (0 != sit) {
            fprintf(stderr, "Event does not occur today\n");
            exit(1);
        }
        now = tm->tm_hour/1.0 + tm->tm_min/60.0 + tm->tm_sec/3600.0;
        offset = offset_min/60.0;
        interval = down - now + offset;

        if (0 > interval) {
            printf("==>Sunset has occured today  %5.2f hours ago\n",interval);
            if (interval+(duration/60)>0 ) {
                duration=duration+(interval*60);
                printf("Can wait %i minutes\n",duration);
            }
            else interval=interval+24.0;
        }

        printf ("==> Hours to Wait: %5.2f \n", interval);
        delay((unsigned int)(interval*3600.0*1000));

        /* do */
        turnOnZone(duration,pin);
        printf("==>SUNLIGHTS program complete for today");
    }
}
コード例 #7
0
ファイル: sunriseset.c プロジェクト: Eric-Schnipke/snippets
main()
{
      int year,month,day;
      double lon, lat;
      double daylen, civlen, nautlen, astrlen;
      double rise, set, civ_start, civ_end, naut_start, naut_end,
             astr_start, astr_end;
      double daylen_int,daylen_frac;  /* CWR */
      int    rs, civ, naut, astr;
      int daylen_hours,daylen_min, gmtoff_hr; /* cwr */
      double rise_int,rise_frac,set_int,set_frac; 
      int rise_hours,rise_min,set_hours,set_min;

      time_t the_time; /* only need so can get time zone of local machine... */
      struct tm *mylocal_time;
      char buf[80];
      
/* get time and offset from GMT */ 
      time(&the_time);
      mylocal_time = localtime(&the_time);
      gmtoff_hr = mylocal_time->tm_gmtoff/3600;

      printf("Enter longitude and latitude in decimal degrees, i.e. -97.75 30.23\n");
      printf("Hint: divide minutes by 60 to get decimal portion\n");
      printf( "Longitude (+ is east) and latitude (+ is north) : " );
      fgets(buf, 80, stdin);
      sscanf(buf, "%lf %lf", &lon, &lat );

      for(;;)
      {
            printf( "Input date ( yyyy mm dd ) (ctrl-C exits): " );
            fgets(buf, 80, stdin);
            sscanf(buf, "%d %d %d", &year, &month, &day );

            daylen  = day_length(year,month,day,lon,lat);
            civlen  = day_civil_twilight_length(year,month,day,lon,lat);
            nautlen = day_nautical_twilight_length(year,month,day,lon,lat);
            astrlen = day_astronomical_twilight_length(year,month,day,
                  lon,lat);
                  
            daylen_frac = modf(daylen,&daylen_int); /* daylen into int and fraction */
            daylen_hours = (int)daylen_int;
            daylen_min = (int)(daylen_frac * 60);
            printf( "Day length:                 %5.2f hours", daylen );
            printf( "(%2d hours %2d min)\n", daylen_hours,daylen_min );
            daylen_frac = modf(civlen,&daylen_int); 
            daylen_hours = (int)daylen_int;
            daylen_min = (int)(daylen_frac * 60);
            printf( "With civil twilight         %5.2f hours", civlen );
            printf( "(%2d hours %2d min)\n", daylen_hours,daylen_min );
            daylen_frac = modf(nautlen,&daylen_int); 
            daylen_hours = (int)daylen_int;
            daylen_min = (int)(daylen_frac * 60);
            printf( "With nautical twilight      %5.2f hours", nautlen );
            printf( "(%2d hours %2d min)\n", daylen_hours,daylen_min );
            daylen_frac = modf(astrlen,&daylen_int); 
            daylen_hours = (int)daylen_int;
            daylen_min = (int)(daylen_frac * 60);
            printf( "With astronomical twilight  %5.2f hours", astrlen );
            printf( "(%2d hours %2d min)\n", daylen_hours,daylen_min );
            daylen_frac = modf((civlen-daylen)/2.0,&daylen_int); 
            daylen_hours = (int)daylen_int;
            daylen_min = (int)(daylen_frac * 60);
            printf( "Length of twilight: civil   %5.2f hours",
                  (civlen-daylen)/2.0);
            printf( "(%2d hours %2d min)\n", daylen_hours,daylen_min );
            daylen_frac = modf((nautlen-daylen)/2.0,&daylen_int); 
            daylen_hours = (int)daylen_int;
            daylen_min = (int)(daylen_frac * 60);
            printf( "                  nautical  %5.2f hours",
                  (nautlen-daylen)/2.0);
            printf( "(%2d hours %2d min)\n", daylen_hours,daylen_min );
            daylen_frac = modf((astrlen-daylen)/2.0,&daylen_int); 
            daylen_hours = (int)daylen_int;
            daylen_min = (int)(daylen_frac * 60);
            printf( "              astronomical  %5.2f hours",
                  (astrlen-daylen)/2.0);
            printf( "(%2d hours %2d min)\n", daylen_hours,daylen_min );

            rs   = sun_rise_set         ( year, month, day, lon, lat,
                                          &rise, &set );
            civ  = civil_twilight       ( year, month, day, lon, lat,
                                          &civ_start, &civ_end );
            naut = nautical_twilight    ( year, month, day, lon, lat,
                                          &naut_start, &naut_end );
            astr = astronomical_twilight( year, month, day, lon, lat,
                                          &astr_start, &astr_end );

            daylen_frac = modf(((rise+set)/2.0)+gmtoff_hr ,&daylen_int); 
            daylen_hours = (int)daylen_int;
            daylen_min = (int)(daylen_frac * 60);

            printf( "Sun at south %5.2fh UT,%20d:%02d local\n", (rise+set)/2.0,daylen_hours,daylen_min);

            switch( rs )
            {
                case 0:
                    /* add minutes and local. CWR*/
                    rise_frac = modf(rise+gmtoff_hr ,&rise_int); 
                    rise_hours = (int)rise_int;
                    rise_min = (int)(rise_frac * 60);
                    set_frac = modf(set+gmtoff_hr ,&set_int); 
                    set_hours = (int)set_int;
                    set_min = (int)(set_frac * 60);

                    printf( "Sun rises %5.2fh UT, sets %5.2fh UT;\n    rises %02d:%02d,     sets %02d:%02d local\n",
                             rise, set, rise_hours,rise_min,set_hours,set_min);
                    break;
                case +1:
                    printf( "Sun above horizon\n" );
                    break;
                case -1:
                    printf( "Sun below horizon\n" );
                    break;
            }

            switch( civ )
            {
                case 0:
                    rise_frac = modf(civ_start+gmtoff_hr ,&rise_int); 
                    rise_hours = (int)rise_int;
                    rise_min = (int)(rise_frac * 60);
                    set_frac = modf(civ_end+gmtoff_hr ,&set_int); 
                    set_hours = (int)set_int;
                    set_min = (int)(set_frac * 60);

                    printf( "Civil twilight starts %5.2fh, "
                            "ends %5.2fh UT;\n               starts %02d:%02d,  ends %02d:%02d local\n", 
                            civ_start, civ_end, 
                            rise_hours, rise_min, set_hours, set_min);
                    break;
                case +1:
                    printf( "Never darker than civil twilight\n" );
                    break;
                case -1:
                    printf( "Never as bright as civil twilight\n" );
                    break;
            }

            switch( naut )
            {
                case 0:
                    rise_frac = modf(naut_start+gmtoff_hr ,&rise_int); 
                    rise_hours = (int)rise_int;
                    rise_min = (int)(rise_frac * 60);
                    set_frac = modf(naut_end+gmtoff_hr ,&set_int); 
                    set_hours = (int)set_int;
                    set_min = (int)(set_frac * 60);

                    printf( "Nautical twilight starts %5.2fh, "
                            "ends %5.2fh UT;\n                  starts %02d:%02d,  ends %02d:%02d local\n", 
                            naut_start, naut_end, 
                            rise_hours, rise_min, set_hours, set_min );
                    break;
                case +1:
                    printf( "Never darker than nautical twilight\n" );
                    break;
                case -1:
                    printf( "Never as bright as nautical twilight\n" );
                    break;
            }

            switch( astr )
            {
                case 0:
                    rise_frac = modf(astr_start+gmtoff_hr ,&rise_int); 
                    rise_hours = (int)rise_int;
                    rise_min = (int)(rise_frac * 60);
                    set_frac = modf(astr_end+gmtoff_hr ,&set_int); 
                    set_hours = (int)set_int;
                    set_min = (int)(set_frac * 60);

                    printf( "Astronomical twilight starts %5.2fh, "
                            "ends %5.2fh UT;\n                      starts %02d:%02d,  ends %02d:%02d local\n", 
                            astr_start, astr_end, 
                            rise_hours, rise_min, set_hours, set_min );
                    break;
                case +1:
                    printf( "Never darker than astronomical twilight\n" );
                    break;
                case -1:
                    printf( "Never as bright as astronomical twilight\n" );
                    break;
            }
      return 0;
      }
}
コード例 #8
0
ファイル: sunriset.c プロジェクト: ThomasBaur/skycalendar
main()
{
      int year,month,day;
      double lon, lat;
      double daylen, civlen, nautlen, astrlen;
      double rise, set, civ_start, civ_end, naut_start, naut_end,
             astr_start, astr_end;
      int    rs, civ, naut, astr;
      char buf[80];

      printf( "Longitude (+ is east) and latitude (+ is north) : " );
      fgets(buf, 80, stdin);
      sscanf(buf, "%lf %lf", &lon, &lat );

      for(;;)
      {
            printf( "Input date ( yyyy mm dd ) (ctrl-C exits): " );
            fgets(buf, 80, stdin);
            sscanf(buf, "%d %d %d", &year, &month, &day );

            daylen  = day_length(year,month,day,lon,lat);
            civlen  = day_civil_twilight_length(year,month,day,lon,lat);
            nautlen = day_nautical_twilight_length(year,month,day,lon,lat);
            astrlen = day_astronomical_twilight_length(year,month,day,
                  lon,lat);

            printf( "Day length:                 %5.2f hours\n", daylen );
            printf( "With civil twilight         %5.2f hours\n", civlen );
            printf( "With nautical twilight      %5.2f hours\n", nautlen );
            printf( "With astronomical twilight  %5.2f hours\n", astrlen );
            printf( "Length of twilight: civil   %5.2f hours\n",
                  (civlen-daylen)/2.0);
            printf( "                  nautical  %5.2f hours\n",
                  (nautlen-daylen)/2.0);
            printf( "              astronomical  %5.2f hours\n",
                  (astrlen-daylen)/2.0);

            rs   = sun_rise_set         ( year, month, day, lon, lat,
                                          &rise, &set );
            civ  = civil_twilight       ( year, month, day, lon, lat,
                                          &civ_start, &civ_end );
            naut = nautical_twilight    ( year, month, day, lon, lat,
                                          &naut_start, &naut_end );
            astr = astronomical_twilight( year, month, day, lon, lat,
                                          &astr_start, &astr_end );

            printf( "Sun at south %5.2fh UT\n", (rise+set)/2.0 );

            switch( rs )
            {
                case 0:
                    printf( "Sun rises %5.2fh UT, sets %5.2fh UT\n",
                             rise, set );
                    break;
                case +1:
                    printf( "Sun above horizon\n" );
                    break;
                case -1:
                    printf( "Sun below horizon\n" );
                    break;
            }

            switch( civ )
            {
                case 0:
                    printf( "Civil twilight starts %5.2fh, "
                            "ends %5.2fh UT\n", civ_start, civ_end );
                    break;
                case +1:
                    printf( "Never darker than civil twilight\n" );
                    break;
                case -1:
                    printf( "Never as bright as civil twilight\n" );
                    break;
            }

            switch( naut )
            {
                case 0:
                    printf( "Nautical twilight starts %5.2fh, "
                            "ends %5.2fh UT\n", naut_start, naut_end );
                    break;
                case +1:
                    printf( "Never darker than nautical twilight\n" );
                    break;
                case -1:
                    printf( "Never as bright as nautical twilight\n" );
                    break;
            }

            switch( astr )
            {
                case 0:
                    printf( "Astronomical twilight starts %5.2fh, "
                            "ends %5.2fh UT\n", astr_start, astr_end );
                    break;
                case +1:
                    printf( "Never darker than astronomical twilight\n" );
                    break;
                case -1:
                    printf( "Never as bright as astronomical twilight\n" );
                    break;
            }
      return 0;
      }
}
コード例 #9
0
ファイル: sunrise-test.c プロジェクト: bonzini/earthview
int
main (void)
{
  int year, month, day;
  double lon, lat;
  double len, rise, set, cur_time;
  int rc;
  char ch;
  time_t t;
  struct tm *tm;

  t = time (NULL);
  tm = gmtime (&t);
  lon = 0;

  sun_rise_set (tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
		lon, 0.0, &rise, &set);
  cur_time = tm->tm_hour + tm->tm_min / 60.0;
  lon += (rise - cur_time) * 15.0;
  if (lon > 180.0)
    lon -= 360.0;
  if (lon < -180.0)
    lon += 360.0;

  printf ("==> Sun is rising at longitude %5.2fE, latitude 0.00N\n", lon);

  printf ("Longitude (+ is east) and latitude (+ is north), or * for Lugano: ");
  ch = getchar ();
  if (ch == '*')
    lon = 8.0 + 57.0/60.0, lat = 46.0;
  else
    {
      ungetc (ch, stdin);
      while (scanf ("%lf %lf", &lon, &lat) < 2)
	;
    }

  tm = localtime (&t);
  year = tm->tm_year + 1900;
  month = tm->tm_mon + 1;
  day = tm->tm_mday;
  do
    {
      len = day_length (year, month, day, lon, lat);
      printf ("==> Day length: %5.2f hours\n", len);

      rc = sun_rise_set (year, month, day, lon, lat, &rise, &set);
      if (rc == 1)
	printf ("==> Sun never sets!\n");
      else if (rc == -1)
	printf ("==> Sun never rises!\n");
      else
	{
	  printf ("==> Midday: %5.2f GMT\n", (rise + set) / 2);
	  printf ("==> Sunrise: %5.2f, Sunset: %5.2f GMT\n", rise, set);
	}

      rc = civil_rise_set (year, month, day, lon, lat, &rise, &set);
      if (rc == 1)
	printf ("==> Sun never reaches civil dusk!\n");
      else if (rc == -1)
	printf ("==> Sun never reaches civil dawn!\n");
      else
	printf ("==> Civil sunrise: %5.2f, Sunset: %5.2f GMT\n", rise, set);

      printf ("Date (yyyy mm dd) (ctrl-C exits): ");
      while (scanf ("%d %d %d", &year, &month, &day) < 3 && !feof (stdin))
	;
    }
  while (!feof (stdin));
  return 0;
}