Providers
Debian Linux
SDF
MinGW-W64
Verizon
Research
Faqs and RFCs
FileSearching
Google
Google Groups
W3Schools Online Web Tutorials
Wikipedia
Multimedia
CTIS
For the Pigs Among Us
Family Photos
My House
My Photos (1999-2002?)
My Photos (2005)
Retro Computers
Minions of Mirth
Main
Criticals
Criticals (Single Player)
Ghaqua
Maps and Points of Interest
Miscellaneous
Monster Templates
Miscellaneous
Area Codes
Bard’s Tale
Dungeons & Dragons Online
My Blog
Quote of the Day
RogueBasin
Slashdot
The Adventuer’s Guild
Weather
GitHub:eprive/Z80-MBC3
Programming
Main
Across
Biorhythm
BMP Chips
Complex Roots
Magic Square
Magic Square(js)
Matrix(js)
More DOS Programs
Roll
Strip Dups (bash)
Wacky Wood-Worker
How To
Enlightenment 0.16.7.2 Startup Patch
How to Create an OS X Iconset
My RedHat Howto
SSH to VMS (Deathrow OpenVMS Cluster)
Tools
Allowable Mortgage Interest
Dates and Mileage
Day of the Year
Fun With Time
Downloads
Educational
Utilities
datesandmileage.c
#include <ctype.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <locale.h> #include "ncsa.h" #include "query.h" #define MYDEBUG 0 enum ERROR_TYPE {NO_ERROR, INVALID_DATE}; enum ERROR_TYPE error_type = NO_ERROR; char *dow[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; void errorMessage(enum ERROR_TYPE); int isLeapYear(int); int daysBetween(struct tm *, struct tm *); int dayOfWeek(struct tm *); int dayOfYear(struct tm *); char buffer[80]; int main(int argc, char **argv) { struct tm *date1; struct tm *date2; char query_string[2048]; char date1_str[255]; char date2_str[255]; char miles1_str[255]; char miles2_str[255]; char year_str[255]; char bizmiles_str[255]; char rate_str[255]; char html_str[255]; int date1_exists = 0; int date2_exists = 0; int miles1_exists = 0; int miles2_exists = 0; int year_exists = 0; int bizmiles_exists = 0; int rate_exists = 0; int html = 0; int miles1 = 0; int miles2 = 0; int year = 0; int bizmiles = 0; double rate = 0.0; int nod = 0; int nom; int mpy; double mpd; char *q; date1 = malloc(sizeof(struct tm)); date2 = malloc(sizeof(struct tm)); date1->tm_sec = 0; date1->tm_min = 0; date1->tm_hour = 0; date1->tm_mday = 0; date1->tm_mon = 0; date1->tm_year = 0; date1->tm_wday = 0; date1->tm_yday = 0; date1->tm_isdst = 0; date1->tm_zone = "UTC"; date1->tm_gmtoff = 0l; date2->tm_sec = 0; date2->tm_min = 0; date2->tm_hour = 0; date2->tm_mday = 0; date2->tm_mon = 0; date2->tm_year = 0; date2->tm_wday = 0; date2->tm_yday = 0; date2->tm_isdst = 0; date2->tm_zone = "UTC"; date2->tm_gmtoff = 0l; setlocale(LC_ALL, "en_US.UTF-8"); setvbuf( stdout, NULL, _IONBF, 0); /* stdout buffering off */ if (strlen(getenv("QUERY_STRING"))>2047) exit (1); strcpy(query_string, getenv("QUERY_STRING")); unescape_url(query_string); get_var(date1_str, "date1", query_string); get_var(date2_str, "date2", query_string); get_var(miles1_str, "miles1", query_string); get_var(miles2_str, "miles2", query_string); get_var(year_str, "year", query_string); get_var(bizmiles_str, "bizmiles", query_string); get_var(rate_str, "rate", query_string); get_var(html_str, "html", query_string); date1_exists = strlen(date1_str); date2_exists = strlen(date2_str); miles1_exists = strlen(miles1_str); miles2_exists = strlen(miles2_str); year_exists = strlen(year_str); bizmiles_exists = strlen(bizmiles_str); rate_exists = strlen(rate_str); html = html_str[0]=='1'; printf("\n<center><h1>Dates and Mileage</h1></center>"); if (html) printf("\n<hr>"); else printf("\n<hr><div><textarea rows=\"25\" cols=\"80\">"); if (MYDEBUG) { printf("Query_string %s<br>", query_string); if (date1_exists) printf("\nDate1: %s<br>", date1_str); if (date2_exists) printf("\nDate2: %s<br>", date2_str); if (miles1_exists) printf("\nMileage1: %s<br>", miles1_str); if (miles2_exists) printf("\nMileage2: %s<br>", miles2_str); if (year_exists||1) printf("\nYear: %s<br>", year_str); if (bizmiles_exists) printf("\nBusiness mileage: %s<br>", bizmiles_str); if (rate_exists) printf("\nDollars/mile: %s<br>", rate_str); printf("\nhtml: %s numeric: %d<br>", html_str, html); } if (date1_exists) { date1_exists=0; if (((q=strptime(date1_str, "%m/%d/%y", date1))!=NULL) && (*q=='\0')) date1_exists=1; else if (strptime(date1_str, "%m/%d/%Y", date1)!=NULL) date1_exists=1; if (date1_exists) { printf( "\n%d/%d/%d was day %d of the year and fell on a %s.", 1+date1->tm_mon, date1->tm_mday, 1900+date1->tm_year, dayOfYear(date1), dow[dayOfWeek(date1)] ); printf(html?"<br>\n":"\n"); printf(html?"<br>\n":"\n"); } } if (!date1_exists) date2_exists=0; if (date2_exists) { date2_exists=0; if (((q=strptime(date2_str, "%m/%d/%y", date2))!=NULL) && (*q=='\0')) date2_exists=1; else if (strptime(date2_str, "%m/%d/%Y", date2)!=NULL) date2_exists=1; if (date2_exists) { printf( "\n%d/%d/%d was day %d of the year and fell on a %s.", 1+date2->tm_mon, date2->tm_mday, 1900+date2->tm_year, dayOfYear(date2), dow[dayOfWeek(date2)] ); printf(html?"<br>\n":"\n"); printf(html?"<br>\n":"\n"); } } if (date2_exists) { nod = daysBetween(date1, date2); } if (year_exists) year = atoi(year_str); if (miles1_exists && miles2_exists && date2_exists && year_exists && (nod>0)) { miles1 = atoi(miles1_str); miles2 = atoi(miles2_str); nom = miles2-miles1; nom = (nom>1)?nom:nom*-1; mpd = (double)nom/nod; mpy = (int)(mpd * (365+isLeapYear(year)) + 0.5); printf( "\nTaxpayer provided documentation showing mileage on the " "following dates:" ); printf(html?"<br>\n":"\n"); printf( "%02d/%02d/%04d %'d miles;", 1+date1->tm_mon, date1->tm_mday, 1900+date1->tm_year, miles1 ); printf(html?"<br>\n":"\n"); printf( "%02d/%02d/%04d %'d miles;", 1+date2->tm_mon, date2->tm_mday, 1900+date2->tm_year, miles2 ); printf(html?"<br>\n":"\n"); printf(html?"<br>\n":"\n"); printf( "\nTaxpayer went %'d miles in %d days for an average of %'.1lf" " miles/day. Extrapolating gives us %'d miles in %s.", nom, nod, mpd, mpy, year_str ); printf(html?"<br>\n":"\n"); printf(html?"<br>\n":"\n"); } else if (date2_exists) { printf( "\nThere were %d days between %d/%d/%d and %d/%d/%d.", nod, 1+date1->tm_mon, date1->tm_mday, 1900+date1->tm_year, 1+date2->tm_mon, date2->tm_mday, 1900+date2->tm_year ); printf(html?"<br>\n":"\n"); printf(html?"<br>\n":"\n"); } else if (year_exists) { printf( "\n%s %s a leap year.", year_str, isLeapYear(year)?"was":"was not" ); printf(html?"<br>\n":"\n"); printf(html?"<br>\n":"\n"); } if (bizmiles_exists && rate_exists) { bizmiles = atoi(bizmiles_str); rate = atof(rate_str); printf( "\nTaxpayer went %'d business miles at $%5.3lf/mile for " "a maximum expense of about $%'.2lf.", bizmiles, rate, bizmiles * rate); printf(html?"<br>\n":"\n"); printf(html?"<br>\n":"\n"); } if (!html) printf("</textarea></div>\n"); if (error_type) errorMessage(error_type); printf( "\n<hr>" "\n" ); return 0; } /* main */ void errorMessage(enum ERROR_TYPE error_no) { switch (error_no) { case INVALID_DATE: printf("<h2>\n"); printf("<br>\n"); printf("You must provide Date 1 in m/d/yy format.<br>\n"); printf("</h2>\n"); break; }; } /* errorMessage */ int isLeapYear(int year) { if ((year % 400) == 0) return 1; if ((year % 100) == 0) return 0; if ((year % 4) == 0) return 1; return 0; } /* isLeapYear */ int daysBetween(struct tm *d1, struct tm *d2) { time_t jd1; time_t jd2; int diff; jd1 = mktime(d1); jd2 = mktime(d2); diff = (int)(floor(difftime(jd1, jd2)+0.5)/(24*60*60)); diff = (diff>0)?diff:diff*-1; return diff; } /* daysBetween */ int dayOfWeek(struct tm *dt) { mktime(dt); return (int)(dt->tm_wday); } /* dayOfWeek */ int dayOfYear(struct tm *dt) { mktime(dt); return (int)(dt->tm_yday)+1; } /* dayOfYear */
This source has been viewed 21 times.
Friday, 6 December 2024
Michael J. Chappell
Contact me at:
mcsuper5@freeshell.org