Skip to content

Commit

Permalink
var
Browse files Browse the repository at this point in the history
  • Loading branch information
xanthospap committed Oct 9, 2024
1 parent 780ac79 commit b6e0524
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 26 deletions.
26 changes: 26 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ project(
LANGUAGES CXX
)

# Enable clang-tidy option
option(ENABLE_CLANG_TIDY "Enable clang-tidy checks" OFF)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED On)
set(CMAKE_CXX_EXTENSIONS Off)
Expand All @@ -26,6 +29,23 @@ add_compile_definitions(
$<$<CONFIG:Debug>:DEBUG>
)

# clang-tidy (targets that follow will be checked)
if(ENABLE_CLANG_TIDY)
set(CMAKE_CXX_CLANG_TIDY "clang-tidy;-checks=bugprone-*,\
clang-analyzer-*,\
cppcoreguidelines-*,\
llvm-*,\
modernize-*,\
performance-*,\
-modernize-use-trailing-return-type,\
-cppcoreguidelines-pro-bounds-pointer-arithmetic,\
-cppcoreguidelines-pro-bounds-constant-array-index,\
-cppcoreguidelines-pro-type-vararg")
message(STATUS "clang-tidy is enabled.")
else()
message(STATUS "clang-tidy is disabled. To enable it, use -DENABLE_CLANG_TIDY=ON.")
endif()

