added date time class and associate unit test case

pull/40/head
Matchman Green 2012-04-27 15:41:46 +08:00
parent dfd7eac5d9
commit f0ca94d7e0
7 changed files with 246 additions and 5 deletions

View File

@ -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
)

64
src/util/DateTime.cc Normal file
View File

@ -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 <cstdlib>
#include <iostream>
#include <time.h>
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

43
src/util/DateTime.hh Normal file
View File

@ -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 <ctime>
#include <string>
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

View File

@ -21,8 +21,14 @@
#include <string>
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

33
test/UnitTest.cc Normal file
View File

@ -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 <cppunit/ui/text/TestRunner.h>
#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 ;
}

40
test/util/DateTimeTest.cc Normal file
View File

@ -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

41
test/util/DateTimeTest.hh Normal file
View File

@ -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 <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
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