"Fossies" - the Fresh Open Source Software Archive

Member "fimex-1.4.1/test/testTimeSpec.cc" (30 Oct 2019, 4428 Bytes) of package /linux/privat/fimex-1.4.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * Fimex, testTimeSpec.cc
    3  *
    4  * (C) Copyright 2009, met.no
    5  *
    6  * Project Info:  https://wiki.met.no/fimex/start
    7  *
    8  * This library is free software; you can redistribute it and/or modify it
    9  * under the terms of the GNU Lesser General Public License as published by
   10  * the Free Software Foundation; either version 2.1 of the License, or
   11  * (at your option) any later version.
   12  *
   13  * This library is distributed in the hope that it will be useful, but
   14  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   15  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
   16  * License for more details.
   17  *
   18  * You should have received a copy of the GNU Lesser General Public
   19  * License along with this library; if not, write to the Free Software
   20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
   21  * USA.
   22  *
   23  *  Created on: Mar 18, 2009
   24  *      Author: Heiko Klein
   25  */
   26 
   27 #include "testinghelpers.h"
   28 
   29 #include "fimex/Logger.h"
   30 #include "fimex/TimeSpec.h"
   31 #include "fimex/TimeUtils.h"
   32 
   33 using namespace std;
   34 using namespace MetNoFimex;
   35 //#define TEST_DEBUG
   36 
   37 TEST4FIMEX_TEST_CASE(test_TimeSpec)
   38 {
   39 #ifdef TEST_DEBUG
   40     defaultLogLevel(Logger::DEBUG);
   41 #endif
   42     string tspec("2000-01-01 00:00:00,2000-01-01 04:00:00,...,2000-01-02 08:00:00");
   43     FimexTime start;
   44     FimexTime end;
   45     TimeSpec ts(tspec, start, end); // start and end don't play a role for absolute times
   46     const vector<FimexTime>& fTimes = ts.getTimeSteps();
   47         TEST4FIMEX_REQUIRE_EQ(fTimes.size(), 9);
   48         TEST4FIMEX_CHECK_EQ(fTimes[2].getHour(), 8);
   49         TEST4FIMEX_CHECK_EQ(fTimes[8].getHour(), 8);
   50         TEST4FIMEX_CHECK_EQ(fTimes[7].getMDay(), 2);
   51 
   52         start.setYear(2001);
   53     start.setMonth(1);
   54     start.setMDay(2);
   55     start.setHour(0);
   56     start.setMinute(0);
   57     start.setSecond(0);
   58     start.setMSecond(0);
   59     end = start;
   60     end.setMDay(3);
   61     end.setHour(8);
   62 
   63     string tspec2("0,3,...,x,x+3;relativeUnit=hours since 2000-01-01 00:00:00");
   64     TimeSpec ts2(tspec2, start, end);
   65     const vector<FimexTime>& fTimes2 = ts2.getTimeSteps();
   66         TEST4FIMEX_REQUIRE_EQ(fTimes2.size(), 12);
   67         TEST4FIMEX_CHECK_EQ(fTimes2[0].getHour(), 0);
   68         TEST4FIMEX_CHECK_EQ(fTimes2[0].getYear(), 2001);
   69         TEST4FIMEX_CHECK_EQ(fTimes2[10].getMDay(), 3);
   70         TEST4FIMEX_CHECK_EQ(fTimes2[10].getHour(), 6);
   71         TEST4FIMEX_CHECK_EQ(fTimes2[11].getMDay(), 3);
   72         TEST4FIMEX_CHECK_EQ(fTimes2[11].getHour(), 9);
   73 }
   74 
   75 TEST4FIMEX_TEST_CASE(test_time_point)
   76 {
   77     {
   78         const time_point tp = make_time_point(1970, 1, 1, 2, 0, 0);
   79         TEST4FIMEX_CHECK_EQ("1970-01-01T02:00:00", make_time_string_extended(tp));
   80         TEST4FIMEX_CHECK(!is_invalid_time_point(tp));
   81 
   82         const time_point tp6 = tp - std::chrono::hours(6);
   83         TEST4FIMEX_CHECK_EQ("19691231T200000", make_time_string(tp6));
   84     }
   85     {
   86         const time_point tp = make_time_point(1820, 2, 28, 12, 13, 14);
   87         TEST4FIMEX_CHECK_EQ("1820-02-28T12:13:14", make_time_string_extended(tp));
   88     }
   89     {
   90         const time_point tp = make_time_point(1970, 1, 1, 0, 0, 0);
   91         TEST4FIMEX_CHECK_EQ(0, std::chrono::system_clock::to_time_t(tp));
   92     }
   93 }
   94 
   95 TEST4FIMEX_TEST_CASE(test_time_point_from_iso9601)
   96 {
   97     const time_point epoch = make_time_point(1970, 1, 1, 0, 0, 0);
   98     TEST4FIMEX_CHECK(epoch == make_time_from_string("1970-01-01 00:00:00"));
   99     TEST4FIMEX_CHECK(epoch == make_time_from_string("1970-01-01T00:00:00"));
  100 }
  101 
  102 TEST4FIMEX_TEST_CASE(fimextime_from_iso8601)
  103 {
  104     FimexTime ft;
  105     TEST4FIMEX_CHECK(ft.parseISO8601("2019-01-02 03:04:05.006"));
  106     TEST4FIMEX_CHECK_EQ(FimexTime(2019, 1, 2, 3, 4, 5, 6), ft);
  107 
  108     TEST4FIMEX_CHECK(ft.parseISO8601("2019-01-02 03:04:05"));
  109     TEST4FIMEX_CHECK_EQ(FimexTime(2019, 1, 2, 3, 4, 5, 0), ft);
  110 
  111     TEST4FIMEX_CHECK(ft.parseISO8601("2019-01-02 03:04"));
  112     TEST4FIMEX_CHECK_EQ(FimexTime(2019, 1, 2, 3, 4, 0, 0), ft);
  113 
  114     TEST4FIMEX_CHECK(ft.parseISO8601("2019-02-03 04:08:16"));
  115     TEST4FIMEX_CHECK_EQ(FimexTime(2019, 2, 3, 4, 8, 16), ft);
  116 
  117     TEST4FIMEX_CHECK(ft.parseISO8601("2019-01-02"));
  118     TEST4FIMEX_CHECK_EQ(FimexTime(2019, 1, 2, 0, 0, 0), ft);
  119 }
  120 
  121 TEST4FIMEX_TEST_CASE(fimextime_timepoint_conversion)
  122 {
  123     const time_point tp1 = make_time_point(1820, 2, 28, 12, 13, 14);
  124     const FimexTime ft1 = fromTimePoint(tp1);
  125     TEST4FIMEX_CHECK_EQ(FimexTime(1820, 2, 28, 12, 13, 14), ft1);
  126 
  127     const time_point tp1f = asTimePoint(ft1);
  128     TEST4FIMEX_CHECK(tp1f == tp1);
  129 }