Skip to content

Commit

Permalink
fixed confusion in TimeLib between system time and gps time
Browse files Browse the repository at this point in the history
  • Loading branch information
jpswinski committed Feb 15, 2023
1 parent 513af58 commit 0247d95
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 92 deletions.
4 changes: 2 additions & 2 deletions packages/arrow/ParquetBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -815,13 +815,13 @@ const char* ParquetBuilder::buildGeoMetaData (void)
const char* ParquetBuilder::buildServerMetaData (void)
{
/* Build Launch Time String */
int64_t launch_time_gps = TimeLib::gettimems(OsApi::getLaunchTime());
int64_t launch_time_gps = TimeLib::sys2gpstime(OsApi::getLaunchTime());
TimeLib::gmt_time_t timeinfo = TimeLib::gps2gmttime(launch_time_gps);
TimeLib::date_t dateinfo = TimeLib::gmt2date(timeinfo);
SafeString timestr("%04d-%02d-%02dT%02d:%02d:%02dZ", timeinfo.year, dateinfo.month, dateinfo.day, timeinfo.hour, timeinfo.minute, timeinfo.second);

/* Build Duration String */
int64_t duration = TimeLib::gettimems() - launch_time_gps;
int64_t duration = TimeLib::gpstime() - launch_time_gps;
SafeString durationstr("%ld", duration);

/* Build Package String */
Expand Down
6 changes: 3 additions & 3 deletions packages/aws/S3CurlIODriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ static headers_t buildReadHeadersV2 (const char* bucket, const char* key, Creden
struct curl_slist* headers = NULL;

/* Build Date String and Date Header */
TimeLib::gmt_time_t gmt_time = TimeLib::gettime();
TimeLib::gmt_time_t gmt_time = TimeLib::gmttime();
TimeLib::date_t gmt_date = TimeLib::gmt2date(gmt_time);
SafeString date("%04d%02d%02dT%02d%02d%02dZ", gmt_date.year, gmt_date.month, gmt_date.day, gmt_time.hour, gmt_time.minute, gmt_time.second);
SafeString dateHeader("Date: %s", date.str());
Expand Down Expand Up @@ -190,7 +190,7 @@ static headers_t buildWriteHeadersV2 (const char* bucket, const char* key, const
struct curl_slist* headers = NULL;

/* Build Date String and Date Header */
TimeLib::gmt_time_t gmt_time = TimeLib::gettime();
TimeLib::gmt_time_t gmt_time = TimeLib::gmttime();
TimeLib::date_t gmt_date = TimeLib::gmt2date(gmt_time);
SafeString date("%04d%02d%02dT%02d%02d%02dZ", gmt_date.year, gmt_date.month, gmt_date.day, gmt_time.hour, gmt_time.minute, gmt_time.second);
SafeString dateHeader("Date: %s", date.str());
Expand Down Expand Up @@ -245,7 +245,7 @@ static headers_t buildWriteHeadersV4 (const char* bucket, const char* key, const
}

/* Build Date String */
TimeLib::gmt_time_t gmt_time = TimeLib::gettime();
TimeLib::gmt_time_t gmt_time = TimeLib::gmttime();
TimeLib::date_t gmt_date = TimeLib::gmt2date(gmt_time);
SafeString timestamp("%04d%02d%02dT%02d%02d%02dZ", gmt_date.year, gmt_date.month, gmt_date.day, gmt_time.hour, gmt_time.minute, gmt_time.second);

Expand Down
2 changes: 1 addition & 1 deletion packages/core/ClusterSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ int ClusterSocket::onAlive(int fd)
read_connection_t* connection = read_connections[fd];

/* Send Meter */
int64_t now = TimeLib::gettimems();
int64_t now = TimeLib::gpstime();
if((now - connection->prev) > METER_PERIOD_MS)
{
uint8_t meter = 0;
Expand Down
8 changes: 4 additions & 4 deletions packages/core/EventLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ uint32_t EventLib::startTrace(uint32_t parent, const char* name, event_level_t l
if(lvl < trace_level) return parent;

/* Initialize Trace */
event.systime = TimeLib::gettimems();
event.systime = TimeLib::gpstime();
event.tid = Thread::getId();
event.id = trace_id++;;
event.parent = parent;
Expand Down Expand Up @@ -280,7 +280,7 @@ void EventLib::stopTrace(uint32_t id, event_level_t lvl)
if(lvl < trace_level) return;

/* Initialize Trace */
event.systime = TimeLib::gettimems();
event.systime = TimeLib::gpstime();
event.tid = 0;
event.id = id;
event.parent = ORIGIN;
Expand Down Expand Up @@ -326,7 +326,7 @@ void EventLib::logMsg(const char* file_name, unsigned int line_number, event_lev
if(lvl < log_level) return;

/* Initialize Log Message */
event.systime = TimeLib::gettimems();
event.systime = TimeLib::gpstime();
event.tid = Thread::getId();
event.id = ORIGIN;
event.parent = ORIGIN;
Expand Down Expand Up @@ -489,7 +489,7 @@ void EventLib::generateMetric (int32_t id, event_level_t lvl)
if (metric.id == INVALID_METRIC) return;

/* Initialize Log Message */
event.systime = TimeLib::gettimems();
event.systime = TimeLib::gpstime();
event.tid = Thread::getId();
event.id = metric.id;
event.parent = metric.subtype;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/LuaLibraryMsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ int LuaLibraryMsg::lmsg_sendlog (lua_State* L)
/* Construct Log Record */
EventLib::event_t event;
memset(&event, 0, sizeof(event));
event.systime = TimeLib::gettimems();
event.systime = TimeLib::gpstime();
event.tid = Thread::getId();
event.id = ORIGIN;
event.parent = ORIGIN;
Expand Down
19 changes: 9 additions & 10 deletions packages/core/LuaLibrarySys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,20 @@ int LuaLibrarySys::luaopen_syslib (lua_State *L)
*----------------------------------------------------------------------------*/
int LuaLibrarySys::lsys_version (lua_State* L)
{
/* Display Version Information on Terminal */
/* Get Information */
int64_t launch_time_gps = TimeLib::sys2gpstime(OsApi::getLaunchTime());
TimeLib::gmt_time_t timeinfo = TimeLib::gps2gmttime(launch_time_gps);
TimeLib::date_t dateinfo = TimeLib::gmt2date(timeinfo);
SafeString timestr("%04d-%02d-%02dT%02d:%02d:%02dZ", timeinfo.year, dateinfo.month, dateinfo.day, timeinfo.hour, timeinfo.minute, timeinfo.second);
int64_t duration = TimeLib::gpstime() - launch_time_gps;
const char** pkg_list = LuaEngine::getPkgList();

/* Display Information on Terminal */
print2term("SlideRule Version: %s\n", LIBID);
print2term("Build Information: %s\n", BUILDINFO);
print2term("Environment Version: %s\n", OsApi::getEnvVersion());

/* Display Timing Information on Terminal */
int64_t duration = TimeLib::gettimems() - OsApi::getLaunchTime();
TimeLib::gmt_time_t timeinfo = TimeLib::gps2gmttime(OsApi::getLaunchTime());
TimeLib::date_t dateinfo = TimeLib::gmt2date(timeinfo);
SafeString timestr("%04d-%02d-%02dT%02d:%02d:%02dZ", timeinfo.year, dateinfo.month, dateinfo.day, timeinfo.hour, timeinfo.minute, timeinfo.second);
print2term("Launch Time: %s\n", timestr.str());
print2term("Duration: %.2lf days\n", (double)duration / 1000.0 / 60.0 / 60.0 / 24.0); // milliseconds / seconds / minutes / hours

/* Display Package Information on Terminal */
const char** pkg_list = LuaEngine::getPkgList();
print2term("Packages: [ ");
if(pkg_list)
{
Expand Down
34 changes: 17 additions & 17 deletions packages/core/LuaLibraryTime.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
/*
* Copyright (c) 2021, University of Washington
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the University of Washington nor the names of its
* contributors may be used to endorse or promote products derived from this
*
* 3. Neither the name of the University of Washington nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF WASHINGTON AND CONTRIBUTORS
* “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF WASHINGTON OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF WASHINGTON OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

Expand Down Expand Up @@ -96,7 +96,7 @@ int LuaLibraryTime::ltime_latch (lua_State* L)
*----------------------------------------------------------------------------*/
int LuaLibraryTime::ltime_getgps (lua_State* L)
{
int64_t now = TimeLib::gettimems();
int64_t now = TimeLib::gpstime();
lua_pushnumber(L, (lua_Number)now); /* push "now" as result */
return 1; /* one result */
}
Expand All @@ -109,7 +109,7 @@ int LuaLibraryTime::ltime_getgps (lua_State* L)
*----------------------------------------------------------------------------*/
int LuaLibraryTime::ltime_getgmt (lua_State* L)
{
TimeLib::gmt_time_t now = TimeLib::gettime();
TimeLib::gmt_time_t now = TimeLib::gmttime();
lua_pushnumber(L, now.year); /* push "year" as result */
lua_pushnumber(L, now.doy); /* push "day of year" as result */
lua_pushnumber(L, now.hour); /* push "hour" as result */
Expand Down
71 changes: 33 additions & 38 deletions packages/core/TimeLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ void TimeLib::init(void)
/* Setup Base Time */
parsenistfile();
lastTime = OsApi::time(OsApi::SYS_CLK);
baseTimeMs = lastTime / 1000;
baseTimeMs += getleapms(baseTimeMs);
baseTimeMs -= LocalGpsEpochMs;
baseTimeMs = sys2gpstime(lastTime);
currentTimeMs = baseTimeMs;
runningTimeUs = 0;
stepTimeUs = 1000;
Expand Down Expand Up @@ -141,47 +139,46 @@ double TimeLib::latchtime(void)
*
* grabs the current number of ms that have elapsed since gps time epoch
*----------------------------------------------------------------------------*/
int64_t TimeLib::gettimems(int64_t now)
int64_t TimeLib::gpstime(void)
{
if(now == USE_CURRENT_TIME)
{
#ifdef TIME_HEARTBEAT
return currentTimeMs;
#else
int64_t sysnow = OsApi::time(OsApi::SYS_CLK);
sysnow /= 1000; // to milliseconds
sysnow += getleapms(sysnow);
sysnow -= LocalGpsEpochMs; // to gps epoch
return sysnow;
#endif
}
else
{
return now + getleapms(now) - LocalGpsEpochMs;
}
#ifdef TIME_HEARTBEAT
return currentTimeMs;
#else
return sys2gpstime(OsApi::time(OsApi::SYS_CLK));
#endif
}

/*----------------------------------------------------------------------------
* gettime
*
* grabs the current GMT system time
*----------------------------------------------------------------------------*/
TimeLib::gmt_time_t TimeLib::gettime(int64_t now)
TimeLib::gmt_time_t TimeLib::gmttime(void)
{
#ifdef _USE_WINDOWS_TIME_
SYSTEMTIME systime;
GetSystemTime(&systime);
gmt_time_t gmttime;
gmttime.year = systime.wYear;
gmttime.doy = dayofyear(systime.wYear, systime.wMonth, systime.wDay);
gmttime.hour = systime.wHour;
gmttime.minute = systime.wMinute;
gmttime.second = systime.wSecond;
gmttime.millisecond = systime.wMilliseconds;
return gmttime;
#else
return gps2gmttime(gettimems(now));
#endif
return gps2gmttime(gpstime());
}

/*----------------------------------------------------------------------------
* sys2gpstime
*
* converts system time (microseconds) to milliseconds since gps epoch
*----------------------------------------------------------------------------*/
int64_t TimeLib::sys2gpstime (int64_t sysnow)
{
sysnow /= 1000; // to milliseconds
sysnow += getleapms(sysnow);
sysnow -= LocalGpsEpochMs; // to gps epoch
return sysnow;
}

/*----------------------------------------------------------------------------
* sys2gmttime
*
* converts system time (microseconds) to milliseconds since gps epoch
*----------------------------------------------------------------------------*/
TimeLib::gmt_time_t TimeLib::sys2gmttime (int64_t sysnow)
{
return(gps2gmttime(sys2gpstime(sysnow)));
}

/*----------------------------------------------------------------------------
Expand Down Expand Up @@ -726,9 +723,7 @@ void TimeLib::heartbeat(void)
{
/* Detect Gross Adjustment */
mlog(CRITICAL, "Gross adjustment detected in step time: %lld", (long long)usec_per_sec);
baseTimeMs = now / 1000;
baseTimeMs += getleapms(baseTimeMs);
baseTimeMs -= LocalGpsEpochMs; // moves time up to GPS epoch
baseTimeMs = sys2gpstime(now);
runningTimeUs = 0;
stepTimeUs = 1000;
}
Expand Down
6 changes: 4 additions & 2 deletions packages/core/TimeLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ class TimeLib
static void init (void);
static void deinit (void);
static double latchtime (void); // system call, returns seconds (microsecond precision)
static int64_t gettimems (int64_t now=USE_CURRENT_TIME); // optimized, returns milliseconds since gps epoch
static gmt_time_t gettime (int64_t now=USE_CURRENT_TIME); // returns GMT time (includes leap seconds)
static int64_t gpstime (void); // optimized, returns milliseconds since gps epoch
static gmt_time_t gmttime (void); // returns GMT time (includes leap seconds)
static int64_t sys2gpstime (int64_t sysnow); // takes system time (microseconds), returns milliseconds from gps epoch
static gmt_time_t sys2gmttime (int64_t sysnow); // takes system time (microseconds), returns GMT time (includes leap seconds)
static gmt_time_t gps2gmttime (int64_t ms); // returns GMT time (includes leap seconds), takes gps time as milliseconds since gps epoch
static gmt_time_t cds2gmttime (int days, int msecs); // returns GMT time (includes leap seconds)
static date_t gmt2date (const gmt_time_t& gmt_time); // returns date (taking into account leap years)
Expand Down
2 changes: 1 addition & 1 deletion packages/geo/GeoJsonRaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ GeoJsonRaster::GeoJsonRaster(lua_State *L, GeoParms* _parms, const char *file, l
mlog(DEBUG, "Rasterized geojson into raster %s", rasterFile.c_str());

/* Store raster creation time */
gmtDate = TimeLib::gettime();
gmtDate = TimeLib::gmttime();

/* Must close raster to flush it into file */
GDALClose((GDALDatasetH)rasterDset);
Expand Down
2 changes: 1 addition & 1 deletion packages/geo/GeoParms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void GeoParms::fromLua (lua_State* L, int index)
/* Start and Stop Time Special Cases */
if(t0_str && !t1_str) // only start time supplied
{
int64_t now = TimeLib::gettimems();
int64_t now = TimeLib::gpstime();
stop_time = TimeLib::gps2gmttime(now);
TimeLib::date_t stop_date = TimeLib::gmt2date(stop_time);
mlog(DEBUG, "Setting %s to %04d-%02d-%02dT%02d:%02d:%02dZ", STOP_TIME, stop_date.year, stop_date.month, stop_date.day, stop_time.hour, stop_time.minute, stop_time.second);
Expand Down
8 changes: 4 additions & 4 deletions packages/h5/H5Coro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1725,16 +1725,16 @@ int H5FileBuffer::readObjHdr (uint64_t pos, int dlvl)
print2term("Object Information [%d]: 0x%lx\n", dlvl, (unsigned long)starting_position);
print2term("----------------\n");

TimeLib::gmt_time_t access_gmt = TimeLib::gettime(access_time * TIME_MILLISECS_IN_A_SECOND);
TimeLib::gmt_time_t access_gmt = TimeLib::sys2gmttime(access_time * TIME_MILLISECS_IN_A_SECOND);
print2term("Access Time: %d:%d:%d:%d:%d\n", access_gmt.year, access_gmt.doy, access_gmt.hour, access_gmt.minute, access_gmt.second);

TimeLib::gmt_time_t modification_gmt = TimeLib::gettime(modification_time * TIME_MILLISECS_IN_A_SECOND);
TimeLib::gmt_time_t modification_gmt = TimeLib::sys2gmttime(modification_time * TIME_MILLISECS_IN_A_SECOND);
print2term("Modification Time: %d:%d:%d:%d:%d\n", modification_gmt.year, modification_gmt.doy, modification_gmt.hour, modification_gmt.minute, modification_gmt.second);

TimeLib::gmt_time_t change_gmt = TimeLib::gettime(change_time * TIME_MILLISECS_IN_A_SECOND);
TimeLib::gmt_time_t change_gmt = TimeLib::sys2gmttime(change_time * TIME_MILLISECS_IN_A_SECOND);
print2term("Change Time: %d:%d:%d:%d:%d\n", change_gmt.year, change_gmt.doy, change_gmt.hour, change_gmt.minute, change_gmt.second);

TimeLib::gmt_time_t birth_gmt = TimeLib::gettime(birth_time * TIME_MILLISECS_IN_A_SECOND);
TimeLib::gmt_time_t birth_gmt = TimeLib::sys2gmttime(birth_time * TIME_MILLISECS_IN_A_SECOND);
print2term("Birth Time: %d:%d:%d:%d:%d\n", birth_gmt.year, birth_gmt.doy, birth_gmt.hour, birth_gmt.minute, birth_gmt.second);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/legacy/CcsdsPacketProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ bool CcsdsPacketProcessor::processMsg (unsigned char* msg, int bytes)
/* Pull Out Time */
if(measureLatency)
{
int64_t nowt = TimeLib::gettimems();
int64_t nowt = TimeLib::gpstime();
int64_t pktt = TimeLib::gmt2gpstime(TimeLib::cds2gmttime(CCSDS_GET_CDS_DAYS(msg), CCSDS_GET_CDS_MSECS(msg)));
latency = nowt - pktt;
cmdProc->setCurrentValue(getName(), latencyKey, (void*)&latency, sizeof(latency));
Expand Down
6 changes: 3 additions & 3 deletions packages/legacy/CfsInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ void CfsInterface::measurePkt (unsigned char* pktbuf, int bytes)
if(pktbuf == NULL || bytes < 6) return;

unsigned int apid = CCSDS_GET_APID(pktbuf);
int64_t now = TimeLib::gettimems();
int64_t now = TimeLib::gpstime();
bool cmd = CCSDS_IS_CMD(pktbuf);
unsigned int seg = CCSDS_GET_SEQFLG(pktbuf);

Expand All @@ -573,7 +573,7 @@ void CfsInterface::measurePkt (unsigned char* pktbuf, int bytes)
/* Calculate Packet Average Bits per Second */
if(stat->first_pkt_time == 0)
{
stat->first_pkt_time = TimeLib::gettimems();
stat->first_pkt_time = TimeLib::gpstime();
}
else
{
Expand All @@ -584,7 +584,7 @@ void CfsInterface::measurePkt (unsigned char* pktbuf, int bytes)
/* Calculate Total Average Bits per Second */
if(all->first_pkt_time == 0)
{
all->first_pkt_time = TimeLib::gettimems();
all->first_pkt_time = TimeLib::gpstime();
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions packages/legacy/UT_Dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ int UT_Dictionary::functionalUnitTestCmd (int argc, char argv[][MAX_CMD_SIZE])
bool failure=false;

/* Start Timer */
int64_t start_time = TimeLib::gettimems();
int64_t start_time = TimeLib::gpstime();

/* Get Word List */
const char* wordset_name = argv[0];
Expand Down Expand Up @@ -283,7 +283,7 @@ int UT_Dictionary::functionalUnitTestCmd (int argc, char argv[][MAX_CMD_SIZE])
}

/* Start Timer */
int64_t stop_time = TimeLib::gettimems();
int64_t stop_time = TimeLib::gpstime();
double elapsed_time = (double)(stop_time - start_time) / 1000.0;
print2term("Time to complete: %lf seconds\n", elapsed_time);

Expand Down
Loading

0 comments on commit 0247d95

Please sign in to comment.