int main(int argc, char *argv[]) { time_t tmpt, now; double days, today, tomorrow; char buf[1024]; if (time(&now) == (time_t)-1) err(1, "time"); if (argc > 1) { tmpt = parsetime(argv[1]); strftime(buf, sizeof(buf), "%a %Y %b %e %H:%M:%S (%Z)", localtime(&tmpt)); printf("%s: ", buf); } else { tmpt = now; } days = (tmpt - EPOCH_MINUS_1970 * 86400) / 86400.0; today = potm(days) + .5; if (tmpt < now) (void)printf("The Moon was "); else if (tmpt == now) (void)printf("The Moon is "); else (void)printf("The Moon will be "); if ((int)today == 100) (void)printf("Full\n"); else if (!(int)today) (void)printf("New\n"); else { tomorrow = potm(days + 1); if ((int)today == 50) (void)printf("%s\n", tomorrow > today ? "at the First Quarter" : "at the Last Quarter"); /* today is 0.5 too big, but it doesn't matter here * since the phase is changing fast enough */ else { today -= 0.5; /* Now it might matter */ (void)printf("%s ", tomorrow > today ? "Waxing" : "Waning"); if (today > 50) (void)printf("Gibbous (%1.0f%% of Full)\n", today); else if (today < 50) (void)printf("Crescent (%1.0f%% of Full)\n", today); } } return EXIT_SUCCESS; }
int main(int argc, char **argv) { time_t tt; struct tm GMT, tmd; double days, today, tomorrow; int ch, cnt, pflag = 0; char *odate = NULL, *otime = NULL; char *progname = argv[0]; while ((ch = getopt(argc, argv, "d:pt:")) != -1) switch (ch) { case 'd': odate = optarg; break; case 'p': pflag = 1; break; case 't': otime = optarg; break; default: usage(progname); } argc -= optind; argv += optind; if (argc) usage(progname); /* Adjust based on users preferences */ time(&tt); if (otime != NULL || odate != NULL) { /* Save today in case -d isn't specified */ localtime_r(&tt, &tmd); if (odate != NULL) { tmd.tm_year = strtol(odate, NULL, 10) - 1900; tmd.tm_mon = strtol(odate + 5, NULL, 10) - 1; tmd.tm_mday = strtol(odate + 8, NULL, 10); /* Use midnight as the middle of the night */ tmd.tm_hour = 0; tmd.tm_min = 0; tmd.tm_sec = 0; } if (otime != NULL) { tmd.tm_hour = strtol(otime, NULL, 10); tmd.tm_min = strtol(otime + 3, NULL, 10); tmd.tm_sec = strtol(otime + 6, NULL, 10); } tt = mktime(&tmd); } gmtime_r(&tt, &GMT); days = (GMT.tm_yday + 1) + ((GMT.tm_hour + (GMT.tm_min / 60.0) + (GMT.tm_sec / 3600.0)) / 24.0); for (cnt = EPOCH; cnt < GMT.tm_year; ++cnt) days += isleap(1900 + cnt) ? 366 : 365; today = potm(days) + .5; if (pflag) { (void)printf("%1.0f\n", today); return (0); } (void)printf("The Moon is "); if ((int)today == 100) (void)printf("Full\n"); else if (!(int)today) (void)printf("New\n"); else { tomorrow = potm(days + 1); if ((int)today == 50) (void)printf("%s\n", tomorrow > today ? "at the First Quarter" : "at the Last Quarter"); else { (void)printf("%s ", tomorrow > today ? "Waxing" : "Waning"); if (today > 50) (void)printf("Gibbous (%1.0f%% of Full)\n", today); else if (today < 50) (void)printf("Crescent (%1.0f%% of Full)\n", today); } } return 0; }