PSTime/test/testTimeUtils.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: testTimeUtils.cpp 4236 2012-07-24 22:16:07Z salnikov@SLAC.STANFORD.EDU $
00004 //
00005 // Description:
00006 //      Test suite case for the testTimeUtils.
00007 //
00008 //------------------------------------------------------------------------
00009 
00010 //---------------
00011 // C++ Headers --
00012 //---------------
00013 #include <iostream>
00014 
00015 //-------------------------------
00016 // Collaborating Class Headers --
00017 //-------------------------------
00018 #include "PSTime/TimeUtils.h"
00019 
00020 using namespace PSTime;
00021 using namespace std;
00022 
00023 #define BOOST_TEST_MODULE testTimeUtils
00024 #include <boost/test/included/unit_test.hpp>
00025 
00026 /**
00027  * Simple test suite for module testTimeUtils.
00028  * See http://www.boost.org/doc/libs/1_36_0/libs/test/doc/html/index.html
00029  */
00030 
00031 
00032 // Print (negative) time difference between UTC and local time
00033 int printTimeDiff(int year, int month, int day)
00034 {
00035   struct tm stm;
00036   stm.tm_year = year-1900;
00037   stm.tm_mon = month-1;
00038   stm.tm_mday = day;
00039   stm.tm_hour = 0;
00040   stm.tm_min = 0;
00041   stm.tm_sec = 0;
00042   stm.tm_isdst = -1;
00043   
00044   time_t tutc = TimeUtils::timegm(&stm);
00045   stm.tm_isdst = -1;
00046   time_t tloc = mktime(&stm);
00047   
00048   double diff = (tloc-tutc)/3600;
00049   
00050   cout << "\nTime: " << asctime(&stm);
00051   cout << "  UTC seconds  : " << tutc << endl;
00052   cout << "  Local seconds: " << tloc << endl;
00053   cout << "  Diff hours   : " << diff << endl;
00054   
00055   return int(diff);
00056 }
00057 
00058 // ==============================================================
00059 
00060 BOOST_AUTO_TEST_CASE( test_1 )
00061 {
00062   cout << "\n=== Non-leap years ===\n";
00063   
00064   int diff_1970_1_1 = printTimeDiff(1970, 1, 1);
00065   int diff_2001_1_1 = printTimeDiff(2001, 1, 1);
00066   int diff_2011_1_1 = printTimeDiff(2011, 1, 1);
00067   int diff_1970_6_1 = printTimeDiff(1970, 6, 1);
00068   int diff_2001_6_1 = printTimeDiff(2001, 6, 1);
00069   int diff_2011_6_1 = printTimeDiff(2011, 6, 1);
00070 
00071   BOOST_CHECK_EQUAL(diff_1970_1_1, diff_2001_1_1);
00072   BOOST_CHECK_EQUAL(diff_1970_1_1, diff_2011_1_1);
00073   BOOST_CHECK_EQUAL(diff_1970_1_1, diff_1970_6_1+1);
00074   BOOST_CHECK_EQUAL(diff_2001_1_1, diff_2001_6_1+1);
00075   BOOST_CHECK_EQUAL(diff_2001_1_1, diff_2011_6_1+1);
00076 }
00077 
00078 // ==============================================================
00079 
00080 BOOST_AUTO_TEST_CASE( test_2 )
00081 {
00082   cout << "\n=== Leap years ===\n";
00083   
00084   int diff_1972_1_1 = printTimeDiff(1972, 1, 1);
00085   int diff_1972_6_1 = printTimeDiff(1972, 6, 1);
00086   int diff_1980_1_1 = printTimeDiff(1980, 1, 1);
00087   int diff_1980_6_1 = printTimeDiff(1980, 6, 1);
00088 
00089   BOOST_CHECK_EQUAL(diff_1972_1_1, diff_1980_1_1);
00090   BOOST_CHECK_EQUAL(diff_1972_1_1, diff_1972_6_1+1);
00091   BOOST_CHECK_EQUAL(diff_1980_1_1, diff_1980_6_1+1);
00092 }
00093 
00094 // ==============================================================
00095 
00096 BOOST_AUTO_TEST_CASE( test_3 )
00097 {
00098   cout << "\n=== Leap (100) years ===\n";
00099   
00100   /*int diff_2100_1_1 =*/ printTimeDiff(2100, 1, 1);
00101   /*int diff_2100_6_1 =*/ printTimeDiff(2100, 6, 1);
00102 
00103   // timezone info does not have DST for 2100 yet, 
00104   // may be one of the two depending on platform
00105   //BOOST_CHECK_EQUAL(diff_2100_1_1, diff_2100_6_1+1);
00106   //BOOST_CHECK_EQUAL(diff_2100_1_1, diff_2100_6_1);
00107 }
00108 
00109 // ==============================================================
00110 
00111 BOOST_AUTO_TEST_CASE( test_4 )
00112 {
00113   cout << "\n=== Leap (400) years ===\n";
00114   
00115   int diff_2000_1_1 = printTimeDiff(2000, 1, 1);
00116   int diff_2000_6_1 = printTimeDiff(2000, 6, 1);
00117 
00118   BOOST_CHECK_EQUAL(diff_2000_1_1, diff_2000_6_1+1);
00119 }
00120 

Generated on 19 Dec 2016 for PSANAclasses by  doxygen 1.4.7