ignoring non-normalized values

This commit is contained in:
Lev Walkin 2005-07-04 01:44:01 +00:00
parent 398176b2a7
commit 4b4c146c70
2 changed files with 67 additions and 6 deletions

View File

@ -559,18 +559,23 @@ asn_time2GT_frac(GeneralizedTime_t *opt_gt, const struct tm *tm, long frac_value
/*
* Deal with fractions.
*/
if(frac_base >= 10 && frac_value > 0) {
if(frac_base >= 10
&& frac_value > 0
/* 1001 ms? should ignore or adjust seconds */
&& (frac_value/frac_base) == 0
) {
char *end = p + 1 + 6; /* '.' + maximum 6 digits */
char *z;
*p++ = '.';
char *z = p;
*z++ = '.';
frac_value %= frac_base;
do {
int digit;
frac_base /= 10;
digit = frac_value / frac_base;
frac_value %= frac_base;
*p++ = digit + 0x30;
} while(frac_base >= 10 && frac_value > 0 && p < end);
for(z = p - 1; *z == 0x30; --z); /* Strip zeroes */
*z++ = digit + 0x30;
} while(frac_base >= 10 && frac_value > 0 && z < end);
for(--z; *z == 0x30; --z); /* Strip zeroes */
p = z + (*z != '.');
size = p - buf;
}

View File

@ -83,11 +83,67 @@ recode(char *time_str, const char *expect) {
FREEMEM(gt.buf);
}
static void
check_fractions() {
GeneralizedTime_t *gt = 0;
struct tm tm;
memset(&tm, 0, sizeof tm);
tm.tm_year = 70;
tm.tm_mday = 1;
gt = asn_time2GT_frac(gt, &tm, -1, -1, 1);
assert(gt);
printf("[%s]\n", gt->buf);
assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
gt = asn_time2GT_frac(gt, &tm, 0, 0, 1);
assert(gt);
printf("[%s]\n", gt->buf);
assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
gt = asn_time2GT_frac(gt, &tm, 0, -1, 1);
assert(gt);
printf("[%s]\n", gt->buf);
assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
gt = asn_time2GT_frac(gt, &tm, -1, 0, 1);
assert(gt);
printf("[%s]\n", gt->buf);
assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
gt = asn_time2GT_frac(gt, &tm, 10, 0, 1);
assert(gt);
printf("[%s]\n", gt->buf);
assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
/* Normalization should happen prior calling the _frac() */
gt = asn_time2GT_frac(gt, &tm, 55, 10, 1);
assert(gt);
printf("[%s]\n", gt->buf);
assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
gt = asn_time2GT_frac(gt, &tm, 10, 20, 1);
assert(gt);
printf("[%s]\n", gt->buf);
assert(strcmp((char *)gt->buf, "19700101000000.5Z") == 0);
gt = asn_time2GT_frac(gt, &tm, -10, 20, 1);
assert(gt);
printf("[%s]\n", gt->buf);
assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
FREEMEM(gt->buf);
FREEMEM(gt);
}
int
main(int ac, char **av) {
(void)av;
check_fractions();
recognize("200401250", -1, 0);
recognize("2004012509300", -1, 0);
recognize("20040125093000-", -1, 0);