Skip to content
Snippets Groups Projects
Commit 90ff2903 authored by Rene Redler's avatar Rene Redler
Browse files

updates to libmtime 1.0.8

parent 6134c295
No related branches found
No related tags found
No related merge requests found
......@@ -71,8 +71,13 @@ int
getNoOfDaysInYearDateTime(struct _datetime* dt);
/*! \cond PRIVATE */
bool
testYearIsLeapYear(int64_t year);
static inline bool
testYearIsLeapYear(int64_t year)
{
bool isLeapYear = !(year % 400) || ((year % 100) && !(year % 4));
return isLeapYear;
}
struct _datetime *
convertDateToDateTime(struct _date* d, struct _datetime* dt_return);
......
......@@ -336,26 +336,6 @@ else
}
/*! \cond PRIVATE */
/* Internal function. Test is year a leap year? */
bool
testYearIsLeapYear(int64_t year)
{
bool flag = false;
if (!(year % 400))
flag = true;
else if (!(year % 100))
flag = false;
else if (!(year % 4))
flag = true;
else
flag = false;
return flag;
}
/*
* NOTE: Internal and not doxyfied.
*
......
......@@ -868,21 +868,21 @@ julianDeltaToTimeDelta(struct _juliandelta* jd, struct _datetime* base_dt, struc
/* Set parameter according to calender. */
switch (getCalendarType())
{
case YEAR_OF_365_DAYS:
case YEAR_OF_365_DAYS:
msdinm = monthSpecificDeltaInMonths365;
ndiny = NO_OF_DAYS_IN_A_YEAR_FOR_CAL_TYPE365;
break;
case YEAR_OF_360_DAYS:
case YEAR_OF_360_DAYS:
msdinm = monthSpecificDeltaInMonths360;
ndiny = NO_OF_DAYS_IN_A_YEAR_FOR_CAL_TYPE360;
break;
case PROLEPTIC_GREGORIAN:
case PROLEPTIC_GREGORIAN:
/* Handle all Gregorian related code here. */
/* Gregorian will have 366 days and 365 days depending on Leap year. */
if ( jd->sign == '-' )
......@@ -895,42 +895,42 @@ julianDeltaToTimeDelta(struct _juliandelta* jd, struct _datetime* base_dt, struc
/* No of days in the final year */
int64_t delta_final_year;
int64_t days = (-1)*jd->day;
/* Set counter to base year and then jump forward to get to the final year.
For each loop forward, increment year by 1.
*/
/* Set counter to base year and then iterate backward to get to the final year.
For each loop forward, increment year by 1.
*/
int64_t j = base_dt->date.year;
/* Initialize to 0. */
td_return->year = 0;
/* Fast-Fwd >= 400 */
if (days >= NO_OF_DAYS_IN_400_YEARS)
{
int64_t numberOf400YearPeriods = days / NO_OF_DAYS_IN_400_YEARS;
td_return->year = td_return->year + numberOf400YearPeriods * 400;
j = j + numberOf400YearPeriods * 400;
days = days - numberOf400YearPeriods * NO_OF_DAYS_IN_400_YEARS;
}
{
int64_t numberOf400YearPeriods = days / NO_OF_DAYS_IN_400_YEARS;
td_return->year = td_return->year + numberOf400YearPeriods * 400;
j = j - numberOf400YearPeriods * 400;
days = days - numberOf400YearPeriods * NO_OF_DAYS_IN_400_YEARS;
}
do
{
/* Loop over and get to the final year by substracting 366/365 days depending
on leap/non-leap year. For each substraction, increment year by 1.
*/
on leap/non-leap year. For each substraction, increment year by 1.
*/
/* The crucial point is month of february. */
delta_final_year = days;
if (
( (testYearIsLeapYear(j + 1)) && (base_dt->date.month >= 3) )
||
( (testYearIsLeapYear(j)) && (base_dt->date.month < 3) )
)
( (testYearIsLeapYear(j)) && (base_dt->date.month >= 3) )
||
( (testYearIsLeapYear(j-1)) && (base_dt->date.month < 3) )
)
{
/* If next year is leap year and base month is >= 3
OR
this year is a leap year and month is < 3
=> delta of 1 year corresponds to 366 day julian delta.
*/
/* If year is leap year and base month is >= 3
OR
next year is a leap year and month is < 3
=> delta of 1 year corresponds to 366 day julian delta.
*/
days = days - NO_OF_DAYS_IN_A_LEAP_YEAR;
}
else
......@@ -939,27 +939,30 @@ julianDeltaToTimeDelta(struct _juliandelta* jd, struct _datetime* base_dt, struc
days = days - NO_OF_DAYS_IN_A_YEAR_FOR_CAL_TYPE365;
}
td_return->year++;
j++;
}while (days >= 0);
j--;
} while (days >= 0);
/* The loop ran one time too much. */
if (days < 0)
{
td_return->year--;
j--;
j++;
}
/* In final year, the crucial point is the month of february. */
if (
((testYearIsLeapYear(j + 1)) && (base_dt->date.month >= 3))
||
((testYearIsLeapYear(j)) && (base_dt->date.month < 3))
)
((testYearIsLeapYear(j)) && (base_dt->date.month >= 3))
||
((testYearIsLeapYear(j-1)) && (base_dt->date.month < 3))
)
{
/* If final year's next year is a leap year and base month is >= 3
OR
final year is a leap year and month is < 3
=> An addition of leap-year specific delta for each month.
*/
/* If final year is a leap year and base month is >= 3
OR
year preceding final year is a leap year and month is < 3
=> An addition of leap-year specific delta for each month.
*/
msdinm = monthSpecificDeltaInMonthsLeapyear;
ndiny = NO_OF_DAYS_IN_A_LEAP_YEAR;
}
......@@ -996,39 +999,39 @@ julianDeltaToTimeDelta(struct _juliandelta* jd, struct _datetime* base_dt, struc
int64_t delta_final_year;
int64_t days = jd->day;
/* Set counter to base year and then loop back to get to the final year.
For each loop back, increment year by 1.
For each loop back, increment year by 1.
*/
int64_t j = base_dt->date.year;
/* Initialize. */
td_return->year = 0;
/* Fast-Fwd >= 400 */
if(days >= NO_OF_DAYS_IN_400_YEARS)
{
int64_t numberOf400YearPeriods = days / NO_OF_DAYS_IN_400_YEARS;
td_return->year = td_return->year + numberOf400YearPeriods * 400;
j = j - numberOf400YearPeriods * 400;
days = days - numberOf400YearPeriods * NO_OF_DAYS_IN_400_YEARS;
}
if (days >= NO_OF_DAYS_IN_400_YEARS)
{
int64_t numberOf400YearPeriods = days / NO_OF_DAYS_IN_400_YEARS;
td_return->year = td_return->year + numberOf400YearPeriods * 400;
j = j - numberOf400YearPeriods * 400;
days = days - numberOf400YearPeriods * NO_OF_DAYS_IN_400_YEARS;
}
do
{
/* Loop over and get the year by substracting 366/365 days depending
on leap/non-leap year. For each substraction, increment year by 1.
on leap/non-leap year. For each substraction, increment year by 1.
*/
/* The crucial point is month of february. */
delta_final_year = days;
if (
((testYearIsLeapYear(j - 1)) && (base_dt->date.month < 3))
||
((testYearIsLeapYear(j)) && (base_dt->date.month >= 3))
)
((testYearIsLeapYear(j+1)) && (base_dt->date.month >= 3))
||
((testYearIsLeapYear(j)) && (base_dt->date.month < 3))
)
{
/* If previous year is leap year and base month is < 3
OR
this year is a leap year and month is >= 3
=> delta of 1 year corresponds to 366 day julian delta.
/* If next year is leap year and base month is < 3
OR
this year is a leap year and month is < 3
=> delta of 1 year corresponds to 366 day julian delta.
*/
days = days - NO_OF_DAYS_IN_A_LEAP_YEAR;
}
......@@ -1039,26 +1042,27 @@ julianDeltaToTimeDelta(struct _juliandelta* jd, struct _datetime* base_dt, struc
}
td_return->year++;
j--;
}while (days >= 0);
j++;
} while (days >= 0);
/* The loop ran one time too much. */
if (days < 0)
{
td_return->year--;
j++;
j--;
}
/* In final year, the crucial point is the month of february. */
if (
((testYearIsLeapYear(j - 1)) && (base_dt->date.month < 3))
||
((testYearIsLeapYear(j)) && (base_dt->date.month >= 3))
)
((testYearIsLeapYear(j+1)) && (base_dt->date.month >= 3))
||
((testYearIsLeapYear(j)) && (base_dt->date.month < 3))
)
{
/* If final year is a leap year and base month is >= 3
OR
final year's previous year is a leap year and month is < 3
=> An addition of leap-year specific delta for each month.
/* If final year is a leap year and base month is >= 3
OR
final year's previous year is a leap year and month is < 3
=> An addition of leap-year specific delta for each month.
*/
msdinm = monthSpecificDeltaInMonthsLeapyear;
ndiny = NO_OF_DAYS_IN_A_LEAP_YEAR;
......@@ -1070,18 +1074,6 @@ julianDeltaToTimeDelta(struct _juliandelta* jd, struct _datetime* base_dt, struc
ndiny = NO_OF_DAYS_IN_A_YEAR_FOR_CAL_TYPE365;
}
/* for (i = NO_OF_MONTHS_IN_A_YEAR; i > 0; i--) */
/* { */
/* if (delta_final_year < (ndiny - msdinm[base_dt->date.month - 1][i - 1])) */
/* { */
/* // Month */
/* td_return->month = NO_OF_MONTHS_IN_A_YEAR - i; */
/* // Day */
/* td_return->day = (int) delta_final_year - (ndiny - msdinm[base_dt->date.month - 1][i]); */
/* break; */
/* } */
/* } */
td_return->month = 0;
td_return->day = (int) delta_final_year;
for (i = NO_OF_MONTHS_IN_A_YEAR; i > 0; i--)
......@@ -1093,7 +1085,7 @@ julianDeltaToTimeDelta(struct _juliandelta* jd, struct _datetime* base_dt, struc
break;
}
}
/* Time */
td_return->hour = (int) jd->ms / NO_OF_MS_IN_A_HOUR;
td_return->minute = ((int) jd->ms - td_return->hour * NO_OF_MS_IN_A_HOUR) / NO_OF_MS_IN_A_MINUTE;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment