Skip to content
This repository has been archived by the owner on Sep 30, 2020. It is now read-only.

Time t 1970 check #203

Merged
merged 4 commits into from
Oct 3, 2013
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions devel/cmake/Tests/test_mktime1970.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>

int main(int argc, char ** argv) {
struct tm ts;
ts.tm_sec = 0;
ts.tm_min = 0;
ts.tm_hour = 0;
ts.tm_mday = 1;
ts.tm_mon = 0;
ts.tm_year = 0;
ts.tm_isdst = -1;
{
time_t t = mktime( &ts );
if (t == -1)
exit(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand. Isn't this slightly backwards? exit(0) from main() typically means "successful" while mktime() returns -1 if an error occurred.

Also, is there a reason you're using POSIX interfaces when the standard <time.h> also provides mktime()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arggh: How to interleave answers in this interface?

I don't understand. Isn't this slightly backwards? exit(0) from main() typically means "successful" while mktime() returns -1 if an error occurred.

yes - it is backwards; I was waiting for that comment. Will fix before merging.

The POSIX thing was an oversight though. Will fix.

Thanks for the review.

else
exit(1);
}
}
5 changes: 5 additions & 0 deletions devel/cmake/ert_check.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ if (ISREG_POSIX)
add_definitions( -DHAVE_ISREG )
endif()

try_run( RUN_RESULT COMPILE_RESULT ${CMAKE_BINARY_DIR} ${PROJECT_SOURCE_DIR}/cmake/Tests/test_mktime1970.c )
if (RUN_RESULT)
add_defintions( -DTIME_T_1970 )
endif()

set( CMAKE_C_FLAGS ${CMAKE_C_FLAGS_main} )
set( CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_main} )

2 changes: 2 additions & 0 deletions devel/libecl/include/ert/ecl/ecl_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ bool ecl_util_fmt_file(const char * filename , bool * __fmt_file);
char * ecl_util_alloc_exfilename_anyfmt(const char * path, const char * base , ecl_file_enum file_type , bool start_fmt , int report_nr);
int ecl_util_get_month_nr(const char * month_name);
int ecl_util_fname_report_cmp(const void *f1, const void *f2);
time_t ecl_util_make_date(int mday , int month , int year);
time_t ecl_util_make_date__(int mday , int month , int year, int * year_offset);

