Ejemplo n.º 1
0
int parseDate (const char *in_date, time_t *out_time, bool forceUT, bool *only_date)
{
	int ret;
	struct ln_date l_date;
	ret = parseDate (in_date, &l_date, forceUT, only_date);
	if (ret)
		return ret;
	ln_get_timet_from_julian (ln_get_julian_day (&l_date), out_time);
	return 0;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
	int ret;
	time_t t;
	double jd;
	struct tm tm;
	const struct object *obj;

	/* Default options */
	double horizon = LN_SOLAR_STANDART_HORIZON; /* 50 Bogenminuten; no twilight, normal sunset/rise */
	int tz = INT_MAX;

	char *obj_str = basename(argv[0]);
	char *format = "%H:%M %d.%m.%Y";
	//char *format = "time: %Y-%m-%d %H:%M:%S (%Z) az: §a (§s) alt: §h";
	char tzid[32];
	char *query = NULL;

	bool horizon_set = false;
	bool next = false;
	bool local_tz = false;
	
	time(&t);
	localtime_r(&t, &tm);

	enum {
		MOMENT_NOW,
		MOMENT_RISE,
		MOMENT_SET,
		MOMENT_TRANSIT
	} moment = MOMENT_NOW;

	struct ln_lnlat_posn obs = { DBL_MAX, DBL_MAX };
	struct object_details result;

	/* set tzid as empty (without repointing the buffer) */
	strcpy(tzid, "");
	/* parse command line arguments */
	while (1) {
		int c = getopt_long(argc, argv, "+hvnult:d:f:a:o:q:z:p:m:H:", long_options, NULL);

		/* detect the end of the options. */
		if (c == -1)
			break;

		switch (c) {
			case 'H':
				if      (strcmp(optarg, "civil") == 0)
					horizon = LN_SOLAR_CIVIL_HORIZON;
				else if (strcmp(optarg, "nautic") == 0)
					horizon = LN_SOLAR_NAUTIC_HORIZON;
				else if (strcmp(optarg, "astronomical") == 0)
					horizon = LN_SOLAR_ASTRONOMICAL_HORIZON;
				else {
					char *endptr;
					horizon = strtod(optarg, &endptr);

					if (endptr == optarg)
						usage_error("invalid horizon / twilight parameter");
				}
				
				horizon_set = true;
				break;

			case 't':
				tm.tm_isdst = -1; /* update dst */
				if (strchr(optarg, '_')) {
					if (!strptime(optarg, "%Y-%m-%d_%H:%M:%S", &tm))
						usage_error("invalid time/date parameter");
				}
				else {
					if (!strptime(optarg, "%Y-%m-%d", &tm))
						usage_error("invalid time/date parameter");
				}
				break;

			case 'm':
				if      (strcmp(optarg, "rise") == 0)
					moment = MOMENT_RISE;
				else if (strcmp(optarg, "set") == 0)
					moment = MOMENT_SET;
				else if (strcmp(optarg, "transit") == 0)
					moment = MOMENT_TRANSIT;
				else
					usage_error("invalid moment");
				break;

			case 'n':
				next = true;
				break;

			case 'f':
				format = strdup(optarg);
				break;

			case 'a':
				obs.lat = strtod(optarg, NULL);
				break;

			case 'o':
				obs.lng = strtod(optarg, NULL);
				break;
#ifdef GEONAMES_SUPPORT
			case 'q':
				query = strdup(optarg);
				break;

			case 'l':
				local_tz = true;
				break;
#endif
			case 'p':
				obj_str = optarg;
				break;

			case 'z':
				strncpy(tzid, optarg, sizeof(tzid));
				break;

			case 'u':
				strncpy(tzid, "UTC", sizeof(tzid));
				break;

			case 'v':
				print_version();
				return 0;

			case 'h':
				print_usage();
				return 0;

			case '?':
			default:
				usage_error("unrecognized option");
		}
	}
	
	/* Parse planet/obj */
	obj = object_lookup(obj_str);
	if (!obj)
		usage_error("invalid or missing object, use --object");

#ifdef GEONAMES_SUPPORT
	/* Lookup place at http://geonames.org */
	if (query) {
		ret =  geonames_lookup_latlng(query, &obs, NULL, 0);
		if (ret)
			usage_error("failed to lookup location");
	}
	
	if (local_tz) {
		int gmt_offset;
		ret =  geonames_lookup_tz(obs, &gmt_offset, tzid, sizeof(tzid));
		if (ret)
			usage_error("failed to lookup location");
	}
#endif

	if(strlen(tzid) > 0)	/* set TZ variable only when we have a value - otherwise rely on /etc/localtime or whatever other system fallbacks */
		setenv("TZ", tzid, 1);
	tzset();

	/* Validate observer coordinates */
	if (fabs(obs.lat) > 90)
		usage_error("invalid latitude, use --lat");
	if (fabs(obs.lng) > 180)
		usage_error("invalid longitude, use --lon");
	
	if (horizon_set && strcmp(object_name(obj), "sun"))
		usage_error("the twilight parameter can only be used for the sun");

	/* Calculate julian date */
	t = mktime(&tm);
	jd = ln_get_julian_from_timet(&t);

	result.obs = obs;

#ifdef DEBUG
	printf("Debug: calculate for jd: %f\n", jd);
	printf("Debug: calculate for ts: %ld\n", t);
	printf("Debug: for position: N %f, E %f\n", obs.lat, obs.lng);
	printf("Debug: for object: %s\n", object_name(obj));
	printf("Debug: with horizon: %f\n", horizon);
	printf("Debug: with timezone: %s\n", tzid);
#endif

	/* calc rst date */
rst:	if (object_rst(obj, jd - .5, horizon, &result.obs, &result.rst) == 1)  {
		if (moment != MOMENT_NOW) {
			fprintf(stderr, "object is circumpolar\n");
			return 2;
		}
	}
	else {
		switch (moment) {
			case MOMENT_NOW:	result.jd = jd; break;
			case MOMENT_RISE:	result.jd = result.rst.rise; break;
			case MOMENT_SET:	result.jd = result.rst.set; break;
			case MOMENT_TRANSIT:	result.jd = result.rst.transit; break;
		}

		if (next && result.jd < jd) {
			jd++;
			next = false;
			goto rst;
		}
	}
	
	ln_get_timet_from_julian(result.jd, &t);
	localtime_r(&t, &result.tm);

	object_pos(obj, jd, &result);

	format_result(format, &result);

	return 0;
}