Skip to content

Commit

Permalink
added bins
Browse files Browse the repository at this point in the history
  • Loading branch information
xanthospap committed Oct 16, 2024
1 parent b6e0524 commit 5f0b742
Show file tree
Hide file tree
Showing 16 changed files with 284 additions and 14 deletions.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ target_include_directories(datetime PUBLIC
$<INSTALL_INTERFACE:include/datetime>
)

add_subdirectory(src)
add_subdirectory(src/lib)

# add executables
add_executable(ymd2mjd src/bin/ymd2mjd.cpp)
target_link_libraries(ymd2mjd PRIVATE datetime)
add_executable(mjd2ymd src/bin/mjd2ymd.cpp)
target_link_libraries(mjd2ymd PRIVATE datetime)
add_executable(mjd2ydoy src/bin/mjd2ydoy.cpp)
target_link_libraries(mjd2ydoy PRIVATE datetime)

# disable clang-tidy (targets that follow will not be checked)
set(CMAKE_CXX_CLANG_TIDY "")
Expand Down
13 changes: 0 additions & 13 deletions src/CMakeLists.txt

This file was deleted.

87 changes: 87 additions & 0 deletions src/bin/mjd2ydoy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "calendar.hpp"
#include "datetime_write.hpp"
#include <cstdio>
#include <exception>
#include <cstring>
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <unistd.h>
#endif

constexpr const int MAX_ERRORS_ALLOWED = 10;

/* help message */
void prhelp() {
printf(
"jd2ydoy: Transform a date from Modified Julian Day to calendar date, "
"i.e.\n\"YYYYdDDD\". The program expects the read a Modified Julian "
"Day string\n(actually an integral value) from STDIN (or multiple MJDs, "
"seperated by\nnewlines) and will print results on STDOUT. The MJD string"
" can be followed by\nany number of remaining characters that will be "
"ignored.\n\nOptions:\n[-h] "
"help message\n\tprint (this) message and exit.\n[-e] "
"MAX_ERRORS_ALLOWED\n\tMaximum number of errors allowed (i.e. date "
"strings that where not\n\tparsed correctly). Default values is %d\n\n"
"\n\nWarnings:\n\t* Command line options are only available on POSIX "
"systems.\n\nDionysos Satellite Observatory\nNational Technical "
"University of "
"Athens\nhttps://github.com/DSOlab/ggdatetime\n",
MAX_ERRORS_ALLOWED);
return;
}

int main(int argc, char *argv[]) {
char line[124];
char buf[64];
int mjd;
char c;
int error = 0, cer = 0;
int max_errors_allowed = MAX_ERRORS_ALLOWED;

#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
/* parse command line arguments */
while ((c = getopt(argc, argv, "he:")) != -1) {
switch (c) {
case 'h':
prhelp();
return 0;
case 'e':
max_errors_allowed = atoi(optarg);
break;
default:
fprintf(stderr, "Usage: %s [-e MAX_ERRORS_ALLOWED]\n", argv[0]);
return 1;
} /* switch c */
}
#endif

while (fgets(line, sizeof(line), stdin) && (error<max_errors_allowed)) {
if (1 == sscanf(line, "%d", &mjd)) {
dso::modified_julian_day m(mjd);
const auto ymd = m.to_ymd();
try {
if (dso::SpitDate<dso::YMDFormat::YYYYDDD>::spit(ymd, buf) !=
dso::SpitDate<dso::YMDFormat::YYYYDDD>::numChars) {
++error;
} else {
printf("%s\n", buf);
}
} catch (std::exception &) {
++error;
}
} else {
++error;
}

if (error > cer) {
fprintf(stderr, "ERROR. Failed parsing/transforming line: %s\n", line);
++cer;
}
}

if (error>=max_errors_allowed) {
fprintf(stderr, "Too many errors, giving up!\n");
return 1;
}

return 0;
}
87 changes: 87 additions & 0 deletions src/bin/mjd2ymd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "calendar.hpp"
#include "datetime_write.hpp"
#include <cstdio>
#include <exception>
#include <cstring>
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <unistd.h>
#endif

constexpr const int MAX_ERRORS_ALLOWED = 10;

/* help message */
void prhelp() {
printf(
"jd2ymd: Transform a date from Modified Julian Day to calendar date, "
"i.e.\n\"YYYYdMMdDD\". The program expects the read a Modified Julian "
"Day string\n(actually an integral value) from STDIN (or multiple MJDs, "
"seperated by\nnewlines) and will print results on STDOUT. The MJD string"
" can be followed by\nany number of remaining characters that will be "
"ignored.\n\nOptions:\n[-h] "
"help message\n\tprint (this) message and exit.\n[-e] "
"MAX_ERRORS_ALLOWED\n\tMaximum number of errors allowed (i.e. date "
"strings that where not\n\tparsed correctly). Default values is %d\n\n"
"\n\nWarnings:\n\t* Command line options are only available on POSIX "
"systems.\n\nDionysos Satellite Observatory\nNational Technical "
"University of "
"Athens\nhttps://github.com/DSOlab/ggdatetime\n",
MAX_ERRORS_ALLOWED);
return;
}

