Skip to content

Commit

Permalink
hide magic time value from test mode decisions. (GPSBabel#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteven4 authored Dec 19, 2019
1 parent 355a20b commit 8eaed5c
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 30 deletions.
1 change: 1 addition & 0 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,7 @@ char* strlower(char* src);
signed int get_tz_offset();
time_t mklocaltime(struct tm* t);
time_t mkgmtime(struct tm* t);
bool gpsbabel_testmode();
gpsbabel::DateTime current_time();
void dotnet_time_to_time_t(double dotnet, time_t* t, int* millisecs);
signed int month_lookup(const char* m);
Expand Down
6 changes: 3 additions & 3 deletions garmin_gpi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1462,15 +1462,15 @@ garmin_gpi_rd_init(const QString& fname)
static void
garmin_gpi_wr_init(const QString& fname)
{
if (gpi_timestamp != 0) { /* not the first gpi output session */
if ((gpi_timestamp != 0) && !gpsbabel_testmode()) { /* not the first gpi output session */
time_t t = time(nullptr);
if (t <= gpi_timestamp) {
gpi_timestamp++; /* don't create files with same timestamp */
} else {
gpi_timestamp = t;
}
} else {
gpi_timestamp = gpsbabel_time; /* always ZERO during 'testo' */
gpi_timestamp = gpsbabel_time;
}

fout = gbfopen_le(fname, "wb", MYNAME);
Expand Down Expand Up @@ -1553,7 +1553,7 @@ garmin_gpi_wr_deinit()
mkshort_del_handle(&short_h);
gbfclose(fout);

if ((opt_sleep) && (gpi_timestamp != 0)) { /* don't sleep during 'testo' */
if ((opt_sleep) && !gpsbabel_testmode()) { /* don't sleep during 'testo' */
int sleep = atoi(opt_sleep);
if (sleep < 1) {
sleep = 1;
Expand Down
3 changes: 1 addition & 2 deletions globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

global_options global_opts;
const char gpsbabel_version[] = VERSION;
time_t gpsbabel_now; /* gpsbabel startup-time; initialized in main.c with time() */
time_t gpsbabel_time; /* gpsbabel startup-time; initialized in main.c with current_time(), ! ZERO within testo ! */
time_t gpsbabel_time; /* gpsbabel startup-time; initialized in main.c with current_time(), ! fixed within testo ! */

posn_status tracking_status;
2 changes: 1 addition & 1 deletion html.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ data_write()

// Don't write this line when running test suite. Actually, we should
// probably not write this line at all...
if (ugetenv("GPSBABEL_FREEZE_TIME").isNull()) {
if (!gpsbabel_testmode()) {
gbfprintf(file_out, " <meta name=\"Generator\" content=\"GPSBabel %s\">\n", gpsbabel_version);
}
gbfprintf(file_out, " <title>GPSBabel HTML Output</title>\n");
Expand Down
2 changes: 1 addition & 1 deletion kml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,7 @@ static void kml_write()
writer->writeTextElement(QStringLiteral("name"), QStringLiteral("GPS device"));
}

if (current_time().isValid()) {
if (!gpsbabel_testmode()) {
writer->writeTextElement(QStringLiteral("snippet"), QStringLiteral("Created ") +
current_time().toString());
}
Expand Down
5 changes: 2 additions & 3 deletions main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -721,10 +721,9 @@ main(int argc, char* argv[])
global_opts.charset_name.clear();
global_opts.inifile = nullptr;

gpsbabel_now = time(nullptr); /* gpsbabel startup-time */
gpsbabel_time = current_time().toTime_t(); /* same like gpsbabel_now, but frozen at zero during testo */
gpsbabel_time = current_time().toTime_t(); /* frozen in testmode */

if (gpsbabel_time != 0) { /* within testo ? */
if (!gpsbabel_testmode()) { /* within testo ? */
global_opts.inifile = inifile_init(QString(), MYNAME);
}

Expand Down
6 changes: 3 additions & 3 deletions osm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ osm_waypt_disp(const Waypoint* wpt)

if (strlen(created_by) !=0) {
gbfprintf(fout, " <tag k='created_by' v='%s",created_by);
if (gpsbabel_time != 0)
if (!gpsbabel_testmode())
if (strcmp("GPSBabel",created_by)==0) {
gbfprintf(fout, "-%s", gpsbabel_version);
}
Expand Down Expand Up @@ -892,7 +892,7 @@ osm_rte_disp_trail(const route_head* rte)

if (strlen(created_by) !=0) {
gbfprintf(fout, " <tag k='created_by' v='%s",created_by);
if (gpsbabel_time != 0)
if (!gpsbabel_testmode())
if (strcmp("GPSBabel",created_by)==0) {
gbfprintf(fout, "-%s", gpsbabel_version);
}
Expand Down Expand Up @@ -926,7 +926,7 @@ osm_write()
{
gbfprintf(fout, "<?xml version='1.0' encoding='UTF-8'?>\n");
gbfprintf(fout, "<osm version='0.6' generator='GPSBabel");
if (gpsbabel_time != 0) {
if (!gpsbabel_testmode()) {
gbfprintf(fout, "-%s", gpsbabel_version);
}
gbfprintf(fout, "'>\n");
Expand Down
22 changes: 12 additions & 10 deletions random.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <QtCore/QDateTime> // for QDateTime
#include <QtCore/QString> // for QString
#include <QtCore/QThread> // for QThread
#include <QtCore/QVector> // for QVector

#include "defs.h"
#include "garmin_fs.h" // for garmin_fs_t, GMSD_SET, garmin_fs_flags_t, garmin_fs_alloc
Expand Down Expand Up @@ -124,16 +125,22 @@ rand_qstr(const int maxlen, const char* fmt)
}

static void
random_rd_init(const QString&)
random_set_generator()
{
generator = new std::mt19937;
if (opt_seed) {
generator->seed(atoi(opt_seed));
} else {
generator->seed(gpsbabel_now);
generator->seed(gpsbabel_time);
}
}

static void
random_rd_init(const QString&)
{
random_set_generator();
}

static void
random_rd_deinit()
{
Expand Down Expand Up @@ -243,7 +250,7 @@ random_read()

route_head* head;
Waypoint* prev = nullptr;
QDateTime time = QDateTime::fromTime_t(gpsbabel_time);
QDateTime time = current_time();

int points = (opt_points) ? atoi(opt_points) : rand_int(128) + 1;
if (doing_trks || doing_rtes) {
Expand Down Expand Up @@ -289,17 +296,12 @@ static realtime_data* realtime;
void
random_rd_posn_init(const QString&)
{
generator = new std::mt19937;
if (opt_seed) {
generator->seed(atoi(opt_seed));
} else {
generator->seed(gpsbabel_now);
}
random_set_generator();
realtime = new realtime_data;
if (opt_points) {
realtime->points = atoi(opt_points);
}
realtime->time = QDateTime::fromTime_t(gpsbabel_time);
realtime->time = current_time();
}

void
Expand Down
9 changes: 8 additions & 1 deletion util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,13 @@ mklocaltime(struct tm* t)
return result;
}

bool
gpsbabel_testmode()
{
static bool testmode = getenv("GPSBABEL_FREEZE_TIME") != nullptr;
return testmode;
}

/*
* Historically, when we were C, this was A wrapper for time(2) that
* allowed us to "freeze" time for testing. The UNIX epoch
Expand All @@ -725,7 +732,7 @@ mklocaltime(struct tm* t)
gpsbabel::DateTime
current_time()
{
if (getenv("GPSBABEL_FREEZE_TIME")) {
if (gpsbabel_testmode()) {
return QDateTime::fromTime_t(0);
}

Expand Down
9 changes: 3 additions & 6 deletions xcsv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ xcsv_parse_val(const char* s, Waypoint* wpt, const field_map& fmp,
wpt->SetCreationTime(sscanftime(s, fmp.printfc.constData(), 1));
break;
case XT_LOCAL_TIME:
if (ugetenv("GPSBABEL_FREEZE_TIME").isNull()) {
if (!gpsbabel_testmode()) {
wpt->creation_time += sscanftime(s, fmp.printfc.constData(), 0);
} else {
/* Force constant time zone for test */
Expand Down Expand Up @@ -1763,13 +1763,10 @@ xcsv_replace_tokens(const QString& original) {
// Don't do potentially expensive replacements if token prefix
// isn't present;
if (original.contains("__")) {
time_t my_time = gpsbabel_time;

replacement.replace("__FILE__", xcsv_file.fname);
replacement.replace("__VERSION__", my_time == 0 ? "" : gpsbabel_version);
replacement.replace("__VERSION__", gpsbabel_testmode()? "" : gpsbabel_version);

QDateTime dt = QDateTime::fromTime_t(my_time);
dt = dt.toTimeSpec(Qt::UTC);
QDateTime dt = current_time().toUTC();

QString dts = dt.toString("ddd MMM dd hh:mm:ss yyyy");
replacement.replace("__DATE_AND_TIME__", dts);
Expand Down

0 comments on commit 8eaed5c

Please sign in to comment.