Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 86 additions & 72 deletions lib/datetime/change.c

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

grass/lib/datetime/change.c

Lines 191 to 211 in 6cd5d8b

/* set the new elements to zero */
for (pos = from; pos < dtfrom; pos++)
switch (pos) {
case DATETIME_YEAR:
dt->year = 0;
break;
case DATETIME_MONTH:
dt->month = 0;
break;
case DATETIME_DAY:
dt->day = 0;
break;
case DATETIME_HOUR:
dt->hour = 0;
break;
case DATETIME_MINUTE:
dt->minute = 0;
break;
case DATETIME_SECOND:
dt->second = 0;
break;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

grass/lib/datetime/change.c

Lines 214 to 233 in 6cd5d8b

for (pos = to; pos > dt->to; pos--)
switch (pos) {
case DATETIME_YEAR:
dt->year = 0;
break;
case DATETIME_MONTH:
dt->month = 0;
break;
case DATETIME_DAY:
dt->day = 0;
break;
case DATETIME_HOUR:
dt->hour = 0;
break;
case DATETIME_MINUTE:
dt->minute = 0;
break;
case DATETIME_SECOND:
dt->second = 0;
break;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

grass/lib/datetime/change.c

Lines 236 to 238 in 6cd5d8b

/* make sure that fracsec is zero if original didn't have seconds */
if (dt->to < DATETIME_SECOND)
dt->fracsec = 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

grass/lib/datetime/change.c

Lines 240 to 241 in 6cd5d8b

/* now set the to */
dt->to = to;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

return 0;

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,66 @@
* Read the file GPL.TXT coming with GRASS for details.
*/
#include <grass/datetime.h>

static void make_incr(DateTime *, int, int, DateTime *);

/* Handles the "round == 0" branch for (to < dt->to): increment using all lost values */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
/* Handles the "round == 0" branch for (to < dt->to): increment using all lost values */
/* Handles the "round == 0" branch for (to < dt->to): increment using all lost
* values */

static void apply_round0_lost(DateTime *dt, int to, DateTime *incr)
{
int pos;
int ndays;

if (datetime_is_absolute(dt))
ndays = datetime_days_in_year(dt->year, dt->positive);
else
ndays = 0;

for (pos = dt->to; pos > to; pos--) {
make_incr(incr, pos, pos, dt);
incr->year = dt->year;
incr->month = dt->month;
incr->day = dt->day + ndays / 2;
incr->hour = dt->hour;
incr->minute = dt->minute;
incr->second = dt->second;

datetime_increment(dt, incr);

if (ndays > 0 && pos == DATETIME_DAY)
break;
}
}
/* Returns 1 if any “lost” component between dt->to down to (to+1) is non-default */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
/* Returns 1 if any “lost” component between dt->to down to (to+1) is non-default */
/* Returns 1 if any “lost” component between dt->to down to (to+1) is
* non-default */

static int has_lost_non_default(const DateTime *dt, int to, int x)
{
int pos;

for (pos = dt->to; pos > to; pos--) {
switch (pos) {
case DATETIME_MONTH:
if (dt->month != x)
return 1;
break;
case DATETIME_DAY:
if (dt->day != x)
return 1;
break;
case DATETIME_HOUR:
if (dt->hour != x)
return 1;
break;
case DATETIME_MINUTE:
if (dt->minute != x)
return 1;
break;
case DATETIME_SECOND:
if (dt->second != (double)x)
return 1;
break;
}
}
return 0;
}

/*!
* \brief
*
Expand Down Expand Up @@ -101,78 +158,35 @@ int datetime_change_from_to(DateTime *dt, int from, int to, int round)
}
}

/* if losing precision, round
* round > 0 force up if any lost values not zero
* round ==0 increment by all lost values
*/
if (to < dt->to) {
if (round > 0) {
int x;

x = datetime_is_absolute(dt) ? 1 : 0;

for (carry = 0, pos = dt->to; carry == 0 && pos > to; pos--) {
switch (pos) {
case DATETIME_MONTH:
if (dt->month != x)
carry = 1;
break;
case DATETIME_DAY:
if (dt->day != x)
carry = 1;
break;
case DATETIME_HOUR:
if (dt->hour != 0)
carry = 1;
break;
case DATETIME_MINUTE:
if (dt->minute != 0)
carry = 1;
break;
case DATETIME_SECOND:
if (dt->second != 0)
carry = 1;
break;
}
}

if (carry) {
make_incr(&incr, to, to, dt);

incr.year = 1;
incr.month = 1;
incr.day = 1;
incr.hour = 1;
incr.minute = 1;
incr.second = 1.0;

datetime_increment(dt, &incr);
}
}

if (round == 0) {
/*NEW*/ if (datetime_is_absolute(dt))
/*NEW*/ ndays = datetime_days_in_year(dt->year, dt->positive);
/*NEW*/
else
/*NEW*/ ndays = 0;

for (pos = dt->to; pos > to; pos--) {
make_incr(&incr, pos, pos, dt);

incr.year = dt->year;
incr.month = dt->month;
/*NEW*/ incr.day = dt->day + ndays / 2;
incr.hour = dt->hour;
incr.minute = dt->minute;
incr.second = dt->second;

datetime_increment(dt, &incr);
/*NEW*/ if (ndays > 0 && pos == DATETIME_DAY)
/*NEW*/ break;
}
}
static void round_lost_precision(DateTime *dt, int to, int round)
{
DateTime incr;
int pos, carry, ndays;

if (to >= dt->to)
return;

if (round > 0) {
int x = datetime_is_absolute(dt) ? 1 : 0;

carry = has_lost_non_default(dt, to, x);
}

if (carry) {
make_incr(&incr, to, to, dt);
incr.year = 1; incr.month = 1; incr.day = 1;
incr.hour = 1; incr.minute = 1; incr.second = 1.0;
datetime_increment(dt, &incr);
}
}

if (round == 0) {
apply_round0_lost(dt, to, &incr);
}
}
Comment on lines +161 to +186

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
static void round_lost_precision(DateTime *dt, int to, int round)
{
DateTime incr;
int pos, carry, ndays;
if (to >= dt->to)
return;
if (round > 0) {
int x = datetime_is_absolute(dt) ? 1 : 0;
carry = has_lost_non_default(dt, to, x);
}
if (carry) {
make_incr(&incr, to, to, dt);
incr.year = 1; incr.month = 1; incr.day = 1;
incr.hour = 1; incr.minute = 1; incr.second = 1.0;
datetime_increment(dt, &incr);
}
}
if (round == 0) {
apply_round0_lost(dt, to, &incr);
}
}
static void round_lost_precision(DateTime * dt, int to, int round)
{
DateTime incr;
int pos, carry, ndays;



Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
if (to >= dt->to)
return;

round_lost_precision(dt, to, round);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
round_lost_precision(dt, to, round);
if (round > 0) {
int x = datetime_is_absolute(dt) ? 1 : 0;


/* set the new elements to zero */
for (pos = from; pos < dtfrom; pos++)
Expand Down
60 changes: 17 additions & 43 deletions lib/datetime/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,68 +183,42 @@ static int _datetime_compare(const DateTime *a, const DateTime *b)
int i;

if (a->positive && !b->positive)
return (1);
else if (b->positive && !a->positive)
return (-1);
return 1;
if (b->positive && !a->positive)
return -1;

/* same signs */
for (i = a->from; i <= a->to; i++) {
switch (i) {
int c = 0;

switch (i) {
case DATETIME_SECOND:
if (a->second > b->second)
return (1);
else if (a->second < b->second)
return (-1);
c = (a->second > b->second) - (a->second < b->second);
break;

case DATETIME_MINUTE:
if (a->minute > b->minute)
return (1);
else if (a->minute < b->minute)
return (-1);
c = cmp_int(a->minute, b->minute);
break;

case DATETIME_HOUR:
if (a->hour > b->hour)
return (1);
else if (a->hour < b->hour)
return (-1);
c = cmp_int(a->hour, b->hour);
break;

case DATETIME_DAY:
if (a->day > b->day)
return (1);
else if (a->day < b->day)
return (-1);
c = cmp_int(a->day, b->day);
break;

case DATETIME_MONTH:
if (a->month > b->month)
return (1);
else if (a->month < b->month)
return (-1);
c = cmp_int(a->month, b->month);
break;

case DATETIME_YEAR: /* only place sign matters */
if (a->positive) {
if (a->year > b->year)
return (1);
else if (a->year < b->year)
return (-1);
}
else {
if (a->year < b->year)
return (1);
else if (a->year > b->year)
return (-1);
}
c = cmp_year_signed(a, b);
break;
}

if (c != 0)
return c;
}
return (0);

return 0;
}


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change

/*************************************************************/

static int _datetime_ymd_to_ddays(const DateTime *dtymd, double *days)
Expand Down
Loading
Loading