int main(int argc, char *argv[]) {
char line[124];
char buf[64];
int mjd;
char c;
int error = 0, cer = 0;
int max_errors_allowed = MAX_ERRORS_ALLOWED;

#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
/* parse command line arguments */
while ((c = getopt(argc, argv, "he:")) != -1) {
switch (c) {
case 'h':
prhelp();
return 0;
case 'e':
max_errors_allowed = atoi(optarg);
break;
default:
fprintf(stderr, "Usage: %s [-e MAX_ERRORS_ALLOWED]\n", argv[0]);
return 1;
} /* switch c */
}
#endif

while (fgets(line, sizeof(line), stdin) && (error<max_errors_allowed)) {
if (1 == sscanf(line, "%d", &mjd)) {
dso::modified_julian_day m(mjd);
const auto ymd = m.to_ymd();
try {
if (dso::SpitDate<dso::YMDFormat::YYYYMMDD>::spit(ymd, buf) !=
dso::SpitDate<dso::YMDFormat::YYYYMMDD>::numChars) {
++error;
} else {
printf("%s\n", buf);
}
} catch (std::exception &) {
++error;
}
} else {
++error;
}

if (error > cer) {
fprintf(stderr, "ERROR. Failed parsing/transforming line: %s\n", line);
++cer;
}
}

if (error>=max_errors_allowed) {
fprintf(stderr, "Too many errors, giving up!\n");
return 1;
}

return 0;
}
88 changes: 88 additions & 0 deletions src/bin/ymd2mjd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "calendar.hpp"
#include <cstdio>
#include <exception>
#include <cstring>
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <unistd.h>
#endif

constexpr const int MAX_ERRORS_ALLOWED = 10;

/* help message */
void prhelp() {
printf(
"ymd2mjd: Transform a date given as \"YYYYdMMdDD\" to Modified Julian "
"Day. The\ncharacter \"d\" in the date string can be any character you "
"want,\nexcept from a numeric value. The program expects the read a date "
"string\nfrom STDIN (or multuple date strings, seperated by newlines) "
"and "
"will\nprint results on STDOUT.\nThe date string can be followed by any "
"numberof remaining characters\nthat will be ignored.\n\nExample "
"Usage:\n$>cat "
"dates\n2014:01:09\n2014:01:9\n2014:1:09\n2014:01:08\n2014:01:07\n2014:"
"01:0 // ERROR\n2014:01:1\n2014T01:1\n2014TT01:1 //ERROR\n2014:01:1with "
"some string\n2014/01/1with some string\n$>cat dates | "
"ymd2mjd\n56666\n56666\n56666\n56665\n56664\nERROR. Failed "
"parsing/transforming line: 2014:01:0\n\n56658\n56658\nERROR. Failed "
"parsing/transforming line: 2014TT01:1\n\n56658\n56658\n\nOptions:\n[-h] "
"help message\n\tprint (this) message and exit.\n[-e] "
"MAX_ERRORS_ALLOWED\n\tMaximum number of errors allowed (i.e. date "
"strings that where not\n\tparsed correctly). Default values is %d\n\n"
"\n\nWarnings:\n\t* Command line options are only available on POSIX "
"systems.\n\nDionysos Satellite "
"Observatory\nNational Technical University of "
"Athens\nhttps://github.com/DSOlab/ggdatetime\n",
MAX_ERRORS_ALLOWED);
return;
}

int main(int argc, char *argv[]) {
char line[124];
int yr, mn, dm;
char c;
int error = 0, cer = 0;
int max_errors_allowed = MAX_ERRORS_ALLOWED;

#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
/* parse command line arguments */
while ((c = getopt(argc, argv, "he:")) != -1) {
switch (c) {
case 'h':
prhelp();
return 0;
case 'e':
max_errors_allowed = atoi(optarg);
break;
default:
fprintf(stderr, "Usage: %s [-e MAX_ERRORS_ALLOWED]\n", argv[0]);
return 1;
} /* switch c */
}
#endif

while (fgets(line, sizeof(line), stdin) && (error<max_errors_allowed)) {
if (5 == sscanf(line, "%d%c%d%c%d", &yr, &c, &mn, &c, &dm)) {
try {
printf("%d\n", dso::modified_julian_day(dso::year(yr), dso::month(mn),
dso::day_of_month(dm))
.as_underlying_type());
} catch (std::exception &) {
++error;
}
} else {
++error;
}

if (error > cer) {
fprintf(stderr, "ERROR. Failed parsing/transforming line: %s\n", line);
++cer;
}
}

if (error>=max_errors_allowed) {
fprintf(stderr, "Too many errors, giving up!\n");
return 1;
}

return 0;
}
13 changes: 13 additions & 0 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
target_sources(datetime
PRIVATE
${CMAKE_SOURCE_DIR}/src/lib/dat.cpp
${CMAKE_SOURCE_DIR}/src/lib/datetime_io_core.cpp
${CMAKE_SOURCE_DIR}/src/lib/modified_julian_day.cpp
${CMAKE_SOURCE_DIR}/src/lib/month.cpp
${CMAKE_SOURCE_DIR}/src/lib/strmonth.cpp
${CMAKE_SOURCE_DIR}/src/lib/tpdateutc.cpp
${CMAKE_SOURCE_DIR}/src/lib/twopartdates.cpp
${CMAKE_SOURCE_DIR}/src/lib/utc2tai.cpp
${CMAKE_SOURCE_DIR}/src/lib/ydoy_date.cpp
${CMAKE_SOURCE_DIR}/src/lib/ymd_date.cpp
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 5f0b742

Please sign in to comment.