mirror of https://github.com/ecmwf/eccodes.git
Add function to check if a date is valid
This commit is contained in:
parent
3692803411
commit
c569fedf35
|
@ -194,3 +194,23 @@ int codes_flush_sync_close_file(FILE* f)
|
|||
}
|
||||
return GRIB_SUCCESS;
|
||||
}
|
||||
|
||||
// Return 1 if input date is valid. Otherwise 0
|
||||
int is_date_valid(long year, long month, long day, long hour, long minute, double second)
|
||||
{
|
||||
// Convert input date to Julian number
|
||||
double result = 0; // Julian number in units of days
|
||||
long year1, month1, day1, hour1, minute1, lSecond1;
|
||||
|
||||
// For validating the date/time, we specify seconds as an integer
|
||||
long lSecond = (long)second;
|
||||
grib_datetime_to_julian(year, month, day, hour, minute, lSecond, &result);
|
||||
|
||||
// Check conversion worked by going other way
|
||||
grib_julian_to_datetime(result, &year1, &month1, &day1, &hour1, &minute1, &lSecond1);
|
||||
if (year1 != year || month1 != month || day1 != day || minute1 != minute || lSecond1 != lSecond) {
|
||||
return 0; // bad date
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -1427,6 +1427,7 @@ int path_is_directory(const char* filename);
|
|||
char* codes_getenv(const char* name);
|
||||
int codes_check_grib_ieee_packing_value(int value);
|
||||
int codes_flush_sync_close_file(FILE* f);
|
||||
int is_date_valid(long year, long month, long day, long hour, long minute, double second);
|
||||
|
||||
/* grib_util.cc*/
|
||||
grib_handle* grib_util_sections_copy(grib_handle* hfrom, grib_handle* hto, int what, int* err);
|
||||
|
|
|
@ -1654,11 +1654,45 @@ static void test_parse_keyval_string()
|
|||
free( (void*)values3[0].name );
|
||||
}
|
||||
|
||||
static void test_dates()
|
||||
{
|
||||
printf("Testing: dates...\n");
|
||||
Assert( is_date_valid(1979,12, 1, 0,0,0) );
|
||||
Assert( is_date_valid(1900, 1, 1, 0,0,0) );
|
||||
Assert( is_date_valid(1964, 4, 6, 0,0,0) );
|
||||
Assert( is_date_valid(2023, 3, 4, 0,0,0) );
|
||||
Assert( is_date_valid(2023, 3, 4, 12,0,0) );
|
||||
Assert( is_date_valid(2023, 3, 4, 0,10,0) );
|
||||
Assert( is_date_valid(2023, 3, 4, 0,0,59) );
|
||||
Assert( is_date_valid(0000, 3, 4, 0,0,0) );
|
||||
Assert( is_date_valid(2020, 2, 29, 0,0,0) );//leap year
|
||||
|
||||
Assert( !is_date_valid( 10, -1, 1, 0,0,0) );// bad months
|
||||
Assert( !is_date_valid(1900, 0, 1, 0,0,0) );
|
||||
Assert( !is_date_valid(1900, 13, 1, 0,0,0) );
|
||||
|
||||
Assert( !is_date_valid(1900, 5, 0, 0,0,0) ); // bad days
|
||||
Assert( !is_date_valid(2000, 5, 32, 0,0,0) );
|
||||
Assert( !is_date_valid(2000, 5, -7, 0,0,0) );
|
||||
|
||||
Assert( !is_date_valid(2000, 5, 8, 99,0,0) );//bad hours
|
||||
Assert( !is_date_valid(2000, 5, 9, -1,0,0) );
|
||||
|
||||
Assert( !is_date_valid(2000, 5, 8, 0, 61,0) );//bad mins
|
||||
Assert( !is_date_valid(2000, 5, 9, 0,-1, 0) );
|
||||
|
||||
Assert( !is_date_valid(2000, 5, 8, 0, 1, -1) );//bad secs
|
||||
Assert( !is_date_valid(2000, 5, 9, 0, 1, 60) );
|
||||
|
||||
Assert( !is_date_valid(2023, 2, 29, 0,0,0) );//Feb
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
printf("Doing unit tests. ecCodes version = %ld\n", grib_get_api_version());
|
||||
|
||||
test_dates();
|
||||
test_logging_proc();
|
||||
test_grib_binary_search();
|
||||
test_parse_keyval_string();
|
||||
|
|
Loading…
Reference in New Issue