PERFORCE change 130569 for review

Peter Wemm peter at FreeBSD.org
Sun Dec 9 15:21:00 PST 2007


http://perforce.freebsd.org/chv.cgi?CH=130569

Change 130569 by peter at peter_overcee on 2007/12/09 23:20:12

	Teach at(1) about "sunrise" and "sunset". eg:
	"at sunrise + 30 minutes" -> 30 min after sunrise
	Also, allow the "+" modifier to be negative.  eg:
	"at sunset + -10 minutes"  --> 10 min before sunset

Affected files ...

.. //depot/projects/hammer/usr.bin/at/Makefile#2 edit
.. //depot/projects/hammer/usr.bin/at/parsetime.c#4 edit
.. //depot/projects/hammer/usr.bin/at/parsetime.h#2 edit
.. //depot/projects/hammer/usr.bin/at/suntime.c#1 add

Differences ...

==== //depot/projects/hammer/usr.bin/at/Makefile#2 (text+ko) ====

@@ -3,7 +3,7 @@
 .include "${.CURDIR}/Makefile.inc"
 
 PROG=	at
-SRCS=	at.c panic.c parsetime.c perm.c
+SRCS=	at.c panic.c parsetime.c perm.c suntime.c
 LINKS=	${BINDIR}/at ${BINDIR}/atq \
 	${BINDIR}/at ${BINDIR}/atrm \
 	${BINDIR}/at ${BINDIR}/batch
@@ -14,6 +14,8 @@
 BINOWN=	root
 BINMODE= 4555
 CLEANFILES+= at.1
+LDADD+=	-lm
+DPADD+= ${LIBM}
 
 at.1: at.man
 	@${ECHO} Making ${.TARGET:T} from ${.ALLSRC:T}; \

==== //depot/projects/hammer/usr.bin/at/parsetime.c#4 (text+ko) ====

@@ -61,7 +61,7 @@
 /* Structures and unions */
 
 enum {	/* symbols */
-    MIDNIGHT, NOON, TEATIME,
+    MIDNIGHT, NOON, TEATIME, SUNRISE, SUNSET,
     PM, AM, TOMORROW, TODAY, NOW,
     MINUTES, HOURS, DAYS, WEEKS, MONTHS, YEARS,
     NUMBER, PLUS, DOT, SLASH, ID, JUNK,
@@ -80,6 +80,8 @@
     { "midnight", MIDNIGHT,0 },	/* 00:00:00 of today or tomorrow */
     { "noon", NOON,0 },		/* 12:00:00 of today or tomorrow */
     { "teatime", TEATIME,0 },	/* 16:00:00 of today or tomorrow */
+    { "sunrise", SUNRISE, 0 },	/* Sunrise */
+    { "sunset", SUNSET, 0 },	/* Sunset */
     { "am", AM,0 },		/* morning times for 0-12 clock */
     { "pm", PM,0 },		/* evening times for 0-12 clock */
     { "tomorrow", TOMORROW,0 },	/* execute 24 hours from time */
@@ -230,7 +232,8 @@
 
 	/* then see what it is
 	 */
-	if (isdigit(sc_token[0])) {
+	if (isdigit(sc_token[0]) || sc_token[0] == '-') {
+	    sc_token[++idx] = *sct++;
 	    while (isdigit(*sct))
 		sc_token[++idx] = *sct++;
 	    sc_token[++idx] = 0;
@@ -564,6 +567,11 @@
     struct tm nowtime, runtime;
     int hr = 0;
     /* this MUST be initialized to zero for midnight/noon/teatime */
+    int mn, r;
+    double lat, lon;
+
+    lat = 37.798325;	/* where I live, so there. */
+    lon = -121.970223;
 
     nowtimer = time(NULL);
     nowtime = *localtime(&nowtimer);
@@ -593,6 +601,26 @@
 	    month(&runtime);
 	    break;
 
+    case SUNRISE:
+	    r = sun_time(1, lat, lon, nowtime.tm_gmtoff / 3600, nowtime.tm_yday + 1, &hr, &mn);
+	    if (r == -1)
+		panic("no sunrise!");
+	    runtime.tm_hour = hr;
+	    runtime.tm_min = mn;
+	    token();
+	    month(&runtime);
+	    break;
+
+    case SUNSET:
+	    r = sun_time(0, lat, lon, nowtime.tm_gmtoff / 3600, nowtime.tm_yday + 1, &hr, &mn);
+	    if (r == -1)
+		panic("no sunrise!");
+	    runtime.tm_hour = hr;
+	    runtime.tm_min = mn;
+	    token();
+	    month(&runtime);
+	    break;
+
 	    /* evil coding for TEATIME|NOON|MIDNIGHT - we've initialised
 	     * hr to zero up above, then fall into this case in such a
 	     * way so we add +12 +4 hours to it for teatime, +12 hours

==== //depot/projects/hammer/usr.bin/at/parsetime.h#2 (text+ko) ====

@@ -24,3 +24,4 @@
  */
 
 time_t parsetime(int argc, char **argv);
+int sun_time(int rise, double lat, double lon, int tz, int yday, int *hr, int *mn);


More information about the p4-projects mailing list