So yesterday at dinner, Alan mentioned that he wanted a tool to tell him
how many days he has left until his vacation. He was thinking that since
the GNU utilities have options for every possible purpose, GNU "date"
ought to have the ability to do this.
Unfortunately, "man date" reveals that, in fact, this is beyond the
power GNU "date".
For some unaccountable reason, I was unable to forget that conversation,
and despite the fact that Alan said he wrote a script to do the job,
I wasted a few minutes tonight banging out this:
/* daysuntil.c: Calculate the number of days until a target date
To compile: cc -O -s -o daysuntil daysuntil.c -lm
Example: To calculate the number days until Christmas 2008, type
"daysuntil 2008 12 25" at the shell prompt.
*/
#include <stdio.h>
#include <time.h>
#include <math.h>
long julian(int, int, int);
int main(int argc, char **argv)
{
time_t now;
int y, m, d;
long nowjd, thenjd;
struct tm *nowtm;
if(argc!=4) {
fprintf(stderr, "usage: %s <year> <month> <day>\n", argv[0]);
exit(1);
}
y = atoi(argv[1]);
m = atoi(argv[2]);
d = atoi(argv[3]);
time(&now);
nowtm = localtime(&now);
nowjd = julian(nowtm->tm_year+1900, nowtm->tm_mon+1, nowtm->tm_mday);
thenjd = julian(y, m, d);
printf("%ld\n", thenjd-nowjd);
exit(0);
}
long julian(int y, int m, int d) /* days since March 1, 1BC */
{
m -= 3;
if(m<0) {
y -= 1;
m += 12;
}
return 365L*y+y/4-y/100+y/400+(long)floor(30.6001*m+0.5)+d-1;
}
And then, since I hadn't wasted enough time yet, and since the calculation
is almost the same, here's one to figure today's date in the September
That Never Ended:
/* sepdate.c: Today's date in Endless September
To compile: cc -O -s -o sepdate sepdate.c -lm
*/
#include <stdio.h>
#include <time.h>
#include <math.h>
long julian(int, int, int);
int main(void)
{
time_t now;
long nowjd, thenjd;
struct tm *nowtm;
time(&now);
nowtm = localtime(&now);
nowjd = julian(nowtm->tm_year+1900, nowtm->tm_mon+1, nowtm->tm_mday);
thenjd = julian(1993, 8, 31);
printf("Today is September %ld, 1993\n", nowjd-thenjd);
exit(0);
}
long julian(int y, int m, int d) /* days since March 1, 1BC */
{
m -= 3;
if(m<0) {
y -= 1;
m += 12;
}
return 365L*y+y/4-y/100+y/400+(long)floor(30.6001*m+0.5)+d-1;
}
(In case anybody cares, today is September 5337, 1993.)
So...anybody else program anything useless recently?
- Neil Parker
_______________________________________________
EUGLUG mailing list
[email protected]
http://www.euglug.org/mailman/listinfo/euglug