Add iso8601_add_nanoseconds, add define for BILLION

This commit is contained in:
Laurence Withers 2007-07-23 12:14:41 +00:00
parent 20a7d92d1c
commit a0800d0ed2
5 changed files with 25 additions and 5 deletions

View File

@ -7,13 +7,13 @@
#include "iso8601.h" #include "iso8601.h"
// Below are all the includes used throughout the library. /* Below are all the includes used throughout the library. */
#include <errno.h> #include <errno.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define BILLION (1000000000L)
#include <stdio.h>
/* options for text editors /* options for text editors
kate: replace-trailing-space-save true; space-indent true; tab-width 4; kate: replace-trailing-space-save true; space-indent true; tab-width 4;

View File

@ -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_lte(const struct iso8601_date* d1, const struct iso8601_date* d2);
int iso8601_eq(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_seconds(struct iso8601_date* date, long seconds);
void iso8601_add_nanoseconds(struct iso8601_date* date, long nano);
/* leap.c */ /* leap.c */
int iso8601_seconds_leap(const struct iso8601_date* date); int iso8601_seconds_leap(const struct iso8601_date* date);

View File

@ -50,7 +50,7 @@ int iso8601_valid_leap(const struct iso8601_date* date)
{ {
int num_ly = 0, i = 0; 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) { switch(date->sec) {
case 0 ... 86399: case 0 ... 86399:

View File

@ -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 /* options for text editors
kate: replace-trailing-space-save true; space-indent true; tab-width 4; kate: replace-trailing-space-save true; space-indent true; tab-width 4;
vim: expandtab:ts=4:sw=4 vim: expandtab:ts=4:sw=4

View File

@ -46,7 +46,7 @@ int iso8601_parse(const char* str, struct iso8601_date* earliest, struct iso8601
++dig; \ ++dig; \
num *= 10; \ num *= 10; \
num += ch - '0'; \ num += ch - '0'; \
if(num > 1000000000) { \ if(num > BILLION) { \
return -1; \ return -1; \
} \ } \
}while(0) }while(0)