cftime_test.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file date_test.cpp
  3. /// \brief Unit tests for CF time functionality
  4. ///
  5. /// \author Joe Siltberg
  6. /// $Date$
  7. ///
  8. ///////////////////////////////////////////////////////////////////////////////////////
  9. #include "catch.hpp"
  10. #include "cftime.h"
  11. #include <time.h>
  12. #ifdef HAVE_NETCDF
  13. using namespace GuessNC::CF;
  14. namespace {
  15. bool verify_datetime(const DateTime& dt,
  16. int year, int month, int day, int hour, int minute, double second) {
  17. return dt.get_year() == year &&
  18. dt.get_month() == month &&
  19. dt.get_day() == day &&
  20. dt.get_hour() == hour &&
  21. dt.get_minute() == minute &&
  22. dt.get_second() == Approx(second);
  23. }
  24. }
  25. TEST_CASE("CF::DateTime/construction", "Tests construction of DateTime objects") {
  26. // default constructor
  27. REQUIRE(verify_datetime(DateTime(),
  28. 0, 1, 1, 0, 0, 0));
  29. // string constructor
  30. REQUIRE(verify_datetime(DateTime("2013-01-25 09:09:09"),
  31. 2013,01,25,9,9,9));
  32. // fractional seconds
  33. REQUIRE(verify_datetime(DateTime("1970-01-01 00:00:00.5"),
  34. 1970,01,01,0,0,.5));
  35. // date only
  36. REQUIRE(verify_datetime(DateTime("1900-10-01"),
  37. 1900,10,01,0,0,0));
  38. }
  39. TEST_CASE("CF::DateTime/add_time", "Tests the DateTime::add_time function") {
  40. // test NO_LEAP calendar
  41. DateTime dt("1980-01-01");
  42. dt.add_time(1, DAYS, NO_LEAP);
  43. REQUIRE(verify_datetime(dt, 1980,01,02,0,0,0));
  44. dt.add_time(30, DAYS, NO_LEAP);
  45. REQUIRE(verify_datetime(dt, 1980,02,01,0,0,0));
  46. dt.add_time(28, DAYS, NO_LEAP);
  47. REQUIRE(verify_datetime(dt, 1980,03,01,0,0,0));
  48. dt.add_time(365, DAYS, NO_LEAP);
  49. REQUIRE(verify_datetime(dt, 1981,03,01,0,0,0));
  50. dt.add_time(365*24+0.5, HOURS, NO_LEAP);
  51. REQUIRE(verify_datetime(dt, 1982,03,01,0,30,0));
  52. dt.add_time(0.1, SECONDS, NO_LEAP); // test rounding
  53. REQUIRE(verify_datetime(dt, 1982,03,01,0,30,0));
  54. dt.add_time(23.5*3600+365*24*3600*200.0, SECONDS, NO_LEAP);
  55. REQUIRE(verify_datetime(dt, 2182,03,02,0,0,0));
  56. // test STANDARD calendar
  57. dt = DateTime("1980-01-01");
  58. dt.add_time(1, DAYS, STANDARD);
  59. REQUIRE(verify_datetime(dt, 1980,01,02,0,0,0));
  60. dt.add_time(30, DAYS, STANDARD);
  61. REQUIRE(verify_datetime(dt, 1980,02,01,0,0,0));
  62. dt.add_time(28, DAYS, STANDARD);
  63. REQUIRE(verify_datetime(dt, 1980,02,29,0,0,0));
  64. dt.add_time(1, DAYS, STANDARD);
  65. REQUIRE(verify_datetime(dt, 1980,03,01,0,0,0));
  66. dt.add_time(364, DAYS, STANDARD);
  67. REQUIRE(verify_datetime(dt, 1981,02,28,0,0,0));
  68. dt.add_time(1, DAYS, STANDARD);
  69. REQUIRE(verify_datetime(dt, 1981,03,01,0,0,0));
  70. dt.add_time(366, DAYS, STANDARD);
  71. REQUIRE(verify_datetime(dt, 1982,03,02,0,0,0));
  72. // step 365*25 days forward and use C library time functions
  73. // (which use a gregorian calendar) to verify we reach the
  74. // correct date
  75. dt.add_time(365*25, DAYS, STANDARD);
  76. tm start_tm;
  77. start_tm.tm_sec = 0;
  78. start_tm.tm_min = 0;
  79. start_tm.tm_hour = 0;
  80. start_tm.tm_mday = 2;
  81. start_tm.tm_mon = 2;
  82. start_tm.tm_year = 82;
  83. time_t end_t = mktime(&start_tm)+25*365*24*3600;
  84. tm* end_tm = localtime(&end_t);
  85. REQUIRE(verify_datetime(dt,
  86. 1900+end_tm->tm_year, end_tm->tm_mon+1, end_tm->tm_mday,
  87. end_tm->tm_hour, end_tm->tm_min, end_tm->tm_sec));
  88. }
  89. TEST_CASE("CF::TimeUnitSpecification", "Tests CF time unit specifications") {
  90. TimeUnitSpecification tus("days since 1970-01-01 00:00:00");
  91. REQUIRE(verify_datetime(tus.get_date_time(1, STANDARD),
  92. 1970,01,02,0,0,0));
  93. tus = TimeUnitSpecification("hours since 1980-02-28 23:00:00");
  94. REQUIRE(verify_datetime(tus.get_date_time(1, STANDARD),
  95. 1980,02,29,0,0,0));
  96. }
  97. #endif // HAVE_NETCDF