bool ecl_util_valid_basename( const char * basename );
const char * ecl_util_get_phase_name( ecl_phase_enum phase );
Expand Down
2 changes: 1 addition & 1 deletion devel/libecl/src/ecl_rft_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ ecl_rft_node_type * ecl_rft_node_alloc(const ecl_file_type * rft) {
/* Time information. */
{
int * time = ecl_kw_get_int_ptr( date_kw );
rft_node->recording_date = util_make_date( time[DATE_DAY_INDEX] , time[DATE_MONTH_INDEX] , time[DATE_YEAR_INDEX] );
rft_node->recording_date = ecl_util_make_date( time[DATE_DAY_INDEX] , time[DATE_MONTH_INDEX] , time[DATE_YEAR_INDEX] );
}
rft_node->days = ecl_kw_iget_float( ecl_file_iget_named_kw( rft , TIME_KW , 0 ) , 0);
if (ecl_file_has_kw( rft , CONLENST_KW))
Expand Down
2 changes: 1 addition & 1 deletion devel/libecl/src/ecl_rsthead.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


static time_t rsthead_date( int day , int month , int year) {
return util_make_date( day , month, year );
return ecl_util_make_date( day , month, year );
}


Expand Down
6 changes: 3 additions & 3 deletions devel/libecl/src/ecl_smspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1058,9 +1058,9 @@ static void ecl_smspec_fread_header(ecl_smspec_type * ecl_smspec, const char * h

{
int * date = ecl_kw_get_int_ptr(startdat);
ecl_smspec->sim_start_time = util_make_date(date[STARTDAT_DAY_INDEX] ,
date[STARTDAT_MONTH_INDEX] ,
date[STARTDAT_YEAR_INDEX]);
ecl_smspec->sim_start_time = ecl_util_make_date(date[STARTDAT_DAY_INDEX] ,
date[STARTDAT_MONTH_INDEX] ,
date[STARTDAT_YEAR_INDEX]);
}

ecl_smspec->grid_dims[0] = ecl_kw_iget_int(dimens , DIMENS_SMSPEC_NX_INDEX );
Expand Down
36 changes: 34 additions & 2 deletions devel/libecl/src/ecl_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ time_t ecl_util_get_start_date(const char * data_file) {
int day, year, month_nr;
if ( util_sscanf_int( stringlist_iget( tokens , 0 ) , &day) && util_sscanf_int( stringlist_iget(tokens , 2) , &year)) {
month_nr = ecl_util_get_month_nr(stringlist_iget( tokens , 1));
start_date = util_make_date(day , month_nr , year );
start_date = ecl_util_make_date(day , month_nr , year );
} else
util_abort("%s: failed to parse DAY MONTH YEAR from : \"%s\" \n",__func__ , buffer);
stringlist_free( tokens );
Expand Down Expand Up @@ -1364,7 +1364,7 @@ void ecl_util_append_month_range( time_t_vector_type * date_list , time_t start_
} else
month += 1;

current_date = util_make_date( 1 , month , year );
current_date = ecl_util_make_date( 1 , month , year );
if (current_date < end_date)
time_t_vector_append( date_list , current_date );
else {
Expand All @@ -1389,6 +1389,38 @@ void ecl_util_init_month_range( time_t_vector_type * date_list , time_t start_da
}




time_t ecl_util_make_date__(int mday , int month , int year, int * __year_offset) {
time_t date;

#ifdef TIME_T_1970
static bool offset_initialized = false;
static int year_offset = 0;

if (!offset_initialized) {
if (year < 1970) {
year_offset = 2000 - year;
fprintf(stderr,"Warning: all year values will be shifted %d years forward. \n", year_offset);
}
offset_initialized = true;
}
*__year_offset = year_offset;
date = util_make_date(mday , month , year + year_offset);
#else
*__year_offset = 0;
date = util_make_date(mday , month , year);
#endif

return date;
}


time_t ecl_util_make_date(int mday , int month , int year) {
int year_offset;
return ecl_util_make_date__( mday , month , year , &year_offset);
}

/*****************************************************************/
/* Small functions to support enum introspection. */

Expand Down
48 changes: 48 additions & 0 deletions devel/libecl/tests/ecl_util_make_date_no_shift.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright (C) 2013 Statoil ASA, Norway.

The file 'ecl_util_make_date_no_shift.c' is part of ERT - Ensemble based Reservoir Tool.

ERT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ERT is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
for more details.
*/
#include <stdlib.h>
#include <stdbool.h>

#include <ert/util/test_util.h>
#include <ert/util/time_t_vector.h>
#include <ert/util/util.h>

#include <ert/ecl/ecl_util.h>


void test_date(int mday, int month , int year) {
time_t t0 = ecl_util_make_date( mday , month , year );
time_t t1 = util_make_date( mday , month , year);

test_assert_time_t_equal( t0 , t1 );
}

void test_offset(int mday, int month , int year) {
int year_offset;
ecl_util_make_date__( mday , month , year , &year_offset);
test_assert_int_equal( 0 , year_offset );
}



int main(int argc , char ** argv) {
test_date(10,10,2000);
test_offset(10,10,2000);
// test_assert_util_abort( make_date( 1 , 1 , 0);
exit(0);
}
51 changes: 51 additions & 0 deletions devel/libecl/tests/ecl_util_make_date_shift.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Copyright (C) 2013 Statoil ASA, Norway.

The file 'ecl_util_make_date_shift.c' is part of ERT - Ensemble based Reservoir Tool.

ERT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ERT is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
for more details.
*/
#include <stdlib.h>
#include <stdbool.h>

#include <ert/util/test_util.h>
#include <ert/util/time_t_vector.h>
#include <ert/util/util.h>

#include <ert/ecl/ecl_util.h>


void test_date(int mday, int month , int year, int * year_offset) {
time_t t0 = ecl_util_make_date__( mday , month , year , year_offset);
time_t t1 = util_make_date( mday , month , year + *year_offset);
test_assert_time_t_equal( t0 , t1 );
}


void test_offset(int mday, int month , int year , int current_offset) {
int year_offset;
time_t t0 = ecl_util_make_date__( mday , month , year , &year_offset);
time_t t1 = util_make_date( mday , month , year + current_offset);

test_assert_time_t_equal(t0,t1);
test_assert_int_equal( current_offset , year_offset );
}




int main(int argc , char ** argv) {
int year_offset;
test_date(1,1,0 , &year_offset);
test_offset(1,1,1000 , year_offset);
exit(0);
}
7 changes: 7 additions & 0 deletions devel/libecl/tests/tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ add_executable( ecl_restart_test ecl_restart_test.c )
target_link_libraries( ecl_restart_test ecl test_util )
add_test( ecl_restart_test ${EXECUTABLE_OUTPUT_PATH}/ecl_restart_test ${PROJECT_SOURCE_DIR}/test-data/Statoil/ECLIPSE/Gurbat/ECLIPSE.UNRST )

add_executable( ecl_util_make_date_no_shift ecl_util_make_date_no_shift.c )
target_link_libraries( ecl_util_make_date_no_shift ecl test_util )
add_test( ecl_util_make_date_no_shift ${EXECUTABLE_OUTPUT_PATH}/ecl_util_make_date_no_shift )

add_executable( ecl_util_make_date_shift ecl_util_make_date_shift.c )
target_link_libraries( ecl_util_make_date_shift ecl test_util )
add_test( ecl_util_make_date_shift ${EXECUTABLE_OUTPUT_PATH}/ecl_util_make_date_shift )

add_executable( ecl_grid_lgr_name ecl_grid_lgr_name.c )
target_link_libraries( ecl_grid_lgr_name ecl test_util )
Expand Down