Clean up iso8601_valid() et al. into iso8601_invalid().

This commit is contained in:
Laurence Withers 2007-07-23 14:30:18 +00:00
parent 674e705d05
commit 47f35dc992
4 changed files with 31 additions and 28 deletions

View File

@ -1,4 +1,4 @@
/* libiso8601/src/libiso8601/leap.c /* libiso8601/src/libiso8601/100_leap.c
* *
* (c)2006, Laurence Withers, <l@lwithers.me.uk>. * (c)2006, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or * Released under the GNU GPLv2. See file COPYING or
@ -46,27 +46,6 @@ int iso8601_seconds_leap(const struct iso8601_date* date)
int iso8601_valid_leap(const struct iso8601_date* date)
{
int num_ly = 0, i = 0;
if(date->nsec < 0 || date->nsec >= BILLION) return 0;
switch(date->sec) {
case 0 ... 86399:
return 1;
case 86400: /* 23:59:60 */
num_ly = sizeof(leap_second_days) / sizeof(int);
for(i = 0; i < num_ly; ++i) if(leap_second_days[i] == date->day) return 1;
return 0;
}
return 0;
}
static int _leap_elapsed_day(int sday, int eday) static int _leap_elapsed_day(int sday, int eday)
{ {
int spos, epos; int spos, epos;

View File

@ -648,17 +648,17 @@ done:
} }
// correct for timezone offset (note trickery to end up at 23:59:60 iff a leap second was /* correct for timezone offset (note trickery to end up at 23:59:60 iff a leap second was
// requested) * requested), and ensure that any leap second requested was a valid leap second. */
if(details) details->tz_sec = tz_sec; if(details) details->tz_sec = tz_sec;
if(tz_sec && earliest) iso8601_add_seconds(earliest, - leap_sec_req - tz_sec); if(tz_sec && earliest) iso8601_add_seconds(earliest, - leap_sec_req - tz_sec);
if(tz_sec && latest) iso8601_add_seconds(latest, - leap_sec_req - tz_sec); if(tz_sec && latest) iso8601_add_seconds(latest, - leap_sec_req - tz_sec);
// check that, if a leap second was requested, it was a valid leap second
if(leap_sec_req) { if(leap_sec_req) {
ERROR_IF(earliest && earliest->sec != 86400); ERROR_IF(earliest && earliest->sec != 86400);
ERROR_IF(latest && latest->sec != 86400); ERROR_IF(latest && latest->sec != 86400);
} }
ERROR_IF(earliest && iso8601_invalid(earliest));
ERROR_IF(latest && iso8601_invalid(latest));
return 0; return 0;
#undef ERROR_IF #undef ERROR_IF
@ -667,6 +667,27 @@ done:
int iso8601_invalid(const struct iso8601_date* date)
{
int num_ly = 0, i = 0;
if(date->nsec < 0 || date->nsec >= BILLION) return 0;
switch(date->sec) {
case 0 ... 86399:
return 1;
case 86400: /* 23:59:60 */
num_ly = sizeof(leap_second_days) / sizeof(int);
for(i = 0; i < num_ly; ++i) if(leap_second_days[i] == date->day) return 1;
return 0;
}
return 0;
}
/* 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

@ -45,8 +45,12 @@ int iso8601_parse(const char* str, struct iso8601_date* earliest, struct iso8601
\retval -1 if not valid. \retval -1 if not valid.
\retval 0 if valid. \retval 0 if valid.
Checks the details of \a date to ensure that they are sensible. This involves checking that \a sec
is in the range 0 to 86399 (or 86400 if there is a leap second), and that \a nsec is in the range 0
to 999999999.
*/ */
int iso8601_valid(const struct iso8601_date* date); int iso8601_invalid(const struct iso8601_date* date);

View File

@ -38,7 +38,6 @@ 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);
int iso8601_valid_leap(const struct iso8601_date* date);
int iso8601_leap_elapsed(const struct iso8601_date* start, const struct iso8601_date* end); int iso8601_leap_elapsed(const struct iso8601_date* start, const struct iso8601_date* end);
/* options for text editors /* options for text editors