Skip to content

Commit

Permalink
Merge pull request #6 from tri-star/hotfix-from-to-bug
Browse files Browse the repository at this point in the history
Fix --from, --to option parse bug.
  • Loading branch information
Tristar committed Aug 25, 2014
2 parents 8543342 + 95d73bc commit e9c2664
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 38 deletions.
5 changes: 3 additions & 2 deletions src/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "application_config_validator.h"
#include "log_type_repository.h"
#include "grep_command.h"
#include "util/date_util.h"

#include <boost/regex.hpp>
#include <boost/program_options.hpp>
Expand Down Expand Up @@ -180,8 +181,8 @@ namespace tigrep {

grep_config->pattern = boost::regex(*regex_string);
grep_config->format = *format;
grep_config->start_date_time = app_config_.getStartTime();
grep_config->end_date_time = app_config_.getEndTime();
grep_config->start_date_time = util::DateUtil::stringToTime(app_config_.start_time, *format);
grep_config->end_date_time = util::DateUtil::stringToTime(app_config_.end_time, *format);
}

/**
Expand Down
17 changes: 0 additions & 17 deletions src/application_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,6 @@ namespace tigrep {
return input_file.length() == 0 ? false : true;
}

time_t getStartTime() {
struct tm tm;
char* result = strptime(start_time.c_str(), format.c_str(), &tm);
if(result == start_time.c_str() || result == 0) {
return 0;
}
return mktime(&tm);
}

time_t getEndTime() {
struct tm tm;
char* result = strptime(end_time.c_str(), format.c_str(), &tm);
if(result == end_time.c_str() || result == 0) {
return 0;
}
return mktime(&tm);
}
};

} //namespace
Expand Down
2 changes: 2 additions & 0 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ add_executable(run_tests
grep_command_test.cc
log_type_repository_test.cc
util/date_compare_test.cc
util/date_util_test.cc
../application_config_validator.h
../grep_command.h
../log_type_repository.h
../util/date_compare.h
../util/date_util.h

)
target_link_libraries(run_tests ${TIGREP_LINK_LIBRARIES_FOR_TEST})
Expand Down
19 changes: 0 additions & 19 deletions src/tests/application_config_validator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,5 @@ namespace tigrep {
validator.getErrors(&errors);
assertMessageIncluded(errors, "invalid end date time.");
}
/*
TEST_F(ApplicationConfigValidatorTest, with_start_time_omitted) {
ApplicationConfig app_config;
fillWithValidInformation(app_config);
app_config.start_time = "";
ApplicationConfigValidator validator;
bool is_ok = validator.validate(app_config);
ASSERT_TRUE(is_ok);
time_t actual_time = app_config.getStartTime();
struct tm tm;
memset(&tm, 0, sizeof(struct tm));
time_t expected_time = mktime(&tm);
ASSERT_EQ(expected_time, actual_time);
}
*/

} //namespace
22 changes: 22 additions & 0 deletions src/tests/util/date_util_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "../../util/date_util.h"

#include <string>

#include <gtest/gtest.h>


namespace tigrep {

namespace util {

TEST(DateUtilTest, basic) {
std::string time = "2014-08-08 10:00:00";
std::string format = "%Y-%m-%d %H:%M:%S";
time_t expected = 1407459600; //date -d "2014-08-08 10:00:00" +%s
time_t actual = DateUtil::stringToTime(time, format);

ASSERT_EQ(expected, actual);
}

} //namespace
} //namespace
30 changes: 30 additions & 0 deletions src/util/date_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef TIGREP_UTIL_DATE_UTIL_H_
#define TIGREP_UTIL_DATE_UTIL_H_

#include <string>
#include <ctime>

namespace tigrep {

namespace util {

class DateUtil {

public:

static time_t stringToTime(const std::string& time, const std::string& format) {
struct tm tm;
char* result = strptime(time.c_str(), format.c_str(), &tm);
if(result == time.c_str() || result == 0) {
return 0;
}
return mktime(&tm);
}

};

} //namespace
} //namespace


#endif

0 comments on commit e9c2664

Please sign in to comment.