add_library(datetime)
target_include_directories(datetime PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/>
Expand All @@ -34,13 +54,19 @@ target_include_directories(datetime PUBLIC

add_subdirectory(src)

# disable clang-tidy (targets that follow will not be checked)
set(CMAKE_CXX_CLANG_TIDY "")

# The tests
include(CTest)
add_subdirectory(test/unit_tests)
add_subdirectory(test/should_not_compile)
find_library(sofa sofa_c)
if (sofa)
add_subdirectory(test/sofa)
message(STATUS "found sofa lib, will compile relevant tests")
else()
message(STATUS "sofa lib not found; tests in test/sofa will not be compiled.")
endif()
enable_testing()

Expand Down
4 changes: 2 additions & 2 deletions include/cdatetime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
namespace dso {

/** Jan 1st 1980 for GPS Time */
constexpr const long JAN61980 = 44'244L;
constexpr const int JAN61980 = 44'244L;

/** */
constexpr const long JAN11901 = 15'385L;
constexpr const int JAN11901 = 15'385L;

/** @brief Seconds per day.
* @warning This is not always true in case of UTC dates; the day a leap
Expand Down
17 changes: 14 additions & 3 deletions include/dtfund.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <cstdio>
#include <limits>
#include <stdexcept>
#include <cstdint>

namespace dso {

Expand All @@ -37,7 +38,7 @@ class nanoseconds;

namespace core {
/** Number of days past at the end of non-leap and leap years. */
constexpr const long month_day[2][13] = {
constexpr const int month_day[2][13] = {
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}};

Expand Down Expand Up @@ -560,11 +561,15 @@ class month {
constexpr static const char *short_names[] = {"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"};
constexpr static const int SHORT_NAMES_LEN =
sizeof(short_names) / sizeof(short_names[0]);

/** long month names. */
constexpr static const char *long_names[] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
constexpr static const int LONG_NAMES_LEN =
sizeof(short_names) / sizeof(short_names[0]);

/** The month as underlying_type. */
underlying_type m_month;
Expand Down Expand Up @@ -982,8 +987,14 @@ constexpr ymd_date mjd2ymd(long mjd) noexcept {
*/
class modified_julian_day {
public:
/** MJDs are represented as long ints. */
typedef long underlying_type;
/** Decide on the INT (underlying) type of modified_julian_day.
*
* underlying_type should be at least as long as int_32_t. Else
* underlying_type will be a long.
* A 32bit int, has a range of -2147483648 to 2147483647. Should be enough!
*/
typedef std::conditional<sizeof(int) >= sizeof(int32_t), int, int64_t>::type
underlying_type;

/** Is fundamental datetime type */
static constexpr bool is_dt_fundamental_type = true;
Expand Down
11 changes: 9 additions & 2 deletions src/dat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
#include <cassert>
#include <cstdio>

namespace {
/** just so we do not have magic numbers */
constexpr const int MONTHS_IN_YEAR = 12;
} /* unnamed namespace */

namespace calendar_dat {
/** Dates and Delta(AT)s */
struct change {
int iyear, month, delat;
/** Combine year and month to form a date-ordered integer */
int ordered_int() const noexcept { return 12 * iyear + month; }
[[nodiscard]] int ordered_int() const noexcept {
return MONTHS_IN_YEAR * iyear + month;
}
};
constexpr const std::array<change, 28> changes = {
{{1972, 1, 10}, {1972, 7, 11}, {1973, 1, 12}, {1974, 1, 13}, {1975, 1, 14},
Expand Down Expand Up @@ -42,7 +49,7 @@ int dso::dat(dso::year iy, dso::month im) noexcept {
assert(iy >= dso::year(1972));

/* Combine year and month to form a date-ordered integer... */
int m = 12 * iy.as_underlying_type() + im.as_underlying_type();
int m = MONTHS_IN_YEAR * iy.as_underlying_type() + im.as_underlying_type();

/* ...and use it to find the preceding table entry. */
auto it = std::find_if(
Expand Down
14 changes: 9 additions & 5 deletions src/datetime_io_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
#include <charconv>
#include <cstdio>

namespace {
/** just so it is not a magic number */
constexpr const int MONTHS_IN_YEAR = 12;
}/* unnamed namespace */

inline const char *skipws(const char *line) noexcept {
const char *c = line;
while (*c && (*c == ' ' || *c == '/' || *c == '-' || *c == 'T' || *c == ':' ||
Expand Down Expand Up @@ -31,11 +36,10 @@ inline int count_decimal_digits(const char *fltstr) noexcept {
const char *dgtc = fltstr;
while (std::isdigit(*dgtc))
++dgtc;
return (dgtc - fltstr);
} else {
/* no decimal digits */
return 0;
return (int)(dgtc - fltstr);
}
/* no decimal digits */
return 0;
}

int dso::datetime_io_core::get_one_int(const char *str, int *ints,
Expand Down Expand Up @@ -151,7 +155,7 @@ int dso::datetime_io_core::get_two_ints_double(const char *str, int *ints,
/* before parsing the next floating point number, count its decimal digits
* If more than nanoseconds, issue a warning
*/
if (count_decimal_digits(skipws(c)) > 12) {
if (count_decimal_digits(skipws(c)) > MONTHS_IN_YEAR) {
fprintf(stderr, "[WARNING] Reading in date with resolution larger than "
"nanoseconds will lead to loss of precision!\n");
fprintf(stderr,
Expand Down
19 changes: 12 additions & 7 deletions src/modified_julian_day.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#include "dtfund.hpp"

namespace {
/** avoid magic numbers */
constexpr const int DAYS_IN_YEAR = 365;
} /* unnamed namespace */

dso::ydoy_date dso::modified_julian_day::to_ydoy() const noexcept {
const long days_fr_jan1_1901 = m_mjd - dso::JAN11901;
const long num_four_yrs = days_fr_jan1_1901 / 1461L;
const long years_so_far = 1901L + 4 * num_four_yrs;
const long days_left = days_fr_jan1_1901 - 1461 * num_four_yrs;
const long delta_yrs = days_left / 365 - days_left / 1460;
const int days_fr_jan1_1901 = m_mjd - dso::JAN11901;
const int num_four_yrs = days_fr_jan1_1901 / 1461;
const int years_so_far = 1901 + 4 * num_four_yrs;
const int days_left = days_fr_jan1_1901 - 1461 * num_four_yrs;
const int delta_yrs = days_left / DAYS_IN_YEAR - days_left / 1460;

return dso::ydoy_date(dso::year(years_so_far + delta_yrs),
dso::day_of_year(days_left - 365 * delta_yrs + 1));
return {dso::year(years_so_far + delta_yrs),
dso::day_of_year(days_left - DAYS_IN_YEAR * delta_yrs + 1)};
}
4 changes: 2 additions & 2 deletions src/strmonth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

dso::month::month(const char *str) : m_month(0) {
if (std::strlen(str) == 3) {
for (int i = 0; i < 12; i++) {
for (int i = 0; i < SHORT_NAMES_LEN; i++) {
if (!strcasecmp(str, short_names[i])) {
m_month = i + 1;
break;
}
}
} else if (std::strlen(str) > 3) {
for (int i = 0; i < 12; ++i) {
for (int i = 0; i < LONG_NAMES_LEN; ++i) {
if (!strcasecmp(str, long_names[i])) {
m_month = i + 1;
break;
Expand Down
4 changes: 2 additions & 2 deletions src/utc2tai.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "tpdate.hpp"

dso::TwoPartDate dso::TwoPartDateUTC::utc2tai() const noexcept {
FDOUBLE taisec;
FDOUBLE taisec=0e0;
int taimjd = this->utc2tai(taisec);
return dso::TwoPartDate(taimjd, dso::FractionalSeconds{taisec});
}

dso::TwoPartDate dso::TwoPartDateUTC::utc2tt() const noexcept {
FDOUBLE ttsec;
FDOUBLE ttsec=0e0;
int ttmjd = this->utc2tai(ttsec);
return dso::TwoPartDate(ttmjd, dso::FractionalSeconds{ttsec});
}
2 changes: 1 addition & 1 deletion src/ydoy_date.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "dtfund.hpp"

dso::ymd_date dso::ydoy_date::to_ymd() const noexcept {
int guess = static_cast<int>(__doy.as_underlying_type() * 0.032);
int guess = __doy.as_underlying_type() * 0.032;
int leap = yr().is_leap();
int more =
((dy().as_underlying_type() - dso::core::month_day[leap][guess + 1]) > 0);
Expand Down
4 changes: 2 additions & 2 deletions src/ymd_date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ dso::ydoy_date dso::ymd_date::to_ydoy() const {
}
int leap = yr().is_leap();
int md = mn().as_underlying_type() - 1;
return dso::ydoy_date(yr(), dso::day_of_year(core::month_day[leap][md] +
dm().as_underlying_type()));
return {yr(), dso::day_of_year(core::month_day[leap][md] +
dm().as_underlying_type())};
}

0 comments on commit b6e0524

Please sign in to comment.