C program days between two dates - c

I have written a program that should find the days between two dates, but it has some hiccups. The logic makes perfect sense in my head when I read through it, so I'm assuming I have some syntax errors that I keep glancing over or something.
Firstly, when entering two dates in different years, the output is always off by about one month (31 in most cases, but 32 in one case...go figure). Second, two dates exactly one month apart will return the number of days in the second month (i.e. 1/1/1 to 2/1/1 yields 28). There are inevitably some other weird things that this program does, but I am hoping that is enough information to help you guys figure out what I'm doing wrong. For the life of me I can't figure this one out on my own. I am relatively new to C, so please be gentle =)
Thanks
// Calculates the number of calendar days between any two dates in history (beginning with 1/1/1).
#include <stdio.h>
#include <stdlib.h>
void leap(int year1, int year2, int *leap1, int *leap2);
void date(int *month1, int *day1, int *year1, int *month2, int *day2, int *year2, int *leap1, int *leap2);
int main(void)
{
int month1, day1, year1, month2, day2, year2, leap1, leap2;
int daysPerMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int daysPerMonthLeap[] = {31,29,31,30,31,30,31,31,30,31,30,31};
leap(year1, year2, &leap1, &leap2);
date(&month1, &day1, &year1, &month2, &day2, &year2, &leap1, &leap2);
if(year1 == year2)
{
int i, total;
if(month1 == month2) // Total days if month1 == month2
{
total = day2 - day1;
printf("There are %d days between the two dates.", total);
}
else
{
if(leap1 == 1)
total = daysPerMonthLeap[month1] - day1;
else
total = daysPerMonth[month1] - day1;
for(i = month1 + 1; i < month2; i++) // Days remaining between dates (excluding last month)
{
if(leap1 == 1)
total += daysPerMonthLeap[i];
else
total += daysPerMonth[i];
}
total += day2; // Final sum of days between dates (including last month)
printf("There are %d days between the two dates.", total);
}
}
else // If year1 != year2 ...
{
int i, total, century1 = ((year1 / 100) + 1) * 100, falseleap = 0;
if(leap1 == 1)
total = daysPerMonthLeap[month1] - day1;
else
total = daysPerMonth[month1] - day1;
for(i = month1 + 1; i <= 12; i++) // Day remaining in first year
{
if(leap1 == 1)
total += daysPerMonthLeap[i];
else
total += daysPerMonth[i];
}
for(i = 1; i < month2; i++) // Days remaining in final year (excluding last month)
{
if(leap2 == 1)
total += daysPerMonthLeap[i];
else
total += daysPerMonth[i];
}
int leapcount1 = year1 / 4; // Leap years prior to and including first year
int leapcount2 = year2 / 4; // Leap years prior to and NOT including final year
if(year2 % 4 == 0)
leapcount2 -= 1;
int leaptotal = leapcount2 - leapcount1; // Leap years between dates
for(i = century1; i < year2; i += 100) // "False" leap years (divisible by 100 but not 400)
{
if((i % 400) != 0)
falseleap += 1;
}
total += 365 * (year2 - year1 - 1) + day2 + leaptotal - falseleap; // Final calculation
printf("There are %d days between the two dates.", total);
}
return 0;
}
void leap(int year1, int year2, int *leap1, int *leap2) // Determines if first and final years are leap years
{
if(year1 % 4 == 0)
{
if(year1 % 100 == 0)
{
if(year1 % 400 == 0)
*leap1 = 1;
else
*leap1 = 0;
}
else
*leap1 = 1;
}
else
*leap1 = 0;
if(year2 % 4 == 0)
{
if(year2 % 100 == 0)
{
if(year2 % 400 == 0)
*leap2 = 1;
else
*leap2 = 0;
}
else
*leap2 = 1;
}
else
*leap2 = 0;
}
void date(int *month1, int *day1, int *year1, int *month2, int *day2, int *year2, int *leap1, int *leap2)
{
for(;;) // Infinite loop (exited upon valid input)
{
int fail = 0;
printf("\nEnter first date: ");
scanf("%d/%d/%d", month1, day1, year1);
if(*month1 < 1 || *month1 > 12)
{
printf("Invalid entry for month.\n");
fail += 1;
}
if(*day1 < 1 || *day1 > 31)
{
printf("Invalid entry for day.\n");
fail += 1;
}
if(*year1 < 1)
{
printf("Invalid entry for year.\n");
fail += 1;
}
if(daysPerMonth[month1] == 30 && *day1 > 30)
{
printf("Invalid month and day combination.\n");
fail += 1;
}
if(*month1 == 2)
{
if(*leap1 == 1 && *day1 > 29)
{
printf("Invalid month and day combination.\n");
fail += 1;
}
else if(*day1 > 28)
{
printf("Invalid month and day combination.\n");
fail += 1;
}
}
if(fail > 0)
continue;
else
break;
}
for(;;)
{
int fail = 0;
printf("\nEnter second date: ");
scanf("%d/%d/%d", month2, day2, year2);
if(*year1 == *year2)
{
if(*month1 > *month2)
{
printf("Invalid entry.\n");
fail += 1;
}
if(*month1 == *month2 && *day1 > *day2)
{
printf("Invalid entry.\n");
fail += 1;
}
}
if(*month2 < 1 || *month2 > 12)
{
printf("Invalid entry for month.\n");
fail += 1;
}
if(*day2 < 1 || *day2 > 31)
{
printf("Invalid entry for day.\n");
fail += 1;
}
if(*year2 < 1)
{
printf("Invalid entry for year.\n");
fail += 1;
}
if(daysPerMonth[month2] == 30 && *day2 > 30)
{
printf("Invalid month and day combination.\n");
fail += 1;
}
if(*month2 == 2)
{
if(*leap2 == 1 && *day2 > 29)
{
printf("Invalid month and day combination.\n");
fail += 1;
}
else if(*day2 > 28)
{
printf("Invalid month and day combination.\n");
fail += 1;
}
}
if(fail > 0)
continue;
else
break;
}
}

