Refactor this code so that I don't have to insert the parameters any time I need to use one of the functions.
I have a class, say julian
, for calculating Julian dates of today, a date I insert and the difference between these two Julian dates.
int julian::calc_julianToday()
{
time_t t = time(0);
struct tm *now = localtime( &t );
todDay = now->tm_mday;
todMonth = now->tm_mon+1;
todYear = now->tm_year+1900;
int at = (14 - todMonth) / 12;
int yt = todYear + 4800 - at;
int mt = todMonth + 12 * at - 3;
if (todYear > 1582 || (todYear == 1582 && todMonth > 10) ||
(todYear == 1582 && todMonth == 10 && todDay >= 15))
return julToday = todDay + (153 * mt + 2) / 5 + 365 * yt
+ yt / 4 - yt / 100 + yt / 400 - 32045;
else
return julToday = todDay + (153 * mt + 2) / 5 + 365 * yt + yt / 4 - 32083;
}
int julian::calc_juliandate(int day, int month, int year)
{
int a = (14 - month) / 12;
int y = year + 4800 - a;
int m = month + 12 * a - 3;
if (year > 1582 || (year == 1582 && month > 10) ||
(year == 1582 && month == 10 && day >= 15))
return julStart = day + (153 * m + 2) / 5
+ 365 * y + y / 4 - y / 100 + y / 400 - 32045;
else
return julStart = day + (153 * m + 2) / 5 + 365 * y + y / 4 - 32083;
}
int julian::dates_diff(int day, int month, int year)
{
int start = calcJulStartDate(day, month, year);
int today = calcJulTodayDate();
differ = today-start;
return differ;
}
Please note that all the variable types are declared in a header file. What I would like to do in main()
is to be able to use calc_juliandate()
without having to call it and pass its attributes each and every time I need it in another function, as in dates_diff()
.
Any suggestions about coding style or implementing this?