Example #1
0
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);
		}
	}
Example #2
0
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);
}
Example #3
0
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");
    }
}