Skip to content
Snippets Groups Projects
Commit d237e51e authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

datetime2rtimeval(): refactor to JulianDate

parent a42d2b99
No related branches found
No related tags found
No related merge requests found
Pipeline #18333 failed
#include "julian_date.h"
JulianDate
julianDateEncode(int calendar, CdiDateTime dt)
julianDate_encode(int calendar, CdiDateTime dt)
{
JulianDate julianDate;
......@@ -10,3 +10,39 @@ julianDateEncode(int calendar, CdiDateTime dt)
return julianDate;
}
static void
adjust_seconds(int64_t *julianDay, double *secondOfDay)
{
const double SecondsPerDay = 86400.0;
while (*secondOfDay >= SecondsPerDay)
{
*secondOfDay -= SecondsPerDay;
(*julianDay)++;
}
while (*secondOfDay < 0)
{
*secondOfDay += SecondsPerDay;
(*julianDay)--;
}
}
// subtract jjulianDate1 from julianDate2 and returns the result in seconds
double
julianDate_sub(JulianDate julianDate1, JulianDate julianDate2, int64_t *days, double *secs)
{
*days = julianDate2.julianDay - julianDate1.julianDay;
*secs = julianDate2.secondOfDay - julianDate1.secondOfDay;
double sec_of_day = *secs;
adjust_seconds(days, &sec_of_day);
*secs = sec_of_day;
double seconds = (*days) * 86400.0 + sec_of_day;
return seconds;
}
......@@ -16,7 +16,8 @@ typedef struct
double secondOfDay;
} JulianDate;
JulianDate julianDateEncode(int calendar, CdiDateTime dt);
JulianDate julianDate_encode(int calendar, CdiDateTime dt);
double julianDate_sub(JulianDate julianDate1, JulianDate julianDate2, int64_t *days, double *secs);
#ifdef __cplusplus
}
......
......@@ -1290,8 +1290,9 @@ datetime2rtimeval(CdiDateTime vdatetime, taxis_t *taxis)
int64_t days;
double secs;
int timeunit = (*taxis).unit;
const int calendar = (*taxis).calendar;
int timeunits = (*taxis).unit;
const int timeunits0 = timeunits;
CdiDateTime rdatetime = (*taxis).rdatetime;
......@@ -1299,28 +1300,16 @@ datetime2rtimeval(CdiDateTime vdatetime, taxis_t *taxis)
if (cdiDateTime_isNull(rdatetime) && cdiDateTime_isNull(vdatetime)) return value;
int year = taxis->rdatetime.date.year;
int month = taxis->rdatetime.date.month;
int day = taxis->rdatetime.date.day;
int hour = taxis->rdatetime.time.hour;
int minute = taxis->rdatetime.time.minute;
int second = taxis->rdatetime.time.second;
int ms = taxis->rdatetime.time.ms;
const int ryear = year;
const int rmonth = month;
int64_t julianDay1 = encode_calday(calendar, ryear, rmonth, day);
double secondOfDay1 = encode_secofday(hour, minute, second, ms);
const JulianDate julianDate1 = julianDate_encode(calendar, rdatetime);
cdiDateDecode(vdatetime.date, &year, &month, &day);
cdiTimeDecode(vdatetime.time, &hour, &minute, &second, &ms);
if (timeunits == TUNIT_MONTH && calendar == CALENDAR_360DAYS) timeunits = TUNIT_DAY;
int timeunit0 = timeunit;
if (timeunit == TUNIT_MONTH && calendar == CALENDAR_360DAYS) timeunit = TUNIT_DAY;
if (timeunit == TUNIT_MONTH || timeunit == TUNIT_YEAR)
if (timeunits == TUNIT_MONTH || timeunits == TUNIT_YEAR)
{
const int ryear = rdatetime.date.year;
const int rmonth = rdatetime.date.month;
int year = vdatetime.date.year;
int month = vdatetime.date.month;
value = (year - ryear) * 12 - rmonth + month;
int nmonth = (int) value;
......@@ -1331,28 +1320,25 @@ datetime2rtimeval(CdiDateTime vdatetime, taxis_t *taxis)
while (month < 1) { month += 12; year--; }
// clang-format on
int dpm = days_per_month(calendar, year, month);
int64_t julianDay2 = encode_calday(calendar, year, month, day);
double secondOfDay2 = encode_secofday(hour, minute, second, ms);
const int dpm = days_per_month(calendar, year, month);
julday_sub(julianDay1, secondOfDay1, julianDay2, secondOfDay2, &days, &secs);
vdatetime.date.year = year;
vdatetime.date.month = month;
const JulianDate julianDate2 = julianDate_encode(calendar, vdatetime);
julianDate_sub(julianDate1, julianDate2, &days, &secs);
value += (days + secs / 86400.0) / dpm;
if (timeunit == TUNIT_YEAR) value = value / 12;
if (timeunits == TUNIT_YEAR) value = value / 12;
}
else
{
int64_t julianDay2 = encode_calday(calendar, year, month, day);
double secondOfDay2 = encode_secofday(hour, minute, second, ms);
julday_sub(julianDay1, secondOfDay1, julianDay2, secondOfDay2, &days, &secs);
const JulianDate julianDate2 = julianDate_encode(calendar, vdatetime);
julianDate_sub(julianDate1, julianDate2, &days, &secs);
value = cdi_encode_timevalue(days, secs, timeunit);
value = cdi_encode_timevalue(days, secs, timeunits);
}
if (timeunit0 == TUNIT_MONTH && calendar == CALENDAR_360DAYS) value /= 30.0;
if (timeunits0 == TUNIT_MONTH && calendar == CALENDAR_360DAYS) value /= 30.0;
return value;
}
......@@ -1535,7 +1521,7 @@ cdiSetForecastPeriod(double timevalue, taxis_t *taxis)
(*taxis).vdatetime.date.month = month;
}
JulianDate julianDate = julianDateEncode(calendar, (*taxis).vdatetime);
JulianDate julianDate = julianDate_encode(calendar, (*taxis).vdatetime);
int64_t days;
double secs;
......
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