First, that leap function feels overly complicated; you don't need to do both dates in one function call, and I'm sure that can be written more succinctly so that it is more obviously correct. Here's a version I've got laying around that isn't succinct but I'm confident it is easy to check the logic:
int is_leap_year(int year) {
if (year % 400 == 0) {
return 1;
} else if (year % 100 == 0) {
return 0;
} else if (year % 4 == 0) {
return 1;
} else {
return 0;
}
}
You could call it like this:
int year1, year2, leap1, leap2;
year1 = get_input();
year2 = get_input();
leap1 = is_leap_year(year1);
leap2 = is_leap_year(year2);
No pointers and significantly less code duplication. Yes, I know that is_leap_year() can be reduced to a single if(...) statement, but this is easy for me to read.
Second, I think you're got a mismatch between 0-indexed arrays and 1-indexed human months:
if(*month1 < 1 || *month1 > 12)
vs
int daysPerMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
Third, I think that days per month can be calculated slightly nicer:
int days_in_month(int month, int year) {
int leap = is_leap_year(year);
/* J F M A M J J A S O N D */
int days[2][12] = {{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
if (month < 0 || month > 11 || year < 1753)
return -1;
return days[leap][month];
}
Here, I assume January is 0; you would need to force the rest of the code to match. (I learned this double-array trick from The Elements of Programming Style (page 54).) The best part of using a routine like this is that it removes the leap condition from the difference calculation.
Fourth, you're indexing arrays outside their bounds:
for(i = month1 + 1; i <= 12; i++)
{
if(leap1 == 1)
total += daysPerMonthLeap[i];
This is just another instance of the problem with 0-indexed arrays and 1-indexed months -- but be sure that you fix this, too, when you fix the months.
I have a fear that I haven't yet found all the issues -- you may find it easier to sort the first and the second date after input and remove all that validation code -- and then use names before and after or something to give names that are easier to think through in the complicated core of the calculation.

This is not a complete answer. I just wanted to mention a better way to calculate leap year (this is taken from The C Programming Language - Page #41)
if ((year % 4 == 0 && year % 100 != 0) || year % 400 ==0)
printf("%d is a leap year \n", year);
else
printf("%d is not a leap year \n", year);

Reduce all month indexes by 1.
What I mean to say is January will correspond to daysPerMonth[0] or daysPerMonthLeap[0] and not daysPerMonth[1] or daysPerMonthLeap[1].
The reason for this being array indexes start from 0.
So, wherever you are using month1, month2 insidedaysPerMonth[] or daysPerMonthLeap[], use month1-1 and month2-1 instead.
I hope this is clear enough.
Otherwise, feel free to comment.

Change
int daysPerMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int daysPerMonthLeap[] = {31,29,31,30,31,30,31,31,30,31,30,31};
to
int daysPerMonth[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int daysPerMonthLeap[] = {0,31,29,31,30,31,30,31,31,30,31,30,31};
i.e. pad the arrays at the beginning since all the code relies on the array values to start at element 1 rather than element 0.
That will get rid of the error you complained of.
The other problem is an off-by-one error when you add day2 to the total. In both cases you should add day2 - 1 rather than day2. This is also due to the date indexes starting at 1 instead of 0.
After I made these changes (plus a couple just to get the code to compile), it works properly.

There are multiple problems in your code snippet.. but I must say it is a very good attempt. There are many short cuts to what you're try to achieve.
I have written the following program which finds the number of days between two given dates. You may use this as a reference.
#include <stdio.h>
#include <stdlib.h>
char *month[13] = {"None", "Jan", "Feb", "Mar",
"Apr", "May", "June", "July",
"Aug", "Sept", "Oct",
"Nov", "Dec"};
/*
daysPerMonth[0] = non leap year
daysPerMonth[1] = leap year
*/
int daysPerMonth[2][13] = {{-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{-1, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
typedef struct _d {
int day; /* 1 to 31 */
int month; /* 1 to 12 */
int year; /* any */
}dt;
void print_dt(dt d)
{
printf("%d %s %d \n", d.day, month[d.month], d.year);
return;
}
int leap(int year)
{
return ((year % 4 == 0 && year % 100 != 0) || year % 400 ==0) ? 1 : 0;
}
int minus(dt d1, dt d2)
{
int d1_l = leap(d1.year), d2_l = leap(d2.year);
int y, m;
int total_days = 0;
for (y = d1.year; y >= d2.year ; y--) {
if (y == d1.year) {
for (m = d1.month ; m >= 1 ; m--) {
if (m == d1.month) total_days += d1.day;
else total_days += daysPerMonth[leap(y)][m];
// printf("%d - %5s - %d - %d \n", y, month[m], daysPerMonth[leap(y)][m], total_days);
}
} else if (y == d2.year) {
for (m = 12 ; m >= d2.month ; m--) {
if (m == d2.month) total_days += daysPerMonth[leap(y)][m] - d2.day;
else total_days += daysPerMonth[leap(y)][m];
// printf("%d - %5s - %d - %d \n", y, month[m], daysPerMonth[leap(y)][m], total_days);
}
} else {
for (m = 12 ; m >= 1 ; m--) {
total_days += daysPerMonth[leap(y)][m];
// printf("%d - %5s - %d - %d \n", y, month[m], daysPerMonth[leap(y)][m], total_days);
}
}
}
return total_days;
}
int main(void)
{
/* 28 Oct 2018 */
dt d2 = {28, 10, 2018};
/* 30 June 2006 */
dt d1 = {30, 6, 2006};
int days;
int d1_pt = 0, d2_pt = 0;
if (d1.year > d2.year) d1_pt += 100;
else d2_pt += 100;
if (d1.month > d2.month) d1_pt += 10;
else d2_pt += 10;
if (d1.day > d2.day) d1_pt += 1;
else d2_pt += 1;
days = (d1_pt > d2_pt) ? minus(d1, d2) : minus(d2, d1);
print_dt(d1);
print_dt(d2);
printf("number of days: %d \n", days);
return 0;
}
The output is as follows:
$ gcc dates.c
$ ./a.out
30 June 2006
28 Oct 2018
number of days: 4503
$
Note: this is not a complete program. It lacks input validation.
Hope it helps!

//Difference/Duration between two dates
//No need to calculate leap year offset or anything
// Author: Vinay Kaple
# include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
int days_add, days_sub, c_date, c_month, b_date, b_month, c_year, b_year;
cout<<"Current Date(dd mm yyyy): ";
cin>>c_date>>c_month>>c_year;
cout<<"Birth Date(dd mm yyyy): ";
cin>>b_date>>b_month>>b_year;
int offset_month[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
days_add = c_date + offset_month[c_month-1];
days_sub = b_date + offset_month[b_month-1];
int total_days = (c_year-b_year)*365.2422 + days_add - days_sub+1;
cout<<"Total days: "<<total_days<<"\n";
int total_seconds = total_days*24*60*60;
cout<<"Total seconds: "<<total_seconds<<"\n";
return 0;
}

Related

Calculate the end date from start date and number of days in C

for the past few hours I've been trying to figure out how to write a programme in C to calculate the end date based on the start date and number of days. (I haven't found the forum for this exact problem, yet).
So let's say you input 27/1/2021 as the starting date and then 380 days. The programme should now calculate and show you the end date 11/2/2022.
I don't know how to move forward, the help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
int day, month, year, numberDays;
int leapYear(int year) {
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}
int monthYear[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main(void) {
printf("Enter starting date: ");
scanf("%d %d %d", &day, &month, &year);
printf("Enter number of days: ");
scanf("%d", &numberDays);
leapYear(year);
int wholeYears, rest;
if (leapYear(year)) {
wholeYears = numberDays / 366;
rest = numberDays % 366;
}
else {
wholeYears = numberDays / 365;
rest = numberDays % 365;
}
int resultYears = year + wholeYears;
int midDays = day + rest;
int resultMonths;
return 0;
}
I can't move any further. I'd need help.
So let's say you input 27/1/2021 as the starting date and then 380 days. The programme should now calculate and show you the end date 11/2/2022.
An easy approach is to use mktime() to bring a date into its standard range.
#include <stdio.h>
#include <time.h>
int main(void) {
struct tm start = {.tm_year = 2021 - 1900, .tm_mon = 1 - 1, .tm_mday = 27,
.tm_hour = 12}; // Midday to avoid DST issues.
start.tm_mday += 380;
printf("%d/%d/%d\n", start.tm_mday, start.tm_mon + 1, start.tm_year + 1900);
time_t t = mktime(&start);
printf("%d/%d/%d %s\n", start.tm_mday, start.tm_mon + 1, start.tm_year + 1900,
t == -1 ? "failed" : "OK");
}
Output
407/1/2021
11/2/2022 OK
Otherwise with discrete code, one is effectively re-writing the year/month/day portion of mktime().
I don't know how to move forward, the help would be appreciated.
Add the 380 to d of y, m, d. Maybe add some value to m in other cases. Then perform basic range reductions. There are 12 months to every year. Month range [1...12]. There are 365*400 + 97 days every 400 years. Day of the month is at least 1.
Last step is to handle a day outside the month range.
A simply, though inefficient, approach: test if the data is more than the days in that month. If so subtract the days in that month and advance to the next month.
I left out some code details as OP's goal is some ideas to move forward.
#define DAYSPER400YEARS (365 * 400 + 97)
#define JANUARY 1
#define FEBRUARY 2
#define DECEMBER 12
#define MONTHPERYEAR 12
static int is_leap_year(long long year) {
// If not divisible by 4 ...
// If not divisible by 100 ...
// If not divisible by 400 ...
}
static int days_per_month(long long year, int month) {
static const signed char dpm[] = { 0, //
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month != FEBRUARY) {
return ....;
}
return .... + is_leap_year(year);
}
/*
* Bring a date into it primary range.
* https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar
* The year, month, day may be any value INT_MIN ... INT_MAX.
* Return error flag.
*/
bool ymd_to_primary_range(int *year, int *month, int *day) {
long long y = *year;
y += *month / 12;
*month %= 12; // month now in -11 to 11 range
while (*month < JANUARY) {
*month += MONTHPERYEAR;
year--;
}
y += (*day / DAYSPER400YEARS) * 400;
*day %= DAYSPER400YEARS;
while (*day < 1) {
*day += DAYSPER400YEARS;
y -= 400;
}
int dpm;
while (*day > (dpm = days_per_month(y, *month))) {
*day -= dpm;
(*month)++;
if (*month > ...) {
*month -= ....;
y++;
}
}
if (y < INT_MIN) {
*year = INT_MIN;
return true;
}
if (y > INT_MAX) {
*year = INT_MAX;
return true;
}
*year = (int) y;
return false;
}
Smaple
int main(void) {
int y = 2021;
int m = 1;
int d = 27;
d += 380;
printf("Error: %d\n", ymd_to_primary_range(&y, &m, &d));
printf("Date (dmy): %2d/%02d/%04d\n", d, m, y);
}
Output
Error: 0
Date (dmy): 11/02/2022

How to check if date is valid using mktime in C?

I have a little problem with my code. I need to check if user's given date is valid or not. I want to check it using 2 rules:
1. Date is valid date and found in a calendar (for example 29.2.2015 is invalid because there's only 28 days in February 2015) 2. Day and month can only be max 2 numbers (for example day 10 and 02 are valid but 010 and 002 are not). All help is appreciated!
This is my code this far:
void dateValidator(const char *date1) {
struct tm date = {0};
int day1;
int month1;
int year1;
int vday;
int vmonth;
int vyear;
sscanf(date1, "%3d.%3d.%d",&day1,&month1,&year1);
/**How do I check that if sscanf reads more than 2 characters on month and day, date is
invalid?**/
date.tm_year = year1 - 1900;
date.tm_mon = month1 - 1;
date.tm_mday = day1;
date.tm_isdst = -1;
vday = date.tm_mday;
vmonth = date.tm_mon;
vyear = date.tm_year;
mktime(&pvm);
if ((vday != date.tm_mday) || (vmonth != date.tm_mon) || (vyear != date.tm_year)) {
/**This doesnt seem to work**/
printf("Invalid date");
}
}
I have deleted my previous answer and posting another one. Actually your code is fine, You just have to send the correct parameter to mktime and to check its return value. I.e. change:
mktime(&pvm);
if ((vday != date.tm_mday) || (vmonth != date.tm_mon) || (vyear != date.tm_year)) {
/**This doesnt seem to work**/
printf("Invalid date");
}
to something like:
r = mktime(&date);
if (r == -1 || (vday != date.tm_mday) || (vmonth != date.tm_mon) || (vyear != date.tm_year)) {
printf("Invalid date");
}
and declare the variable r as time_t r;.
int IsValidDate(int year, int month, int day)
{
unsigned int leap;
unsigned char mon_day[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
/* check the rang of the year */
if ((year < 1) || (year >= 3200))
{
return 0;
}
if ((month < 1) || (month > 12))
{
return 0;
}
/* if it's leep year */
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
{
leap = 1;
}
else
{
leap = 0;
}
mon_day[1] += leap;
if ((day > mon_day[month - 1]) || (day < 1))
{
return 0;
}
return 1;
}
For ruler 1, I have wrote this code for check the date before.
And I hope this code can help you.
The ruler 2 can easily checked, you can check it by format the valid date and compare it with the input string.
How do I check that if sscanf reads more than 2 characters on month and day, date is invalid?
Day and month can only be max 2 numbers digits (for example day 10 and 02 are valid but 010 and 002 are not).
An easy check: Record the offset at the end of the scan and tests 1) that it is not the original 0 (scanning got that far) and 2) this is the end of the string so as to detect extra junk in data1[]. Change 3d to 2d.
int n = 0;
sscanf(date1, "%2d.%2d.%d %n",&day1,&month1,&year1, &n);
if (n > 0 && data1[n] == '\0') Success();
else Fail();
Pedantic code would check with "%[]" to disallow leading spaces and '+'.
int n = 0;
sscanf(date1, "%*2[0-9].%*2[0-9].%*4[0-9] %n", &n);
if (n == 0 || data1[n]) Fail();
else {
sscanf(date1, "%2d.%2d.%d",&day1,&month1,&year1);
...
How to check if date is valid using mktime?
#Marian posted a good solution which adds a test of the return value of mktime().
if (-1 == mktime(&data)) Fail();
if (vday != date.tm_mday) || (vmonth != date.tm_mon) || (vyear != date.tm_year) Fail();

My C program that uses Zeller's algorithm is very buggy on leap years, not sure why?

The program is supposed to return what day of the week it is for the entered date. One of the dates that doesn't work is 01012000. Nothing is returned at all. But on some other leap years the first day of March can be calculated. Also sometimes seemingly random dates don't work. I'm not sure how to fix this. Also I'm supposed to write the part that calculates "daynumber" and then call on it later so I'm not sure if I'm doing that right.
Sorry for the beginner questions, this is my first ever C program.
#include<stdio.h>
#include<math.h>
int main()
{
int day, month, year, lastday, dayname, daynumber, input, d;
//Determine the last day of user specified month
printf("Enter date: ddmmyyyy:\n");
scanf("%d", &input);
day = input/1000000;
month = (input/10000) % 100;
year = input % 10000;
if (month == 1 || month == 3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12)
lastday = 31;
else if (month == 4 || month == 6 || month == 9 || month == 11)
lastday = 30;
else if ((year%4 == 0 && year%100 !=0) || year%400 == 0)
lastday = 29;
else
lastday = 28;
//Verify the date
if (year < 0)
return 1;
if (month < 1 || month > 12)
return 2;
if (day < 1 || day > lastday)
return 3;
//Algorithm
{
int m, d, y, c, daynumber;
if (month > 3)
m = month - 2;
else
m = month + 10;
if (m == 11 || m == 12)
year = year - 1;
else
year = year;
d = day;
y = year % 100;
c = year / 100;
daynumber = (((13*m - 1)/5) + d + y + (y/4) + (c/4) - 2*c) % 7;
if (daynumber == 0)
printf("Sunday\n");
if (daynumber == 1)
printf("Monday\n");
if (daynumber == 2)
printf("Tuesday\n");
if (daynumber == 3)
printf("Wednesday\n");
if (daynumber == 4)
printf("Thursday\n");
if (daynumber == 5)
printf("Friday\n");
if (daynumber == 6)
printf("Saturday\n");
}
}
I can't see your bug, but there is no need to scan the whole number and divide, use:
scanf("%2d%2d%4d", &day, &month, &year);
Using Sakamoto's algorithm you can do the same in few lines:
#include <stdio.h>
static int wday(int d, int m, int y)
{
static int offset[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y / 4 - y / 100 + y / 400 + offset[m - 1] + d) % 7;
}
int main(void)
{
const char *days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int d, m, y;
printf("Enter date [ddmmyyyy]: ");
scanf("%2d%2d%4d", &d, &m, &y);
printf("%s\n", days[wday(d, m, y)]);
return 0;
}
This
(((13*m - 1)/5) + d + y + (y/4) + (c/4) - 2*c)
can probably be negative. The result of % 7 would then still be negative and nothing is printed.
Just add daynumber = (daynumber + 7) % 7; after the line
daynumber = (((13*m - 1)/5) + d + y + (y/4) + (c/4) - 2*c) % 7;
You are using:
printf("Enter date: ddmmyyyy:\n");
scanf("%d", &input);
Here you are storing the input as a int. If sizeof(int) is 2 byte, its range would be between -32,768 to 32,767 and your input would be out of the range. For this you should use long int.

How to calculate the date after x days in C

I'm programming a microcontroller in C that has an internal RTC and automatically increments a day counter (0-65536). So, given the initial date adjusted by the user (DD/MM/YYYY), I need to keep the calendar updated based on that counter. That is, I need to know how to calculate the date after x days. Does anyone know an algorithm for that? Couldn't find anything all over the web.
Thanks in advance.
Daniel
As #moooeeeep suggests in his answer to the same question,
For the sake of clarity and correctness, you should stick to the existing solutions.
#include <stdio.h>
#include <time.h>
int main()
{
/* initialize */
int y=1980, m=2, d=5;
struct tm t = { .tm_year=y-1900, /* The number of years since 1900 */
.tm_mon=m-1, /* month, range 0 to 11 */
.tm_mday=d };
/* modify */
t.tm_mday += 40;
mktime(&t);
/* show result */
printf("%s", asctime(&t)); /* prints: Sun Mar 16 00:00:00 1980 */
return 0;
}
Check this. Maybe it need some adjustments. Somewhat brute-force, but speed should be enough even for microcontroller.
#include <stdio.h>
static int days_in_month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day, month, year;
unsigned short day_counter;
int is_leap(int y) {
return ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0);
}
next_day()
{
day += 1; day_counter++;
if (day > days_in_month[month]) {
day = 1;
month += 1;
if (month > 12) {
month = 1;
year += 1;
if (is_leap(year)) {
days_in_month[2] = 29;
} else {
days_in_month[2] = 28;
}
}
}
}
set_date(int d, int m, int y)
{
m < 1 ? m = 1 : 0;
m > 12 ? m = 12 : 0;
d < 1 ? d = 1 : 0;
d > days_in_month[m] ? d = days_in_month[m] : 0;
if (is_leap(y)){
days_in_month[2] = 29;
} else {
days_in_month[2] = 28;
}
day = d;
month = m;
year = y;
}
skip_days(int x)
{
int i;
for (i=0;i<x;i++) next_day();
}
print_date()
{
printf ("day: %d month: %d year: %d\n", day, month, year);
}
int main(int argc, char **argv)
{
int i;
set_date(5, 2, 1980);
skip_days(10000);
day_counter = 0;
/* after this call next_day each day */
print_date();
return 0;
}

C Program to find day of week given date

Is there a way to find out day of the week given date in just one line of C code?
For example
Given 19-05-2011(dd-mm-yyyy) gives me Thursday
As reported also by Wikipedia, in 1990 Michael Keith and Tom Craver published an expression to minimise the number of keystrokes needed to enter a self-contained function for converting a Gregorian date into a numerical day of the week.
The expression does preserve neither y nor d, and returns a zero-based index representing the day, starting with Sunday, i.e. if the day is Monday the expression returns 1.
A code example which uses the expression follows:
int d = 15 ; //Day 1-31
int m = 5 ; //Month 1-12`
int y = 2013 ; //Year 2013`
int weekday = (d += m < 3 ? y-- : y - 2, 23*m/9 + d + 4 + y/4- y/100 + y/400)%7;
The expression uses the comma operator, as discussed in this answer.
Enjoy! ;-)
A one-liner is unlikely, but the strptime function can be used to parse your date format and the struct tm argument can be queried for its tm_wday member on systems that modify those fields automatically (e.g. some glibc implementations).
int get_weekday(char * str) {
struct tm tm;
memset((void *) &tm, 0, sizeof(tm));
if (strptime(str, "%d-%m-%Y", &tm) != NULL) {
time_t t = mktime(&tm);
if (t >= 0) {
return localtime(&t)->tm_wday; // Sunday=0, Monday=1, etc.
}
}
return -1;
}
Or you could encode these rules to do some arithmetic in a really long single line:
1 Jan 1900 was a Monday.
Thirty days has September, April, June and November; all the rest have thirty-one, saving February alone, which has twenty-eight, rain or shine, and on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
EDIT: note that this solution only works for dates after the UNIX epoch (1970-01-01T00:00:00Z).
Here's a C99 version based on wikipedia's article about Julian Day
#include <stdio.h>
const char *wd(int year, int month, int day) {
/* using C99 compound literals in a single line: notice the splicing */
return ((const char *[]) \
{"Monday", "Tuesday", "Wednesday", \
"Thursday", "Friday", "Saturday", "Sunday"})[ \
( \
day \
+ ((153 * (month + 12 * ((14 - month) / 12) - 3) + 2) / 5) \
+ (365 * (year + 4800 - ((14 - month) / 12))) \
+ ((year + 4800 - ((14 - month) / 12)) / 4) \
- ((year + 4800 - ((14 - month) / 12)) / 100) \
+ ((year + 4800 - ((14 - month) / 12)) / 400) \
- 32045 \
) % 7];
}
int main(void) {
printf("%d-%02d-%02d: %s\n", 2011, 5, 19, wd(2011, 5, 19));
printf("%d-%02d-%02d: %s\n", 2038, 1, 19, wd(2038, 1, 19));
return 0;
}
By removing the splicing and spaces from the return line in the wd() function, it can be compacted to a 286 character single line :)
This is my implementation. It's very short and includes error checking. If you want dates before 01-01-1900, you could easily change the anchor to the starting date of the Gregorian calendar.
#include <stdio.h>
int main(int argv, char** arv) {
int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char* day[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int d, m, y, i;
printf("Fill in a date after 01-01-1900 as dd-mm-yyyy: ");
scanf("%d-%d-%d", &d, &m, &y);
// correction for leap year
if (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0))
month[1] = 29;
if (y < 1900 || m < 1 || m > 12 || d < 1 || d > month[m - 1]) {
printf("This is an invalid date.\n");
return 1;
}
for (i = 1900; i < y; i++)
if (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0))
d += 366;
else
d += 365;
for (i = 0; i < m - 1; i++)
d += month[i];
printf("This is a %s.\n", day[d % 7]);
return 0;
}
The answer I came up with:
const int16_t TM_MON_DAYS_ACCU[12] = {
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
};
int tm_is_leap_year(unsigned year) {
return ((year & 3) == 0) && ((year % 400 == 0) || (year % 100 != 0));
}
// The "Doomsday" the the day of the week of March 0th,
// i.e the last day of February.
// In common years January 3rd has the same day of the week,
// and on leap years it's January 4th.
int tm_doomsday(int year) {
int result;
result = TM_WDAY_TUE;
result += year; // I optimized the calculation a bit:
result += year >>= 2; // result += year / 4
result -= year /= 25; // result += year / 100
result += year >>= 2; // result += year / 400
return result;
}
void tm_get_wyday(int year, int mon, int mday, int *wday, int *yday) {
int is_leap_year = tm_is_leap_year(year);
// How many days passed since Jan 1st?
*yday = TM_MON_DAYS_ACCU[mon] + mday + (mon <= TM_MON_FEB ? 0 : is_leap_year) - 1;
// Which day of the week was Jan 1st of the given year?
int jan1 = tm_doomsday(year) - 2 - is_leap_year;
// Now just add these two values.
*wday = (jan1 + *yday) % 7;
}
with these defines (matching struct tm of time.h):
#define TM_WDAY_SUN 0
#define TM_WDAY_MON 1
#define TM_WDAY_TUE 2
#define TM_WDAY_WED 3
#define TM_WDAY_THU 4
#define TM_WDAY_FRI 5
#define TM_WDAY_SAT 6
#define TM_MON_JAN 0
#define TM_MON_FEB 1
#define TM_MON_MAR 2
#define TM_MON_APR 3
#define TM_MON_MAY 4
#define TM_MON_JUN 5
#define TM_MON_JUL 6
#define TM_MON_AUG 7
#define TM_MON_SEP 8
#define TM_MON_OCT 9
#define TM_MON_NOV 10
#define TM_MON_DEC 11
#include<stdio.h>
#include<math.h>
#include<conio.h>
int fm(int date, int month, int year) {
int fmonth, leap;
if ((year % 100 == 0) && (year % 400 != 0))
leap = 0;
else if (year % 4 == 0)
leap = 1;
else
leap = 0;
fmonth = 3 + (2 - leap) * ((month + 2) / (2 * month))+ (5 * month + month / 9) / 2;
fmonth = fmonth % 7;
return fmonth;
}
int day_of_week(int date, int month, int year) {
int dayOfWeek;
int YY = year % 100;
int century = year / 100;
printf("\nDate: %d/%d/%d \n", date, month, year);
dayOfWeek = 1.25 * YY + fm(date, month, year) + date - 2 * (century % 4);
//remainder on division by 7
dayOfWeek = dayOfWeek % 7;
switch (dayOfWeek) {
case 0:
printf("weekday = Saturday");
break;
case 1:
printf("weekday = Sunday");
break;
case 2:
printf("weekday = Monday");
break;
case 3:
printf("weekday = Tuesday");
break;
case 4:
printf("weekday = Wednesday");
break;
case 5:
printf("weekday = Thursday");
break;
case 6:
printf("weekday = Friday");
break;
default:
printf("Incorrect data");
}
return 0;
}
int main() {
int date, month, year;
printf("\nEnter the year ");
scanf("%d", &year);
printf("\nEnter the month ");
scanf("%d", &month);
printf("\nEnter the date ");
scanf("%d", &date);
day_of_week(date, month, year);
return 0;
}
OUTPUT:
Enter the year 2012
Enter the month 02
Enter the date 29
Date: 29/2/2012
weekday = Wednesday
This is question 20.31 in the C FAQ list. It lists three answers, two of which are amenable to one-liners.
Modified Zeller's congruence: j = y/100; k = y%100; dow = (d + 26 * (m + 1) / 10 + k + k/4 + j/4 + 5*j) % 7;
Tomohiko Sakamoto's method: static int t[] = {0,3,2,5,0,3,5,1,4,6,2,4}; y -= m < 3; dow = (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
There's a further condensation of Sakamoto's method using an ASCII string instead of the t array: dow = (y + y/4 - y/100 + y/400 + "-bed=pen+mad."[m] + d) % 7;
The basic idea underlying all of these methods is the same: boil the date down to a monotonically-increasing sequence of day numbers (taking month lengths and leap years into account), then reduce that day number modulo 7.
I think you can find that in glib:
http://developer.gnome.org/glib/unstable/glib-Date-and-Time-Functions.html#g-date-get-day
Regards
#include<stdio.h>
static char day_tab[2][13] = {
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
int main()
{
int year,month;
scanf("%d%d%d",&year,&month,&day);
printf("%d\n",day_of_year(year,month,day));
return 0;
}
int day_of_year(int year ,int month,int day)
{
int i,leap;
leap = year%4 == 0 && year%100 != 0 || year%400 == 0;
if(month < 1 || month >12)
return -1;
if (day <1 || day > day_tab[leap][month])
return -1;
for(i= 1;i<month ; i++)
{
day += day_tab[leap][year];
}
return day;
}
/*
Program to calculate the day on a given date by User
*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int dd=0,mm=0,i=0,yy=0,odd1=0,todd=0;//variable declaration for inputing the date
int remyr=0,remyr1=0,lyrs=0,oyrs=0,cyr=0,upyr=0,leap=0;//variable declaration for calculation of odd days
int montharr[12]={31,28,31,30,31,30,31,31,30,31,30,31};//array of month days
clrscr();
printf("Enter the date as DD-MM-YY for which you want to know the day\t:");
scanf("%d%d%d",&dd,&mm,&yy); //input the date
/*
check out correct date or not?
*/
if(yy%100==0)
{
if(yy%400==0)
{
//its the leap year
leap=1;
if(dd>29&&mm==2)
{
printf("You have entered wrong date");
getch();
exit(0);
}
}
else if(dd>28&&mm==2)
{
//not the leap year
printf("You have entered wrong date");
getch();
exit(0);
}
}
else if(yy%4==0)
{
//again leap year
leap=1;
if(dd>29&mm==2)
{
printf("You have entered wrong date");
getch();
exit(0);
}
}
else if(dd>28&&mm==2)
{
//not the leap year
printf("You have entered wrong date");
getch();
exit(0);
}
//if the leap year feb month contains 29 days
if(leap==1)
{
montharr[1]=29;
}
//check date,month,year should not be beyond the limits
if((mm>12)||(dd>31)|| (yy>5000))
{
printf("Your date is wrong");
getch();
exit(0);
}
//odd months should not contain more than 31 days
if((dd>31 && (mm == 1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12)))
{
printf("Your date is wrong");
getch();
exit(0);
}
//even months should not contains more than 30 days
if((dd>30 && (mm == 4||mm==6||mm==9||mm==11)))
{
printf("Your date is wrong");
getch();
exit(0);
}
//logic to calculate odd days.....
printf("\nYou have entered date: %d-%d-%d ",dd,mm,yy);
remyr1=yy-1;
remyr=remyr1%400;
cyr=remyr/100;
if(remyr==0)
{
oyrs=0;
}
else if(cyr==0 && remyr>0)
{
oyrs=0;
}
else if(cyr==1)
{
oyrs=5;
}
else if(cyr==2)
{
oyrs=3;
}
else if(cyr==3)
{
oyrs=1;
}
upyr=remyr%100;
lyrs=upyr/4;
odd1=lyrs+upyr;
odd1=odd1%7;
odd1=odd1+oyrs;
for(i=0;i<mm-1;i++)
{
odd1=odd1+montharr[i];
}
todd=odd1+dd;
if(todd>7)
todd=todd%7; //total odd days gives the re quired day....
printf("\n\nThe day on %d-%d-%d :",dd,mm,yy);
if(todd==0)
printf("Sunday");
if(todd==1)
printf("Monday");
if(todd==2)
printf("Tuesday");
if(todd==3)
printf("Wednesday");
if(todd==4)
printf("Thrusday");
if(todd==5)
printf("Friday");
if(todd==6)
printf("Saturday");
getch();
}
For Day of Week, years 2000 - 2099.
uint8_t rtc_DayOfWeek(uint8_t year, uint8_t month, uint8_t day)
{
//static const uint8_t month_offset_table[] = {0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5}; // Typical table.
// Added 1 to Jan, Feb. Subtracted 1 from each instead of adding 6 in calc below.
static const uint8_t month_offset_table[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
// Year is 0 - 99, representing years 2000 - 2099
// Month starts at 0.
// Day starts at 1.
// Subtract 1 in calc for Jan, Feb, only in leap years.
// Subtracting 1 from year has the effect of subtracting 2 in leap years, subtracting 1 otherwise.
// Adding 1 for Jan, Feb in Month Table so calc ends up subtracting 1 for Jan, Feb, only in leap years.
// All of this complication to avoid the check if it is a leap year.
if (month < 2) {
year--;
}
// Century constant is 6. Subtract 1 from Month Table, so difference is 7.
// Sunday (0), Monday (1) ...
return (day + month_offset_table[month] + year + (year >> 2)) % 7;
} /* end rtc_DayOfWeek() */
This one works: I took January 2006 as a reference. (It is a Sunday)
int isLeapYear(int year) {
if(((year%4==0)&&(year%100!=0))||((year%400==0)))
return 1;
else
return 0;
}
int isDateValid(int dd,int mm,int yyyy) {
int isValid=-1;
if(mm<0||mm>12) {
isValid=-1;
}
else {
if((mm==1)||(mm==3)||(mm==5)||(mm==7)||(mm==8)||(mm==10)||(mm==12)) {
if((dd>0)&&(dd<=31))
isValid=1;
} else if((mm==4)||(mm==6)||(mm==9)||(mm==11)) {
if((dd>0)&&(dd<=30))
isValid=1;
} else {
if(isLeapYear(yyyy)){
if((dd>0)&&dd<30)
isValid=1;
} else {
if((dd>0)&&dd<29)
isValid=1;
}
}
}
return isValid;
}
int calculateDayOfWeek(int dd,int mm,int yyyy) {
if(isDateValid(dd,mm,yyyy)==-1) {
return -1;
}
int days=0;
int i;
for(i=yyyy-1;i>=2006;i--) {
days+=(365+isLeapYear(i));
}
printf("days after years is %d\n",days);
for(i=mm-1;i>0;i--) {
if((i==1)||(i==3)||(i==5)||(i==7)||(i==8)||(i==10)) {
days+=31;
}
else if((i==4)||(i==6)||(i==9)||(i==11)) {
days+=30;
} else {
days+= (28+isLeapYear(i));
}
}
printf("days after months is %d\n",days);
days+=dd;
printf("days after days is %d\n",days);
return ((days-1)%7);
}
Here is a simple code that I created in c that should fix your problem :
#include <conio.h>
int main()
{
int y,n,oy,ly,td,a,month,mon_,d,days,down,up; // oy==ordinary year, td=total days, d=date
printf("Enter the year,month,date: ");
scanf("%d%d%d",&y,&month,&d);
n= y-1; //here we subtracted one year because we have to find on a particular day of that year, so we will not count whole year.
oy= n%4;
if(oy==0) // for leap year
{
mon_= month-1;
down= mon_/2; //down means months containing 30 days.
up= mon_-down; // up means months containing 31 days.
if(mon_>=2)
{
days=(up*31)+((down-1)*30)+29+d; // here in down case one month will be of feb so we subtracted 1 and after that seperately
td= (oy*365)+(ly*366)+days; // added 29 days as it is the if block of leap year case.
}
if(mon_==1)
{
days=(up*31)+d;
td= (oy*365)+(ly*366)+days;
}
if(mon_==0)
{
days= d;
td= (oy*365)+(ly*366)+days;
}
}
else
{
mon_= month-1;
down= mon_/2;
up= mon_-down;
if(mon_>=2)
{
days=(up*31)+((down-1)*30)+28+d;
td= (oy*365)+(ly*366)+days;
}
if(mon_==1)
{
days=(up*31)+d;
td= (oy*365)+(ly*366)+days;
}
if(mon_==0)
{
days= d;
td= (oy*365)+(ly*366)+days;
}
}
ly= n/4;
a= td%7;
if(a==0)
printf("\nSunday");
if(a==1)
printf("\nMonday");
if(a==2)
printf("\nTuesday");
if(a==3)
printf("\nWednesday");
if(a==4)
printf("\nThursday");
if(a==5)
printf("\nFriday");
if(a==6)
printf("\nSaturday");
return 0;
}
I wrote a small function to do this in C inspired by Conway's doomsday algorithm.
It was written with 8-bit microcontrollers in mind and the "year" argument is a year from 0 to 99 only (representing 2000 to 2099). I wrote it to compile down nice and small on avr-gcc. It uses no lookup table (I got the code smaller without).
// day of week, Thomas Rutter
// y should be a year 0 to 99 (meaning 2000 to 2099)
// m and d are 1-based (1-12, 1-31)
// returns 0 = sunday, ..., 6 = saturday
uint8_t dayofweek(uint8_t y, uint8_t m, uint8_t d)
{
uint8_t x = y + 24 - (m < 3);
x = x + (x >> 2) + d - m;
if (m & 1)
x += (m & 8) ? 4 : 3;
if (m < 3)
x += 3;
return x % 7;
}
In my own version, the mod operator at the bottom was replaced with my own optimised code as the platform I was using has no MUL or DIV and I wanted it 1 to 7 instead of 0 to 6.
This could be adapted to work with other centuries or specify the year in full - I just didn't need to do so for my own use.
See strftime and %u or %w qualifiers
#include<stdio.h>
int main(void) {
int n,y;
int ly=0;
int mon;
printf("enter the date\n");
scanf("%d",&n);
printf("enter the month in integer\n");
scanf("%d",&mon);
mon=mon-1;
printf("enter year\n");
scanf("%d",&y);
int dayT;
dayT=n%7;
if((y%4==0&&y%100!=0)|(y%4==0&&y%100==0&&y%400==0))
{
ly=y;
printf("the given year is a leap year\n");
}
char a[12]={6,2,2,5,0,3,5,1,4,6,2,4};
if(ly!=0)
{
a[0]=5;
a[1]=1;
}
int m,p;
m=a[mon];
int i,j=0,t=1;
for(i=1600;i<=3000;i++)
{
i=i+99;
if(i<y)
{
if(t==1)
{
p=5;t++;
}
else if(t==2)
{
p=3;
t++;
}
else if(t==3)
{
p=1;
t++;
}
else
{
p=0;
t=1;
}
}}
int q,r,s;
q=y%100;
r=q%7;
s=q/4;
int yTerm;
yTerm=p+r+s;
int w=dayT+m+yTerm;
w=w%7;
if(w==0)
printf("SUNDAY");
else if(w==1)
printf("MONDAY");
else if(w==2)
printf("TUESDAY");
else if(w==3)
printf("WEDNESDAY");
else if(w==4)
printf("THURSDAY");
else if(w==5)
printf("FRIDAY");
else
printf("SATURDAY");
return 0;
}

Resources