From dfd7eac5d9fc6d6865a23daa4278fac37f988b03 Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Fri, 27 Apr 2012 10:49:33 +0800 Subject: [PATCH 1/3] refactored files. moved around. added OS encapsulation layer. --- CMakeLists.txt | 12 ++++++++---- src/{ => drive}/Collection.cc | 8 +++----- src/{ => drive}/Collection.hh | 0 src/{ => drive}/Drive.cc | 2 +- src/{ => drive}/Drive.hh | 0 src/main.cc | 4 ++-- src/{ => protocol}/OAuth2.cc | 0 src/{ => protocol}/OAuth2.hh | 0 src/util/OS.cc | 33 +++++++++++++++++++++++++++++++++ src/util/OS.hh | 28 ++++++++++++++++++++++++++++ 10 files changed, 75 insertions(+), 12 deletions(-) rename src/{ => drive}/Collection.cc (96%) rename src/{ => drive}/Collection.hh (100%) rename src/{ => drive}/Drive.cc (99%) rename src/{ => drive}/Drive.hh (100%) rename src/{ => protocol}/OAuth2.cc (100%) rename src/{ => protocol}/OAuth2.hh (100%) create mode 100644 src/util/OS.cc create mode 100644 src/util/OS.hh diff --git a/CMakeLists.txt b/CMakeLists.txt index 167a7ac..3d94a95 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,14 +4,18 @@ cmake_minimum_required(VERSION 2.8) include(FindOpenSSL) +include_directories( ${grive_SOURCE_DIR}/src ) + add_executable( grive src/main.cc - src/OAuth2.cc - src/Drive.cc - src/Collection.cc + src/drive/Collection.cc + src/drive/Drive.cc + src/protocol/Download.cc src/protocol/HTTP.cc src/protocol/Json.cc - src/protocol/Download.cc ) + src/protocol/OAuth2.cc + src/util/OS.cc +) target_link_libraries( grive curl diff --git a/src/Collection.cc b/src/drive/Collection.cc similarity index 96% rename from src/Collection.cc rename to src/drive/Collection.cc index f660dfc..8fc5ce5 100644 --- a/src/Collection.cc +++ b/src/drive/Collection.cc @@ -20,13 +20,10 @@ #include "Collection.hh" #include "protocol/Json.hh" +#include "util/OS.hh" #include -// OS specific library -#include -#include - // for debugging #include @@ -104,7 +101,8 @@ void Collection::Swap( Collection& coll ) void Collection::CreateSubDir( const std::string& prefix ) { std::string dir = prefix + m_title ; - mkdir( dir.c_str(), 0700 ) ; +// mkdir( dir.c_str(), 0700 ) ; + os::MakeDir( dir ) ; for ( std::vector::iterator i = m_child.begin() ; i != m_child.end() ; ++i ) { diff --git a/src/Collection.hh b/src/drive/Collection.hh similarity index 100% rename from src/Collection.hh rename to src/drive/Collection.hh diff --git a/src/Drive.cc b/src/drive/Drive.cc similarity index 99% rename from src/Drive.cc rename to src/drive/Drive.cc index 60c0ca5..06267ca 100644 --- a/src/Drive.cc +++ b/src/drive/Drive.cc @@ -21,7 +21,7 @@ #include "protocol/HTTP.hh" #include "protocol/Json.hh" -#include "OAuth2.hh" +#include "protocol/OAuth2.hh" // dependent libraries #include diff --git a/src/Drive.hh b/src/drive/Drive.hh similarity index 100% rename from src/Drive.hh rename to src/drive/Drive.hh diff --git a/src/main.cc b/src/main.cc index 4df7d9e..1d1c123 100644 --- a/src/main.cc +++ b/src/main.cc @@ -17,8 +17,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "OAuth2.hh" -#include "Drive.hh" +#include "drive/Drive.hh" +#include "protocol/OAuth2.hh" #include "protocol/Json.hh" #include diff --git a/src/OAuth2.cc b/src/protocol/OAuth2.cc similarity index 100% rename from src/OAuth2.cc rename to src/protocol/OAuth2.cc diff --git a/src/OAuth2.hh b/src/protocol/OAuth2.hh similarity index 100% rename from src/OAuth2.hh rename to src/protocol/OAuth2.hh diff --git a/src/util/OS.cc b/src/util/OS.cc new file mode 100644 index 0000000..a69dd2f --- /dev/null +++ b/src/util/OS.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 "OS.hh" + +// OS specific headers +#include +#include + +namespace gr { namespace os { + +void MakeDir( const std::string& dir ) +{ + mkdir( dir.c_str(), 0700 ) ; +} + +} } // end of namespaces diff --git a/src/util/OS.hh b/src/util/OS.hh new file mode 100644 index 0000000..59d542e --- /dev/null +++ b/src/util/OS.hh @@ -0,0 +1,28 @@ +/* + 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 + +namespace gr { namespace os { + +void MakeDir( const std::string& dir ) ; + +} } // end of namespaces From f0ca94d7e03bfc0198ca53f7e1712d4a4342deef Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Fri, 27 Apr 2012 15:41:46 +0800 Subject: [PATCH 2/3] added date time class and associate unit test case --- CMakeLists.txt | 18 +++++++++-- src/util/DateTime.cc | 64 +++++++++++++++++++++++++++++++++++++++ src/util/DateTime.hh | 43 ++++++++++++++++++++++++++ src/util/OS.hh | 12 ++++++-- test/UnitTest.cc | 33 ++++++++++++++++++++ test/util/DateTimeTest.cc | 40 ++++++++++++++++++++++++ test/util/DateTimeTest.hh | 41 +++++++++++++++++++++++++ 7 files changed, 246 insertions(+), 5 deletions(-) create mode 100644 src/util/DateTime.cc create mode 100644 src/util/DateTime.hh create mode 100644 test/UnitTest.cc create mode 100644 test/util/DateTimeTest.cc create mode 100644 test/util/DateTimeTest.hh 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 From ceca83dc72f9b6d6e8647c707d1602ff1f99e7e9 Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Sat, 28 Apr 2012 01:52:02 +0800 Subject: [PATCH 3/3] set the mtime of the local file to be the same as the remote --- CMakeLists.txt | 2 +- src/drive/Drive.cc | 13 ++++++++-- src/protocol/Json.cc | 6 ++++- src/protocol/Json.hh | 8 ++++++ src/util/DateTime.cc | 51 +++++++++++++++++++++++++++++++-------- src/util/DateTime.hh | 9 +++++-- src/util/OS.cc | 20 +++++++++++++++ src/util/OS.hh | 3 ++- test/util/DateTimeTest.cc | 23 ++++++++++++++++++ test/util/DateTimeTest.hh | 6 +++++ 10 files changed, 124 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d9f5963..e345418 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ add_executable( grive src/protocol/HTTP.cc src/protocol/Json.cc src/protocol/OAuth2.cc - src/util/DateTime.hh + src/util/DateTime.cc src/util/OS.cc ) diff --git a/src/drive/Drive.cc b/src/drive/Drive.cc index 06267ca..91641c1 100644 --- a/src/drive/Drive.cc +++ b/src/drive/Drive.cc @@ -22,6 +22,8 @@ #include "protocol/HTTP.hh" #include "protocol/Json.hh" #include "protocol/OAuth2.hh" +#include "util/DateTime.hh" +#include "util/OS.hh" // dependent libraries #include @@ -180,10 +182,12 @@ void Drive::UpdateFile( const Json& entry ) if ( entry.Has( "docs$filename" ) ) { // use title as the filename - std::string filename = entry["docs$filename"]["$t"].As() ; - std::string url = entry["content"]["src"].As() ; + std::string filename = entry["docs$filename"]["$t"].As() ; + std::string url = entry["content"]["src"].As() ; std::string parent_href = Parent( entry ) ; + DateTime remote( entry["updated"]["$t"].As() ) ; + bool changed = true ; std::string path = "./" + filename ; @@ -194,11 +198,16 @@ void Drive::UpdateFile( const Json& entry ) if ( pit != m_coll.end() ) path = pit->Path() + "/" + filename ; } + DateTime local = os::FileMTime( path ) ; + + std::cout << "file time: " << entry["updated"]["$t"].As() << " " << remote << " " << local << std::endl ; // compare checksum first if file exists std::ifstream ifile( path.c_str(), std::ios::binary | std::ios::out ) ; if ( ifile && entry.Has("docs$md5Checksum") ) { + os::SetFileTime( path, remote ) ; + std::string remote_md5 = entry["docs$md5Checksum"]["$t"].As() ; std::string local_md5 = MD5( ifile.rdbuf() ) ; diff --git a/src/protocol/Json.cc b/src/protocol/Json.cc index 381cfe8..0fcc12a 100644 --- a/src/protocol/Json.cc +++ b/src/protocol/Json.cc @@ -138,6 +138,11 @@ void Json::Add( const std::string& key, const Json& json ) ::json_object_object_add( m_json, key.c_str(), json.m_json ) ; } +Json::Proxy Json::As() const +{ + return Proxy( *this ) ; +} + template <> std::string Json::As() const { @@ -243,5 +248,4 @@ bool Json::FindInArray( const std::string& key, const std::string& value, Json& } } - } diff --git a/src/protocol/Json.hh b/src/protocol/Json.hh index c977d0e..5417526 100644 --- a/src/protocol/Json.hh +++ b/src/protocol/Json.hh @@ -52,6 +52,14 @@ public : template T As() const ; + struct Proxy + { + const Json& referring ; + explicit Proxy( const Json& j ) : referring( j ) { } + template operator T() const { return referring.As() ; } + } ; + Proxy As() const ; + template bool Is() const ; diff --git a/src/util/DateTime.cc b/src/util/DateTime.cc index b396885..b32111b 100644 --- a/src/util/DateTime.cc +++ b/src/util/DateTime.cc @@ -20,7 +20,9 @@ #include "DateTime.hh" #include +#include #include +#include #include @@ -28,20 +30,32 @@ namespace gr { DateTime::DateTime( ) : m_sec ( 0 ), - m_usec ( 0 ) + m_nsec ( 0 ) { } DateTime::DateTime( const std::string& iso ) : m_sec ( 0 ), - m_usec ( 0 ) + m_nsec ( 0 ) +{ + struct tm tp = {} ; + const char *r = ::strptime( iso.c_str(), "%Y-%m-%dT%H:%M:%S", &tp ) ; + + // should be '.' followed by 3 digits and 'Z' (e.g. .123Z) + if ( r != 0 && r - iso.c_str() == 19 ) + { + m_sec = ::timegm( &tp ) ; + + // at least 3 digits is OK. we don't care the Z + if ( *r == '.' && ::strlen( r+1 ) >= 3 ) + m_nsec = std::atoi( r+1 ) * 1000 * 1000 ; + } +} + +DateTime::DateTime( std::time_t sec, unsigned long nsec ) : + m_sec ( sec ), + m_nsec ( nsec ) { - 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 @@ -56,9 +70,26 @@ std::time_t DateTime::Sec( ) const return m_sec ; } -unsigned long DateTime::USec( ) const +unsigned long DateTime::NanoSec( ) const { - return m_usec ; + return m_nsec ; +} + +std::ostream& operator<<( std::ostream& os, const DateTime& dt ) +{ + struct tm tp = dt.Tm() ; + + char buf[40] ; + strftime( buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &tp ) ; + return os << buf << '.' << std::setw( 3 ) << std::setfill('0') << dt.NanoSec()/1000000 << 'Z' ; +} + +struct timeval DateTime::Tv() const +{ + timeval result ; + result.tv_sec = m_sec ; + result.tv_usec = m_nsec / 1000 ; + return result ; } } // end of namespace diff --git a/src/util/DateTime.hh b/src/util/DateTime.hh index 97a1764..a2e1ae4 100644 --- a/src/util/DateTime.hh +++ b/src/util/DateTime.hh @@ -21,6 +21,7 @@ #include #include +#include namespace gr { @@ -29,15 +30,19 @@ class DateTime public : DateTime( ) ; explicit DateTime( const std::string& iso ) ; + explicit DateTime( std::time_t sec, unsigned long nsec = 0 ) ; std::time_t Sec( ) const ; - unsigned long USec( ) const ; + unsigned long NanoSec( ) const ; struct tm Tm() const ; + struct timeval Tv() const ; private : std::time_t m_sec ; - unsigned long m_usec ; + unsigned long m_nsec ; } ; +std::ostream& operator<<( std::ostream& os, const DateTime& dt ) ; + } // end of namespace diff --git a/src/util/OS.cc b/src/util/OS.cc index a69dd2f..a70ce0a 100644 --- a/src/util/OS.cc +++ b/src/util/OS.cc @@ -18,9 +18,13 @@ */ #include "OS.hh" +#include "DateTime.hh" + +#include // OS specific headers #include +#include #include namespace gr { namespace os { @@ -30,4 +34,20 @@ void MakeDir( const std::string& dir ) mkdir( dir.c_str(), 0700 ) ; } +DateTime FileMTime( const std::string& filename ) +{ + struct stat s = {} ; + if ( ::stat( filename.c_str(), &s ) != 0 ) + throw std::runtime_error( "cannot get file attribute of " + filename ) ; + + return DateTime( s.st_mtim.tv_sec, s.st_mtim.tv_nsec ) ; +} + +void SetFileTime( const std::string& filename, const DateTime& t ) +{ + struct timeval tvp[2] = { t.Tv(), t.Tv() } ; + if ( ::utimes( filename.c_str(), tvp ) != 0 ) + throw std::runtime_error( "cannot set file time" ) ; +} + } } // end of namespaces diff --git a/src/util/OS.hh b/src/util/OS.hh index a108357..2f52e98 100644 --- a/src/util/OS.hh +++ b/src/util/OS.hh @@ -28,7 +28,8 @@ class DateTime ; namespace os { void MakeDir( const std::string& dir ) ; - DateTime FileMTime( const std::string& file ) ; + DateTime FileMTime( const std::string& filename ) ; + void SetFileTime( const std::string& filename, const DateTime& t ) ; } } // end of namespaces diff --git a/test/util/DateTimeTest.cc b/test/util/DateTimeTest.cc index 88c4820..6c803f4 100644 --- a/test/util/DateTimeTest.cc +++ b/test/util/DateTimeTest.cc @@ -21,6 +21,8 @@ #include "util/DateTime.hh" +#include + namespace grut { using namespace gr ; @@ -35,6 +37,27 @@ void DateTimeTest::TestParseIso( ) struct tm tp = subject.Tm() ; CPPUNIT_ASSERT( tp.tm_year == 109 ) ; CPPUNIT_ASSERT( tp.tm_sec == 39 ) ; + CPPUNIT_ASSERT_EQUAL( 804000000UL, subject.NanoSec() ) ; +} + +void DateTimeTest::TestParseNoMillisec( ) +{ + DateTime subject( "2009-07-29T20:31:39Z" ) ; + CPPUNIT_ASSERT_EQUAL( 0UL, subject.NanoSec() ) ; +} + +void DateTimeTest::TestParseInvalid( ) +{ + DateTime subject( "abcdefg" ) ; + CPPUNIT_ASSERT_EQUAL( static_cast(0), subject.Sec() ) ; + CPPUNIT_ASSERT_EQUAL( 0UL, subject.NanoSec() ) ; +} + +void DateTimeTest::TestOffByOne( ) +{ + DateTime subject( "2008-12-21T02:48:53.940Z" ) ; + struct tm tp = subject.Tm() ; + CPPUNIT_ASSERT_EQUAL( 21, tp.tm_mday ) ; } } // end of namespace grut diff --git a/test/util/DateTimeTest.hh b/test/util/DateTimeTest.hh index 79cf9af..1246a33 100644 --- a/test/util/DateTimeTest.hh +++ b/test/util/DateTimeTest.hh @@ -32,10 +32,16 @@ public : // declare suit function CPPUNIT_TEST_SUITE( DateTimeTest ) ; CPPUNIT_TEST( TestParseIso ) ; + CPPUNIT_TEST( TestParseNoMillisec ) ; + CPPUNIT_TEST( TestOffByOne ) ; + CPPUNIT_TEST( TestParseInvalid ) ; CPPUNIT_TEST_SUITE_END(); private : void TestParseIso( ) ; + void TestParseNoMillisec( ) ; + void TestOffByOne( ) ; + void TestParseInvalid( ) ; } ; } // end of namespace