# Thread: Need to simplify/shorten this code. Help.

1. ## Need to simplify/shorten this code. Help.

Im doing this days between dates prog and I need to shorten this fuction without loosing its purpose. Can someone help.
Code:
```int days (int yer2,int yer1,int mnth2,int mnth1,int day1,int day2)
{
int fyear1,syear1,fyear2,syear2,theyer1,theyer2;
if (mnth1==1 || mnth2==1 || mnth1==2  || mnth2==2)
{
fyear1=int((yer1-1)/100);
syear1=2-fyear1+int(fyear1/4);
fyear2=int((yer2-1)/100);
syear2=2-fyear2+int(fyear2/4);
theyer1=int(365.25*(yer1-1))+int(30.6001*(mnth1+13))+fyear1+day1+1720995;
theyer2=int(365.25*(yer2-1))+int(30.6001*(mnth2+13))+syear2+day2+1720995;

}
else
{
fyear1=int(yer1/100);
fyear1=2-fyear1+(int(fyear1/4));
syear2=int(yer2/100);
syear2=2-syear2+(int(syear2/4));
theyer1=int(365.25*(yer1))+int(30.6001*(mnth1+1))+fyear1+day1+1720995;
theyer2=int(365.25*(yer2))+int(30.6001*(mnth2+1))+syear2+day2+1720995;

}
return abs(theyer1-theyer2);

}```

2. I haven't used the <ctime> header a lot, but with the help of references I came up with something as the following:
Code:
```#include <iostream>
#include <ctime>
#include <cmath>

std::time_t get_date(int year, int month, int day)
{
std::tm a;
a.tm_sec = 0;
a.tm_min = 0;
a.tm_hour = 0;
a.tm_mday = day;
a.tm_mon = month - 1;
a.tm_year = year - 1900;
a.tm_isdst = 0;
return std::mktime(&a);
}

int main()
{
std::cout <<
std::abs((int)std::difftime( get_date(2004, 1, 1), get_date(2005, 1, 1))/(60*60*24))
<< '\n';
}```
Not sure if boost has something easier to use.

3. Very nice code. I just have one thing to say about it: if ints were 16 bits, then that would wrap after a mere 9.10194444 hours: http://www.google.ca/search?hl=en&q=...G=Search&meta=

With 32-bit ints, you're safe for 68.0962597 years. http://www.google.ca/search?hl=en&q=...G=Search&meta=

On the other hand, why risk this at all? Simply use this and away you go.
Code:
`std::abs((int)(std::difftime( get_date(2004, 1, 1), get_date(2005, 1, 1))/(60*60*24)))`
Of course, you're then limited to 2^15-1 days (for 16-bit ints). So perhaps modf() would be best.
Code:
```#include <cmath>
double days, seconds =
std::difftime(
get_date(2004, 1, 1),
get_date(2005, 1, 1)
) / (60*60*24));
std::modf(seconds, &days);```
Sigh . . . I think I have too much spare time on my hands.

4. Is there a way to write this differently, more simplistic? The codes above seem a little complex for me. I don't want to come off like an expert or something but I feel that my code isn't right, that it can be written differently. Either shorten it a little bit or do the calculations differently.

5. Use the approach that anon posted, but write your own function which assigns a "day number" to both dates, which gets called twice in your function.

6. I agree, us anon's example, or some other library function that does date/time calculations.
Code:
`theyer1=int(365.25*(yer1))+int(30.6001*(mnth1+1))+fyear1+day1+1720995;`
Have you tested this for all sorts of arbitrary calculations of short and long date intervals, gapping over leap years and non-leapyears, etc, etc.

By the way, there's a bunch of duplication in your code. The first thing I would do is to convert your common for first and second year calculation into one function:
Code:
```int dayNo(int y, int m, int d)
{
int yoff = 0;
int moff = 1;
int fy, sy;
if (m == 1 || m == 2)
{
yoff = 1;
moff = 13;
}
fy = (y - 1) / 100;
sy = 2 - fy + (fy / 4);
return int(365.25*(y - yoff)+int(30.6001*(m+moff)) + sy + d = 1720995;
}```
I removed a bunch of "int()" since the calculation itself is done completely in integer in the first place, it doesn't need to be "int()" around it - it's already done automagically by the compiler (or rather the processor - but by the compiler choosing integer math operations).

I also used a few local variables to avoid having an almost identical calculation twice. It makes it easier to read the code, and it's easier to change ONE long complicated calculation than to do two different calculations.

Using this function makes it much easier to produce the days funciton:
Code:
```int days (int yer2,int yer1,int mnth2,int mnth1,int day1,int day2)
{
int theyer1 = dayNo(yer1, mnth1, day1);
int theyer2 = dayNo(yer2, mnth2, day2);

return abs(theyer1-theyer2);
}```

I'm also dubious to the code in red below:
Code:
```        fyear1=int((yer1-1)/100);
syear1=2-fyear1+int(fyear1/4);
fyear2=int((yer2-1)/100);
syear2=2-fyear2+int(fyear2/4);
theyer1=int(365.25*(yer1-1))+int(30.6001*(mnth1+13))+fyear1+day1+1720995;
theyer2=int(365.25*(yer2-1))+int(30.6001*(mnth2+13))+syear2+day2+1720995;```
--
Mats