diff --git a/src/libiso8601/400_manip.c b/src/libiso8601/400_manip.c index d9489d1..92185ea 100644 --- a/src/libiso8601/400_manip.c +++ b/src/libiso8601/400_manip.c @@ -48,16 +48,22 @@ void iso8601_add_elapsed(struct iso8601_date* date, const struct iso8601_elapsed leapcorrect = _leap_elapsed_day(date->day, date->day + qr.quot); date->day += qr.quot; date->sec += qr.rem - leapcorrect; - if(date->sec < 0) { - --date->day; - date->sec += iso8601_seconds_leap(date); - } date->nsec += per->nsec; if(date->nsec > BILLION) { ++date->sec; date->nsec -= BILLION; } + + if(date->sec < 0) { + --date->day; + date->sec += iso8601_seconds_leap(date); + } + + if(date->sec >= iso8601_seconds_leap(date)) { + date->sec -= iso8601_seconds_leap(date); + ++date->day; + } } @@ -73,6 +79,7 @@ void iso8601_subtract_elapsed(struct iso8601_date* date, const struct iso8601_el date->day -= qr.quot; date->sec -= qr.rem - leapcorrect; date->nsec -= per->nsec; + if(date->nsec < 0) { --date->sec; date->nsec += BILLION; @@ -82,6 +89,7 @@ void iso8601_subtract_elapsed(struct iso8601_date* date, const struct iso8601_el --date->day; date->sec += iso8601_seconds_leap(date); } + if(date->sec >= iso8601_seconds_leap(date)) { date->sec -= iso8601_seconds_leap(date); ++date->day;