You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

512 lines
15 KiB
C++

/***************************************************************************
* Copyright (C) 2018 by gempa GmbH *
* *
* All Rights Reserved. *
* *
* NOTICE: All information contained herein is, and remains *
* the property of gempa GmbH and its suppliers, if any. The intellectual *
* and technical concepts contained herein are proprietary to gempa GmbH *
* and its suppliers. *
* Dissemination of this information or reproduction of this material *
* is strictly forbidden unless prior written permission is obtained *
* from gempa GmbH. *
* *
* Author: Tracey Werner, Enrico Ellguth *
* Email: tracey.werner@gempa.de, enrico.ellguth@gempa.de *
***************************************************************************/
#define SEISCOMP_TEST_MODULE gempa
#include <seiscomp3/unittest/unittests.h>
#include <gempa/caps/datetime.h>
namespace gc = Gempa::CAPS;
namespace bu = boost::unit_test;
BOOST_AUTO_TEST_SUITE(gempa_common_caps_datetime_time)
bool isClose(gc::TimeSpan time, long sec, long micro, int offset = 1) {
long microSeconds = time.microseconds();
long secDiff = time.seconds() - sec;
if ( secDiff > 0 )
microSeconds += secDiff * 1000000;
else if ( secDiff < 0 )
micro += abs(secDiff) * 1000000;
if ( abs(microSeconds - micro) <= offset )
return true;
return false;
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_AUTO_TEST_CASE(construction) {
bu::unit_test_log.set_threshold_level(bu::log_warnings);
bu::unit_test_log.set_threshold_level(bu::log_messages);
gc::Time time;
BOOST_CHECK(time == gc::Time(0.0));
// long
gc::Time tPositive(200, 600);
BOOST_CHECK(tPositive == gc::Time(200.000600));
gc::Time tNegativeUsec(3000, -789);
BOOST_WARN_EQUAL(tNegativeUsec.seconds(), 3000);
BOOST_WARN_EQUAL(tNegativeUsec.microseconds(), -789);
gc::Time tNegativeSec(-12, 12345);
BOOST_WARN_EQUAL(tNegativeSec.seconds(), -12);
BOOST_WARN_EQUAL(tNegativeSec.microseconds(), 12345);
gc::Time tNegative(-15,-9876);
BOOST_WARN_EQUAL(tNegative.seconds(), -15);
BOOST_WARN_EQUAL(tNegative.microseconds(), -9876);
// TimeSpan
gc::Time tsPositive(gc::TimeSpan(5.345));
BOOST_WARN_EQUAL(tsPositive.seconds(), 5);
BOOST_WARN_EQUAL(tsPositive.microseconds(), 345000);
// timeval
timeval number;
number.tv_sec = 150;
number.tv_usec = 6000;
gc::Time tvPositive(number);
BOOST_WARN_EQUAL(tvPositive.seconds(), 150);
BOOST_WARN_EQUAL(tvPositive.microseconds(), 6000);
number.tv_sec = -150;
number.tv_usec = 9000;
gc::Time tvNegativeSec(number);
BOOST_WARN_EQUAL(tvNegativeSec.seconds(), -150);
BOOST_WARN_EQUAL(tvNegativeSec.microseconds(),9000);
number.tv_sec = 4000;
number.tv_usec = -98876;
gc::Time tvNegativeUsec(number);
BOOST_WARN_EQUAL(tvNegativeUsec.seconds(), 4000);
BOOST_WARN_EQUAL(tvNegativeUsec.microseconds(), -98876);
number.tv_sec = -9877;
number.tv_usec = -874547;
gc::Time tvNegative(number);
BOOST_WARN_EQUAL(tvNegative.seconds(), -9877);
BOOST_WARN_EQUAL(tvNegative.microseconds(), -874547);
// double
double val = 5678.9864;
gc::Time tdPositive(val);
BOOST_WARN_EQUAL(tdPositive.seconds(), 5678);
BOOST_CHECK_EQUAL(tdPositive.microseconds(), 986400);
val = -89765.745377;
gc::Time tdNegative(val);
BOOST_WARN_EQUAL(isClose(tdNegative, -89765, -745377), true);
// pointer
timeval pointer;
pointer.tv_sec = 76656;
pointer.tv_usec = 8900;
gc::Time tpPositive(&pointer);
BOOST_WARN_EQUAL(tpPositive.seconds(), 76656);
BOOST_WARN_EQUAL(tpPositive.microseconds(), 8900);
pointer.tv_sec = -76656;
pointer.tv_usec = 8900;
gc::Time tpNegativeSec(&pointer);
BOOST_WARN_EQUAL(tpNegativeSec.seconds(), -76656);
BOOST_WARN_EQUAL(tpNegativeSec.microseconds(), 8900);
pointer.tv_sec = 98744;
pointer.tv_usec = -8965;
gc::Time tpNegativeUsec(&pointer);
BOOST_WARN_EQUAL(tpNegativeUsec.seconds(), 98744);
BOOST_WARN_EQUAL(tpNegativeUsec.microseconds(), -8965);
pointer.tv_sec = -44;
pointer.tv_usec = -895;
gc::Time tpNegative(&pointer);
BOOST_WARN_EQUAL(tpNegative.seconds(), -44);
BOOST_WARN_EQUAL(tpNegative.microseconds(), -895);
// copy
gc::Time copyPositive(gc::Time(758.9975));
BOOST_CHECK_EQUAL(copyPositive.seconds(), 758);
BOOST_CHECK_EQUAL(copyPositive.microseconds(), 997500);
gc::Time copyNegative(gc::Time(-877.963));
BOOST_WARN_EQUAL(isClose(copyNegative, -877, -963000), true);
// date
gc::Time date(1971,1,3,1,1,4,6544);
double dayInSeconds = 86400;
double yearInSeconds = 31536000;
BOOST_WARN_CLOSE(double(date), dayInSeconds*2 + yearInSeconds,0.3);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_AUTO_TEST_CASE(addition) {
gc::Time a(7,5);
gc::TimeSpan b = 9.000004;
gc::TimeSpan result = a + b;
BOOST_CHECK_EQUAL(result.microseconds(), 9);
BOOST_CHECK_EQUAL(result.seconds(), 16);
gc::Time c(7,5);
gc::TimeSpan d = -3.000004;
result = c + d;
BOOST_CHECK_EQUAL(result.microseconds(), 2);
BOOST_CHECK_EQUAL(result.seconds(), 4);
gc::Time e(-7,5);
gc::TimeSpan f = 9.000004;
result = e + f;
BOOST_CHECK_EQUAL(result.microseconds(),9);
BOOST_CHECK_EQUAL(result.seconds(), 2);
gc::Time g(900,789);
gc::TimeSpan h;
result = h += g;
BOOST_CHECK_EQUAL(result.microseconds(),789);
BOOST_CHECK_EQUAL(result.seconds(), 900);
gc::Time i(455, -355);
gc::TimeSpan j = 80.000444;
i += j;
BOOST_CHECK_EQUAL(i.microseconds(),89);
BOOST_CHECK_EQUAL(i.seconds(), 535);
gc::Time k(-899, 22255);
gc::TimeSpan l = 773.992;
l += k;
BOOST_WARN_EQUAL(l.seconds(), -125);
BOOST_WARN_EQUAL(l.microseconds(), 14255);
gc::Time m(500, 987);
gc::TimeSpan n(-30, 876);
int result2 = m.microseconds() + n.microseconds();
int result3 = m.seconds() + n.seconds();
m += n;
BOOST_WARN_EQUAL(m.microseconds(),result2);
BOOST_WARN_EQUAL(m.seconds(),result3);
gc::Time o(-60, 47);
gc::TimeSpan p(-44,5);
long sec = o.seconds() + p.seconds();
long micro = o.microseconds() + p.microseconds();
o += p;
BOOST_CHECK_EQUAL(isClose(o, sec, micro), true);
gc::Time q(9876, -6748);
gc::TimeSpan r = -876.987;
q += r;
BOOST_WARN_EQUAL(q.microseconds(), 6253);
BOOST_CHECK_EQUAL(q.seconds(),8999);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_AUTO_TEST_CASE(subtraction) {
gc::Time a(7,5);
gc::TimeSpan b(9,000004);
gc::TimeSpan result = a - b;
long sec = a.seconds() - b.seconds();
long micro = a.microseconds() - b.microseconds();
BOOST_WARN_EQUAL(isClose(result, sec, micro),true);
gc::Time c(7,5);
gc::TimeSpan d = -3.000004;
result = c - d;
BOOST_CHECK_EQUAL(result.microseconds(), 8);
BOOST_CHECK_EQUAL(result.seconds(), 10);
gc::Time e(-7,5);
gc::TimeSpan f(9,000004);
result = e - f;
sec = e.seconds() - f.seconds();
micro = e.microseconds() -f.microseconds();
BOOST_WARN_EQUAL(isClose(result, sec, micro),true);
gc::Time g(900,789);
gc::TimeSpan h;
sec = h.seconds() - g.seconds();
micro = h.microseconds() - g.microseconds();
h -= g;
BOOST_CHECK_EQUAL(isClose(h, sec, micro), true);
gc::Time i(455, -355);
gc::TimeSpan j(80, 444);
sec = i.seconds() - j.seconds();
micro = i.microseconds() - j.microseconds();
i -= j;
BOOST_CHECK_EQUAL(isClose(i, sec, micro), true);
gc::Time k(-899, 22255);
gc::TimeSpan l(773, 992);
sec = l.seconds() - k.seconds();
micro = l.microseconds() - k.microseconds();
l -= k;
BOOST_CHECK_EQUAL(isClose(l, sec, micro), true);
gc::Time m(500,987);
gc::TimeSpan n = -30.876;
m -= n;
BOOST_CHECK_EQUAL(m.microseconds(),876986);
BOOST_CHECK_EQUAL(m.seconds(), 530);
gc::Time o(-60, 47);
gc::TimeSpan p = -44.05;
sec = o.seconds() - p.seconds();
micro = o.microseconds() - p.microseconds();
o -= p;
BOOST_CHECK_EQUAL(isClose(o, sec, micro), true);
gc::Time q(9876, -6748);
gc::TimeSpan r = -876.987;
sec = q.seconds() -r.seconds();
micro = q.microseconds() - r.microseconds();
q -= r;
BOOST_CHECK_EQUAL(isClose(q, sec, micro), true);
gc::Time s(50, 778), t(4, 221);
result = s - t;
BOOST_CHECK_EQUAL(result.microseconds(), 557);
BOOST_CHECK_EQUAL(result.seconds(), 46);
gc::Time u(-30,0),v(60,66);
result = u - v;
sec = u.seconds() -v.seconds();
micro = u.microseconds() - v.microseconds();
BOOST_CHECK_EQUAL(isClose(result, sec, micro), true);
gc::Time w(798, -444),x(6, 0321);
sec = w.seconds() - x.seconds();
micro = w.microseconds() - x.microseconds();
result = w - x;
BOOST_CHECK_EQUAL(isClose(result, sec, micro), true);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_AUTO_TEST_CASE(setAndGet) {
gc::Time date;
gc::TimeSpan oneDay (86400); // one day in seconds
gc::TimeSpan oneYear (31536000); // one year in seconds
gc::TimeSpan toNextYear (26524800); // seconds to the next year
int year = 1970, month = 8, day = 5,h = 7,min = 50,sec = 33,uSec= 80;
date.set(year,month,day,h,min,sec,uSec);
BOOST_CHECK(date.get(&year,&month,&day,&h,&min,&sec,&uSec) == true);
BOOST_CHECK(year == 1970);
BOOST_CHECK(month == 8);
BOOST_CHECK(day == 5);
BOOST_CHECK(h == 7);
BOOST_CHECK(min = 50);
BOOST_CHECK(sec = 33);
BOOST_CHECK(uSec = 80);
date -= oneYear;
BOOST_CHECK(date.get(&year,&month,&day,&h,&min,&sec,&uSec) == true);
BOOST_CHECK(year == 1969);
BOOST_CHECK(month == 8);
BOOST_CHECK_EQUAL(day , 5);
year = 2017, month = 2, day = 28;
date.set(year,month,day,h,min,sec,uSec);
BOOST_CHECK(date.get(&year,&month,&day,&h,&min,&sec,&uSec) == true);
BOOST_CHECK(year == 2017);
BOOST_CHECK(month == 2);
BOOST_CHECK(day == 28);
date += oneDay;
BOOST_CHECK(date.get(&year,&month,&day,&h,&min,&sec,&uSec) == true);
BOOST_CHECK_EQUAL(month , 3);
BOOST_CHECK_EQUAL(day , 1);
year = 2018, month = 2, day = 28;
date.set(year,month,day,h,min,sec,uSec);
date += oneDay;
BOOST_CHECK(date.get(&year,&month,&day,&h,&min,&sec,&uSec) == true);
BOOST_CHECK_EQUAL(month , 3);
BOOST_CHECK_EQUAL(day, 1);
date += oneYear;
BOOST_CHECK(date.get(&year,&month,&day,&h,&min,&sec,&uSec) == true);
BOOST_CHECK_EQUAL(year , 2019);
BOOST_CHECK_EQUAL(day, 1);
BOOST_CHECK_EQUAL(month , 3);
gc::Time leapYear;
year = 1956, month = 2, day = 28;
leapYear.set(year,month,day,h,min,sec,uSec);
BOOST_CHECK(leapYear.get(&year,&month,&day,&h,&min,&sec,&uSec) == true);
BOOST_CHECK(year == 1956);
BOOST_CHECK(month == 2);
BOOST_CHECK(day == 28);
leapYear += oneDay;
BOOST_CHECK(leapYear.get(&year,&month,&day,&h,&min,&sec,&uSec) == true);
BOOST_CHECK(month == 2);
BOOST_CHECK(day == 29);
leapYear += oneDay;
BOOST_CHECK(leapYear.get(&year,&month,&day,&h,&min,&sec,&uSec) == true);
BOOST_CHECK(month == 3);
BOOST_CHECK(day == 1);
gc::Time time;
year = 2011, month = 2, day = 28;
int yday ;
time.set(year,month,day,h,min,sec,uSec);
BOOST_CHECK(time.get2(&year,&yday,&h,&min,&sec,&uSec) == true);
BOOST_CHECK(year == 2011);
BOOST_CHECK_EQUAL(yday , 58);
time += toNextYear;
BOOST_CHECK(time.get2(&year,&yday,&h,&min,&sec,&uSec) == true);
BOOST_CHECK_EQUAL(year , 2012);
BOOST_CHECK_EQUAL(yday , 0);
year = 1964, month = 2, day = 29;
leapYear.set(year,month,day,h,min,sec,uSec);
BOOST_CHECK(leapYear.get2(&year,&yday,&h,&min,&sec,&uSec) == true);
BOOST_CHECK_EQUAL(yday , 59);
leapYear += toNextYear;
BOOST_CHECK(leapYear.get2(&year,&yday,&h,&min,&sec,&uSec) == true);
BOOST_CHECK_EQUAL(year, 1965);
BOOST_CHECK_EQUAL(yday , 0);
gc::Time before1900;
day = 28, month = 2, year = 1900;
before1900.set(year,month,day,h,min,sec,uSec);
BOOST_CHECK(before1900.get(&year,&month,&day,&h,&min,&sec,&uSec) == true);
BOOST_CHECK_EQUAL(year , 1900);
BOOST_CHECK_EQUAL(day , 28);
BOOST_CHECK_EQUAL(month, 2);
gc::Time pure;
pure.get(&year,&month,&day,&h,&min,&sec,&uSec);
BOOST_CHECK_EQUAL(year, 1970);
pure -= oneYear;
pure.get(&year,&month,&day,&h,&min,&sec,&uSec);
BOOST_CHECK_EQUAL(year, 1969);
day = 50, month = 4, year = 1566;
before1900.set(year,month,day,h,min,sec,uSec);
BOOST_CHECK(before1900.get(&year,&month,&day,&h,&min,&sec,&uSec) == true);
BOOST_CHECK_EQUAL(year , 1566);
BOOST_CHECK_EQUAL(day , 20);
BOOST_CHECK_EQUAL(month, 5);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_AUTO_TEST_CASE(localTime) {
gc::Time local;
local.set(1970,3,14,5,30,3,39);
gc::Time time(local);
BOOST_CHECK_EQUAL(double(local), double(time));
std::string check1 = local.toString("%FT%T.%fZ");
std::string check2 = "1970-03-14T05:30:03.000039Z";
bool equal = boost::iequals(check1,check2);
BOOST_CHECK_EQUAL(equal, true);
gc::Time localtest = local.LocalTime();
local = local.LocalTime();
localtest.setUSecs(0);
local.setUSecs(0);
check1 = local.iso();
check2 = localtest.iso();
BOOST_CHECK_EQUAL(check1, check2);
local.set(1970,3,14,5,30,3,39);
check1 = "1970-03-14T05:30:03.000039Z";
check2 = local.toString("%FT%T.%fZ");
BOOST_CHECK_EQUAL(check1, check2);
local.set(1981,9,14,5,30,3,39);
check1 = "1981-09-14T05:30:03.000039Z";
check2 = local.toString("%FT%T.%fZ");
BOOST_CHECK_EQUAL(check1, check2);
local.set(2014,3,14,5,30,3,39);
check1 = "2014-03-14T05:30:03.000039Z";
check2 = local.toString("%FT%T.%fZ");
BOOST_CHECK_EQUAL(check1, check2);
local.set(2000,8,14,5,30,3,39);
check1 = local.toString("%FT%T.%fZ");
check2 = "2000-08-14T05:30:03.000039Z";
BOOST_CHECK_EQUAL(check1, check2);
// before 1970
gc::Time before1970;
before1970.set(1950,6,4,15,8,66,11);
gc::Time t(before1970);
gc::Time time1 = local.LocalTime();
time1.setUSecs(0);
gc::Time time2 = before1970.LocalTime();
time2.setUSecs(0);
check1 = time1.toString("%FT%T.%fZ");
check2 = time2.toString("%FT%T.%fZ");
BOOST_CHECK_EQUAL(check1, check2);
before1970.set(1914,9,4,7,8,66,11);
check1 = "1914-09-04T07:09:06.000011Z";
check2 = before1970.toString("%FT%T.%fZ");
BOOST_CHECK_EQUAL(check1, check2);
gc::Time yearDay = yearDay.FromYearDay(1971, 3);
double dayInSeconds = 86400;
double yearInSeconds = 31536000;
BOOST_CHECK_EQUAL(double(yearDay),dayInSeconds*2 + yearInSeconds);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_AUTO_TEST_CASE(validStrings) {
gc::Time date(2016,8,26,15,44,9,644);
std::string test = date.toString("%FT%T.%fZ");
std::string check = "2016-08-26T15:44:09.000644Z";
bool equal = boost::iequals(test,check);
BOOST_CHECK_EQUAL(equal, true);
BOOST_CHECK(date.FromString(test.c_str(),"%FT%T.%fZ") == date);
BOOST_CHECK(test == date.iso());
BOOST_CHECK(date.fromString(test.c_str(),"%FT%T.%fZ") == true);
BOOST_CHECK_EQUAL(date.valid(), true);
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
BOOST_AUTO_TEST_SUITE_END()