From a0800d0ed2c5405e7b796463fff93a1d97e86e8e Mon Sep 17 00:00:00 2001 From: Laurence Withers Date: Mon, 23 Jul 2007 12:14:41 +0000 Subject: [PATCH] Add iso8601_add_nanoseconds, add define for BILLION --- src/libiso8601/TopSource.c | 6 +++--- src/libiso8601/functions.h | 1 + src/libiso8601/leap.c | 2 +- src/libiso8601/manip.c | 19 +++++++++++++++++++ src/libiso8601/parser.c | 2 +- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/libiso8601/TopSource.c b/src/libiso8601/TopSource.c index 2045dbf..102cc8d 100644 --- a/src/libiso8601/TopSource.c +++ b/src/libiso8601/TopSource.c @@ -7,13 +7,13 @@ #include "iso8601.h" -// Below are all the includes used throughout the library. +/* Below are all the includes used throughout the library. */ #include +#include #include #include - -#include +#define BILLION (1000000000L) /* options for text editors kate: replace-trailing-space-save true; space-indent true; tab-width 4; diff --git a/src/libiso8601/functions.h b/src/libiso8601/functions.h index de00936..7238fd1 100644 --- a/src/libiso8601/functions.h +++ b/src/libiso8601/functions.h @@ -71,6 +71,7 @@ int iso8601_lt(const struct iso8601_date* d1, const struct iso8601_date* d2); int iso8601_lte(const struct iso8601_date* d1, const struct iso8601_date* d2); int iso8601_eq(const struct iso8601_date* d1, const struct iso8601_date* d2); void iso8601_add_seconds(struct iso8601_date* date, long seconds); +void iso8601_add_nanoseconds(struct iso8601_date* date, long nano); /* leap.c */ int iso8601_seconds_leap(const struct iso8601_date* date); diff --git a/src/libiso8601/leap.c b/src/libiso8601/leap.c index 4ea0bcf..4893473 100644 --- a/src/libiso8601/leap.c +++ b/src/libiso8601/leap.c @@ -50,7 +50,7 @@ int iso8601_valid_leap(const struct iso8601_date* date) { int num_ly = 0, i = 0; - if(date->nsec < 0 || date->nsec >= 1000000000) return 0; + if(date->nsec < 0 || date->nsec >= BILLION) return 0; switch(date->sec) { case 0 ... 86399: diff --git a/src/libiso8601/manip.c b/src/libiso8601/manip.c index e611a1d..34873ab 100644 --- a/src/libiso8601/manip.c +++ b/src/libiso8601/manip.c @@ -68,6 +68,25 @@ void iso8601_add_seconds(struct iso8601_date* date, long seconds) +void iso8601_add_nanoseconds(struct iso8601_date* date, long nsec) +{ + ldiv_t qr; + + qr = ldiv(nsec, BILLION); + date->nsec += qr.rem; + if(date->nsec > BILLION) { + date->nsec -= BILLION; + ++qr.quot; + } else if(date->nsec < 0) { + date->nsec += BILLION; + --qr.quot; + } + + iso8601_add_seconds(date, qr.quot); +} + + + /* options for text editors kate: replace-trailing-space-save true; space-indent true; tab-width 4; vim: expandtab:ts=4:sw=4 diff --git a/src/libiso8601/parser.c b/src/libiso8601/parser.c index 40dc41f..5a9b2ef 100644 --- a/src/libiso8601/parser.c +++ b/src/libiso8601/parser.c @@ -46,7 +46,7 @@ int iso8601_parse(const char* str, struct iso8601_date* earliest, struct iso8601 ++dig; \ num *= 10; \ num += ch - '0'; \ - if(num > 1000000000) { \ + if(num > BILLION) { \ return -1; \ } \ }while(0)