diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d94a95..d9f5963 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,9 @@ cmake_minimum_required(VERSION 2.8) include(FindOpenSSL) -include_directories( ${grive_SOURCE_DIR}/src ) +include_directories( + ${grive_SOURCE_DIR}/src +) add_executable( grive src/main.cc @@ -14,10 +16,22 @@ add_executable( grive src/protocol/HTTP.cc src/protocol/Json.cc src/protocol/OAuth2.cc + src/util/DateTime.hh src/util/OS.cc ) target_link_libraries( grive curl json - ${OPENSSL_LIBRARIES} ) + ${OPENSSL_LIBRARIES} +) + +add_executable( unittest + test/UnitTest.cc + src/util/DateTime.cc + test/util/DateTimeTest.cc +) + +target_link_libraries( unittest + cppunit +) diff --git a/src/util/DateTime.cc b/src/util/DateTime.cc new file mode 100644 index 0000000..b396885 --- /dev/null +++ b/src/util/DateTime.cc @@ -0,0 +1,64 @@ +/* + grive: an GPL program to sync a local directory with Google Drive + Copyright (C) 2012 Wan Wai Ho + + This program 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 version 2 + of the License. + + This program 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 for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "DateTime.hh" + +#include +#include + +#include + +namespace gr { + +DateTime::DateTime( ) : + m_sec ( 0 ), + m_usec ( 0 ) +{ +} + +DateTime::DateTime( const std::string& iso ) : + m_sec ( 0 ), + m_usec ( 0 ) +{ + struct tm tp ; + const char *r = ::strptime( iso.c_str(), "%Y-%m-%dT%H:%M:%S.", &tp ) ; + + m_sec = ::mktime( &tp ) ; + if ( r != 0 ) + m_usec = std::atoi( r ) * 1000 ; +} + +struct tm DateTime::Tm() const +{ + struct tm tp ; + gmtime_r( &m_sec, &tp ) ; + return tp ; +} + +std::time_t DateTime::Sec( ) const +{ + return m_sec ; +} + +unsigned long DateTime::USec( ) const +{ + return m_usec ; +} + +} // end of namespace diff --git a/src/util/DateTime.hh b/src/util/DateTime.hh new file mode 100644 index 0000000..97a1764 --- /dev/null +++ b/src/util/DateTime.hh @@ -0,0 +1,43 @@ +/* + grive: an GPL program to sync a local directory with Google Drive + Copyright (C) 2012 Wan Wai Ho + + This program 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 version 2 + of the License. + + This program 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 for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#pragma once + +#include +#include + +namespace gr { + +class DateTime +{ +public : + DateTime( ) ; + explicit DateTime( const std::string& iso ) ; + + std::time_t Sec( ) const ; + unsigned long USec( ) const ; + + struct tm Tm() const ; + +private : + std::time_t m_sec ; + unsigned long m_usec ; +} ; + +} // end of namespace diff --git a/src/util/OS.hh b/src/util/OS.hh index 59d542e..a108357 100644 --- a/src/util/OS.hh +++ b/src/util/OS.hh @@ -21,8 +21,14 @@ #include -namespace gr { namespace os { +namespace gr { -void MakeDir( const std::string& dir ) ; +class DateTime ; -} } // end of namespaces +namespace os +{ + void MakeDir( const std::string& dir ) ; + DateTime FileMTime( const std::string& file ) ; +} + +} // end of namespaces diff --git a/test/UnitTest.cc b/test/UnitTest.cc new file mode 100644 index 0000000..3b9fc5e --- /dev/null +++ b/test/UnitTest.cc @@ -0,0 +1,33 @@ +/* + grive: an GPL program to sync a local directory with Google Drive + Copyright (C) 2012 Wan Wai Ho + + This program 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 version 2 + of the License. + + This program 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 for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include + +#include "util/DateTimeTest.hh" + +int main( int argc, char **argv ) +{ + using namespace grut ; + + CppUnit::TextUi::TestRunner runner; + runner.addTest( DateTimeTest::suite( ) ) ; + runner.run(); + + return 0 ; +} diff --git a/test/util/DateTimeTest.cc b/test/util/DateTimeTest.cc new file mode 100644 index 0000000..88c4820 --- /dev/null +++ b/test/util/DateTimeTest.cc @@ -0,0 +1,40 @@ +/* + grive: an GPL program to sync a local directory with Google Drive + Copyright (C) 2012 Wan Wai Ho + + This program 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 version 2 + of the License. + + This program 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 for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "DateTimeTest.hh" + +#include "util/DateTime.hh" + +namespace grut { + +using namespace gr ; + +DateTimeTest::DateTimeTest( ) +{ +} + +void DateTimeTest::TestParseIso( ) +{ + DateTime subject( "2009-07-29T20:31:39.804Z" ) ; + struct tm tp = subject.Tm() ; + CPPUNIT_ASSERT( tp.tm_year == 109 ) ; + CPPUNIT_ASSERT( tp.tm_sec == 39 ) ; +} + +} // end of namespace grut diff --git a/test/util/DateTimeTest.hh b/test/util/DateTimeTest.hh new file mode 100644 index 0000000..79cf9af --- /dev/null +++ b/test/util/DateTimeTest.hh @@ -0,0 +1,41 @@ +/* + grive: an GPL program to sync a local directory with Google Drive + Copyright (C) 2012 Wan Wai Ho + + This program 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 version 2 + of the License. + + This program 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 for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#pragma once + +#include +#include + +namespace grut { + +class DateTimeTest : public CppUnit::TestFixture +{ +public : + DateTimeTest( ) ; + + // declare suit function + CPPUNIT_TEST_SUITE( DateTimeTest ) ; + CPPUNIT_TEST( TestParseIso ) ; + CPPUNIT_TEST_SUITE_END(); + +private : + void TestParseIso( ) ; +} ; + +} // end of namespace