Skip to content
Snippets Groups Projects
Commit d57684ab authored by Florian Prill's avatar Florian Prill
Browse files

[develop] allow for scalar timeDelta multiplication up to 29 days.

parent 60e8af19
No related branches found
No related tags found
No related merge requests found
......@@ -94,6 +94,11 @@ PROGRAM example
td = t_timedelta("PT1H")
td = td*0.5_c_double
WRITE (0,*) " PT1H * 0.5 = ", td%toString()
td = t_timedelta("PT1H")
td = td*25
WRITE (0,*) " PT1H * 25 = ", td%toString()
! Cray bug? Doesn't seem to recognize OPERATOR(*)
#ifndef _CRAYFTN
td = t_timedelta("PT1H")
......
......@@ -1774,6 +1774,10 @@ moduloTimeDeltaFromDateTime(struct _datetime* start_dt, struct _timedelta* times
struct _timedelta*
elementwiseScalarMultiplyTimeDelta(struct _timedelta* base_td, int64_t lambda, struct _timedelta* scaled_td)
{
/* scalar multiplication is well-defined/implmented for
days<=MAX_DAYS_ALLOWED. */
const int MAX_DAYS_ALLOWED = 29;
if ((base_td != NULL) && (scaled_td != NULL) )
{
/* Scalar multiplication not supported for TimeDeltas consisting of day/month/year. */
......@@ -1793,6 +1797,9 @@ elementwiseScalarMultiplyTimeDelta(struct _timedelta* base_td, int64_t lambda, s
if (lambda < 0)
lambda *= -1;
scaled_td->day = 0;
scaled_td->month = 0;
scaled_td->year = 0;
/* Multiply each element by scalar. */
......@@ -1818,17 +1825,18 @@ elementwiseScalarMultiplyTimeDelta(struct _timedelta* base_td, int64_t lambda, s
}
scaled_td->hour += (int) lambda*base_td->hour;
/* Scalar multiplication can not give a value in excess of 24 hours. */
if ( scaled_td->hour >= NO_OF_HOURS_IN_A_DAY )
{
/* ERROR: Return on NULL. */
return NULL;
}
scaled_td->day = 0;
scaled_td->month = 0;
scaled_td->year = 0;
while ( scaled_td->hour >= NO_OF_HOURS_IN_A_DAY )
{
scaled_td->hour -= NO_OF_HOURS_IN_A_DAY;
scaled_td->day += 1;
/* scalar multiplication is well-defined/implemented for
days<=MAX_DAYS_ALLOWED. */
if ( scaled_td->day > MAX_DAYS_ALLOWED )
{
/* ERROR: Return on NULL. */
return NULL;
}
}
return scaled_td;
......@@ -1864,6 +1872,10 @@ elementwiseScalarMultiplyTimeDelta(struct _timedelta* base_td, int64_t lambda, s
struct _timedelta*
elementwiseScalarMultiplyTimeDeltaDP(struct _timedelta* base_td, double lambda, struct _timedelta* scaled_td)
{
/* scalar multiplication is well-defined/implmented for
days<=MAX_DAYS_ALLOWED. */
const int MAX_DAYS_ALLOWED = 29;
if ((base_td != NULL) && (scaled_td != NULL) )
{
/* Scalar multiplication not supported for TimeDeltas consisting of day/month/year. */
......@@ -1880,24 +1892,34 @@ elementwiseScalarMultiplyTimeDeltaDP(struct _timedelta* base_td, double lambda,
if (lambda < 0.)
lambda *= -1.;
scaled_td->day = 0;
scaled_td->month = 0;
scaled_td->year = 0;
/* Multiply each element by scalar. */
scaled_td->hour = (int) lambda*base_td->hour;
/* Scalar multiplication can not give a value in excess of 24 hours. */
if ( scaled_td->hour >= NO_OF_HOURS_IN_A_DAY )
{
/* ERROR: Return on NULL. */
return NULL;
}
double remainder_minutes = 60.*(lambda*base_td->hour - scaled_td->hour);
scaled_td->minute = (int) (lambda*base_td->minute + remainder_minutes);
double remainder_seconds = 60.*(lambda*base_td->minute + remainder_minutes - scaled_td->minute);
scaled_td->second += (int) (lambda*base_td->second + remainder_seconds);
double remainder_ms = 1000.*((lambda*base_td->second + remainder_seconds) - scaled_td->second);
scaled_td->ms = (int) (lambda*base_td->ms + remainder_ms);
scaled_td->day = 0;
scaled_td->month = 0;
scaled_td->year = 0;
while ( scaled_td->hour >= NO_OF_HOURS_IN_A_DAY )
{
scaled_td->hour -= NO_OF_HOURS_IN_A_DAY;
scaled_td->day += 1;
/* scalar multiplication is well-defined/implemented for
days<=MAX_DAYS_ALLOWED. */
if ( scaled_td->day > MAX_DAYS_ALLOWED )
{
/* ERROR: Return on NULL. */
return NULL;
}
}
return scaled_td;
}
else
......
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