use system timezone as default
This commit is contained in:
parent
b56e108178
commit
7ee984465a
1 changed files with 43 additions and 22 deletions
|
@ -1,4 +1,4 @@
|
||||||
/**
|
/**
|
||||||
* Main routine
|
* Main routine
|
||||||
*
|
*
|
||||||
* Does parsing of command line options and start calculation
|
* Does parsing of command line options and start calculation
|
||||||
|
@ -32,7 +32,9 @@
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
#include "../config.h"
|
||||||
#include "sun.h"
|
#include "sun.h"
|
||||||
|
|
||||||
static struct option long_options[] = {
|
static struct option long_options[] = {
|
||||||
|
@ -46,6 +48,7 @@ static struct option long_options[] = {
|
||||||
#endif
|
#endif
|
||||||
{"zone", required_argument, 0, 'z'},
|
{"zone", required_argument, 0, 'z'},
|
||||||
{"help", no_argument, 0, 'h'},
|
{"help", no_argument, 0, 'h'},
|
||||||
|
{"version", no_argument, 0, 'v'},
|
||||||
{0}
|
{0}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -59,9 +62,14 @@ static char *long_options_descs[] = {
|
||||||
"query geonames.org for geographical position",
|
"query geonames.org for geographical position",
|
||||||
#endif
|
#endif
|
||||||
"use timezone for output",
|
"use timezone for output",
|
||||||
"show this help"
|
"show this help",
|
||||||
|
"show version"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void version () {
|
||||||
|
printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
|
||||||
|
}
|
||||||
|
|
||||||
void usage() {
|
void usage() {
|
||||||
printf("usage: sun mode [options]\n\n");
|
printf("usage: sun mode [options]\n\n");
|
||||||
printf(" mode is one of: rise, set, noon, daytime, nighttime\n\n");
|
printf(" mode is one of: rise, set, noon, daytime, nighttime\n\n");
|
||||||
|
@ -76,7 +84,7 @@ void usage() {
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\nA combination of --lat, --lon or --query is required!\n");
|
printf("\nA combination of --lat, --lon or --query is required!\n");
|
||||||
printf("For bugs or feature requests please contact Steffen Vogel <stv0g@0l.de>\n");
|
printf("Please report bugs to: %s\n", PACKAGE_BUGREPORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
char * strreplace(char *subject, char *search, char *replace) {
|
char * strreplace(char *subject, char *search, char *replace) {
|
||||||
|
@ -109,39 +117,30 @@ char * strreplace(char *subject, char *search, char *replace) {
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
/* default options */
|
/* default options */
|
||||||
double twilight = -50.0 / 60; /* 50 Bogenminuten; no twilight, normal sunset/rise */
|
double twilight = -50.0 / 60; /* 50 Bogenminuten; no twilight, normal sunset/rise */
|
||||||
int timezone = 0; /* UTC */
|
|
||||||
char *format = "%H:%M";
|
char *format = "%H:%M";
|
||||||
char *query = NULL;
|
char *query = NULL;
|
||||||
bool error = false;
|
bool error = false;
|
||||||
|
int timezone = 0;
|
||||||
|
|
||||||
enum mode mode;
|
enum mode mode;
|
||||||
struct coords pos = { INFINITY, INFINITY };
|
|
||||||
struct tm date;
|
struct tm date;
|
||||||
|
struct coords pos = { INFINITY, INFINITY };
|
||||||
|
|
||||||
/* default date: now */
|
/* default date: now */
|
||||||
time_t t;
|
time_t t;
|
||||||
time(&t);
|
time(&t);
|
||||||
localtime_r(&t, &date);
|
localtime_r(&t, &date);
|
||||||
|
|
||||||
/* parse command */
|
/* default timezone: system */
|
||||||
if (argc <= 1) {
|
struct timezone tz;
|
||||||
fprintf(stderr, "mode required\n\n");
|
if (gettimeofday(NULL, &tz) == 0) {
|
||||||
error = true;
|
timezone = -tz.tz_minuteswest / 60.0;
|
||||||
}
|
|
||||||
else if (strcmp(argv[1], "rise") == 0) mode = RISE;
|
|
||||||
else if (strcmp(argv[1], "set") == 0) mode = SET;
|
|
||||||
else if (strcmp(argv[1], "noon") == 0) mode = NOON;
|
|
||||||
else if (strcmp(argv[1], "daytime") == 0) mode = DAYTIME;
|
|
||||||
else if (strcmp(argv[1], "nighttime") == 0) mode = NIGHTTIME;
|
|
||||||
else {
|
|
||||||
fprintf(stderr, "invalid mode: %s\n", argv[1]);
|
|
||||||
error = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* parse command line arguments */
|
/* parse command line arguments */
|
||||||
while (1) {
|
while (1) {
|
||||||
int optidx;
|
int optidx;
|
||||||
int c = getopt_long(argc-1, argv+1, "ht:d:f:a:o:q:z:", long_options, &optidx);
|
int c = getopt_long(argc-1, argv+1, "hvt:d:f:a:o:q:z:", long_options, &optidx);
|
||||||
|
|
||||||
/* detect the end of the options. */
|
/* detect the end of the options. */
|
||||||
if (c == -1) break;
|
if (c == -1) break;
|
||||||
|
@ -191,6 +190,10 @@ int main(int argc, char *argv[]) {
|
||||||
timezone = atoi(optarg);
|
timezone = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'v':
|
||||||
|
version();
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
|
||||||
case 'h':
|
case 'h':
|
||||||
usage();
|
usage();
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
@ -201,15 +204,32 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* parse command */
|
||||||
|
if (argc < 2) {
|
||||||
|
fprintf(stderr, "mode is missing\n");
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
|
else if (strcmp(argv[1], "rise") == 0) mode = RISE;
|
||||||
|
else if (strcmp(argv[1], "set") == 0) mode = SET;
|
||||||
|
else if (strcmp(argv[1], "noon") == 0) mode = NOON;
|
||||||
|
else if (strcmp(argv[1], "daytime") == 0) mode = DAYTIME;
|
||||||
|
else if (strcmp(argv[1], "nighttime") == 0) mode = NIGHTTIME;
|
||||||
|
else {
|
||||||
|
fprintf(stderr, "invalid mode: %s\n", argv[1]);
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef GEONAMES_SUPPORT
|
#ifdef GEONAMES_SUPPORT
|
||||||
|
/* lookup place at http://geonames.org */
|
||||||
if (query && geonames_lookup(query, &pos, NULL, 0) != 0) {
|
if (query && geonames_lookup(query, &pos, NULL, 0) != 0) {
|
||||||
fprintf(stderr, "failed to lookup location: %s\n", query);
|
fprintf(stderr, "failed to lookup location: %s\n", query);
|
||||||
error = true;
|
error = true;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* validate coordinates */
|
||||||
if (pos.lat == INFINITY) {
|
if (pos.lat == INFINITY) {
|
||||||
fprintf(stderr, "please provide a complete position: latitude is missing\n");
|
fprintf(stderr, "latitude is missing\n");
|
||||||
error = true;
|
error = true;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -223,7 +243,7 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pos.lon == INFINITY) {
|
if (pos.lon == INFINITY) {
|
||||||
fprintf(stderr, "please provide a complete position: longitude is missing\n");
|
fprintf(stderr, "longitude is missing\n");
|
||||||
error = true;
|
error = true;
|
||||||
}
|
}
|
||||||
else if (fabs(pos.lon) > 180) {
|
else if (fabs(pos.lon) > 180) {
|
||||||
|
@ -231,6 +251,7 @@ int main(int argc, char *argv[]) {
|
||||||
error = true;
|
error = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* abort on errors */
|
||||||
if (error) {
|
if (error) {
|
||||||
printf("\n");
|
printf("\n");
|
||||||
usage();
|
usage();
|
||||||
|
@ -240,7 +261,7 @@ int main(int argc, char *argv[]) {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
char date_str[64];
|
char date_str[64];
|
||||||
strftime(date_str, 64, "%Y-%m-%d", &date);
|
strftime(date_str, 64, "%Y-%m-%d", &date);
|
||||||
printf("calculate for %s\n", date_str);
|
printf("calculate for: %s\n", date_str);
|
||||||
printf("for position: %f, %f\n", pos.lat, pos.lon);
|
printf("for position: %f, %f\n", pos.lat, pos.lon);
|
||||||
printf("with twilight: %f\n", twilight);
|
printf("with twilight: %f\n", twilight);
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Reference in a new issue