patterncppMinor
Calculating Julian dates
Viewed 0 times
calculatingjuliandates
Problem
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
Please note that all the variable types are declared in a header file. What I would like to do in
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 Solution
Welcome to Code Review,
I do not know how to solve your problem, however, your code could use polishing.
I do not write C++ but can you not write
I do not know how to solve your problem, however, your code could use polishing.
- Magical constants; your code is littered with them, use constants instead
- Undocumented magical constants, I can guess what
365or12is used for, other constants like32083could use a comment or could show the calculation of how you got there
- Naming;
todDay,todMonthandtodYearare terrible names
- Naming;
at,tyandmtare even worse
- Casing; be consistent, you mix underscores with casing and even your casing is not consistent (
calc_julianToday<>calc_juliandate)
- Indenting! Your indenting is all over the place and distracts the reader
- Repeating / DRY;
I do not write C++ but can you not write
calc_julianToday() asint julian::calc_julianToday()
{
time_t t = time(0);
struct tm *now = localtime( &t );
return julToday = calc_juliandate( now->tm_mday, now->tm_mon+1, now->tm_year+1900 ) ;
}Code Snippets
int julian::calc_julianToday()
{
time_t t = time(0);
struct tm *now = localtime( &t );
return julToday = calc_juliandate( now->tm_mday, now->tm_mon+1, now->tm_year+1900 ) ;
}Context
StackExchange Code Review Q#38893, answer score: 6
Revisions (0)
No revisions yet.