Skip to content
This repository has been archived by the owner on May 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #24 from 107-systems/date-time-to-posix
Browse files Browse the repository at this point in the history
Adding conversion function converting NMEA date/time to POSIX timestamp
  • Loading branch information
aentinger authored Nov 10, 2020
2 parents 46cba8e + b8d6bb9 commit f428605
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 3 deletions.
3 changes: 3 additions & 0 deletions extras/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ set(TEST_SRCS
src/test_ArduinoNmeaParser.cpp
src/test_checksum.cpp
src/test_GxRMC.cpp
src/test_Types.cpp
src/test_main.cpp
src/test_rmc.cpp

../../src/nmea/util/checksum.cpp
../../src/nmea/util/rmc.cpp
../../src/nmea/util/timegm.c
../../src/nmea/GxRMC.cpp
../../src/nmea/Types.cpp
../../src/ArduinoNmeaParser.cpp
)

Expand Down
119 changes: 119 additions & 0 deletions extras/test/src/test_Types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* This software is distributed under the terms of the MIT License.
* Copyright (c) 2020 LXRobotics.
* Author: Alexander Entinger <[email protected]>
* Contributors: https://github.com/107-systems/107-Arduino-NMEA-Parser/graphs/contributors.
*/

/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include <catch.hpp>

#include <nmea/Types.h>

/**************************************************************************************
* TEST CODE
**************************************************************************************/

TEST_CASE("Convert date/time to POSIX timestamp with time.microseconds > 500", "[toPosixTimestamp-01]")
{
nmea::Date const date = {29,10,2020};
nmea::Time const time = {13,37,25,689};

time_t const posix_timestamp = nmea::toPosixTimestamp(date, time);

REQUIRE(posix_timestamp == 1603978646);
}

TEST_CASE("Convert date/time to POSIX timestamp with time.microseconds < 500", "[toPosixTimestamp-02]")
{
nmea::Date const date = {29,10,2020};
nmea::Time const time = {13,37,25,322};

time_t const posix_timestamp = nmea::toPosixTimestamp(date, time);

REQUIRE(posix_timestamp == 1603978646 - 1);
}

TEST_CASE("Testing isValid(Date const &)", "[isValid(Date)-01]")
{
WHEN ("Date is valid")
{
nmea::Date const date = {29,10,2020};
REQUIRE(nmea::isValid(date) == true);
}
WHEN ("Date.day is invalid")
{
nmea::Date const date = {-1,10,2020};
REQUIRE(nmea::isValid(date) == false);
}
WHEN ("Date.month is invalid")
{
nmea::Date const date = {29,-1,2020};
REQUIRE(nmea::isValid(date) == false);
}
WHEN ("Date.year is invalid")
{
nmea::Date const date = {29,10,-1};
REQUIRE(nmea::isValid(date) == false);
}
}

TEST_CASE("Testing isValid(Time const &)", "[isValid(Time)-01]")
{
WHEN ("Time is valid")
{
nmea::Time const time = {13,37,25,322};
REQUIRE(nmea::isValid(time) == true);
}
WHEN ("Time.hour is invalid")
{
nmea::Time const time = {-1,37,25,322};
REQUIRE(nmea::isValid(time) == false);
}
WHEN ("Time.minute is invalid")
{
nmea::Time const time = {13,-1,25,322};
REQUIRE(nmea::isValid(time) == false);
}
WHEN ("Time.second is invalid")
{
nmea::Time const time = {13,37,-1,322};
REQUIRE(nmea::isValid(time) == false);
}
WHEN ("Time.microsecond is invalid")
{
nmea::Time const time = {13,37,25,-1};
REQUIRE(nmea::isValid(time) == false);
}
}

TEST_CASE("Testing isValid(Date const &, Time const &)", "[isValid(Date,Time)-01]")
{
WHEN ("Both date and time are valid")
{
nmea::Date const date = {29,10,2020};
nmea::Time const time = {13,37,25,322};
REQUIRE(nmea::isValid(date, time) == true);
}
WHEN ("Date is invalid")
{
nmea::Date const date = {-1,10,2020};
nmea::Time const time = {13,37,25,322};
REQUIRE(nmea::isValid(date, time) == false);
}
WHEN ("Time is invalid")
{
nmea::Date const date = {29,10,2020};
nmea::Time const time = {-1,37,25,322};
REQUIRE(nmea::isValid(date, time) == false);
}
WHEN ("Both date and time are invalid")
{
nmea::Date const date = {-1,10,2020};
nmea::Time const time = {-1,37,25,322};
REQUIRE(nmea::isValid(date, time) == false);
}
}
20 changes: 19 additions & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,39 @@
# Class (KEYWORD1)
#######################################

# class
ArduinoNmeaParser KEYWORD1
# struct
Time KEYWORD1
Date KEYWORD1
RmcData KEYWORD1
# enum class
RmcSource KEYWORD1
Error KEYWORD1
# namespace
nmea KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

encode KEYWORD2
rmc KEYWORD2
clearerr KEYWORD2
error KEYWORD2
isValid KEYWORD2
toPosixTimestamp KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################

# enum class Error
None LITERAL1
Checksum LITERAL1
RMC LITERAL1
# enum class RmcSource
Unknown LITERAL1
GPS LITERAL1
Galileo LITERAL1
GLONASS LITERAL1
GNSS LITERAL1
3 changes: 1 addition & 2 deletions src/ArduinoNmeaParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include "ArduinoNmeaParser.h"

#include <math.h>
#include <string.h>

#include "nmea/GxRMC.h"
Expand All @@ -26,7 +25,7 @@ ArduinoNmeaParser::ArduinoNmeaParser(OnRmcUpdateFunc on_rmc_update)
: _error{Error::None}
, _parser_state{ParserState::Synching}
, _parser_buf{{0}, 0}
, _rmc{false, nmea::RmcSource::Unknown, NAN, NAN, NAN, NAN, NAN, {-1, -1, -1, -1}, {-1, -1, -1}}
, _rmc{nmea::INVALID_RMC}
, _on_rmc_update{on_rmc_update}
{

Expand Down
83 changes: 83 additions & 0 deletions src/nmea/Types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* This software is distributed under the terms of the MIT License.
* Copyright (c) 2020 LXRobotics.
* Author: Alexander Entinger <[email protected]>
* Contributors: https://github.com/107-systems/107-Arduino-NMEA-Parser/graphs/contributors.
*/

/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include "Types.h"

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

namespace nmea
{

/**************************************************************************************
* EXTERN FUNCTION DECLARATION
**************************************************************************************/

extern "C" time_t rk_timegm (struct tm *tm);

/**************************************************************************************
* PUBLIC FUNCTION DEFINITION
**************************************************************************************/

bool isValid(Date const & date)
{
if ((date.day == INVALID_DATE.day) ||
(date.month == INVALID_DATE.month) ||
(date.year == INVALID_DATE.year))
return false;
else
return true;
}

bool isValid(Time const & time)
{
if ((time.hour == INVALID_TIME.hour) ||
(time.minute == INVALID_TIME.minute) ||
(time.second == INVALID_TIME.second) ||
(time.microsecond == INVALID_TIME.microsecond))
return false;
else
return true;
}

bool isValid(Date const & date, Time const & time)
{
return (isValid(date) && isValid(time));
}

time_t toPosixTimestamp(Date const & date, Time const & time)
{
struct tm tm =
{
/* tm_sec */ time.second + ((time.microsecond > 500) ? 1 : 0),
/* tm_min */ time.minute,
/* tm_hour */ time.hour,
/* tm_mday */ date.day,
/* tm_mon */ date.month - 1,
/* tm_year */ date.year - 1900,
/* tm_wday */ 0,
/* tm_yday */ 0,
/* tm_isdst */ 0,
#ifdef HOST
/* tm_gmtoff */ 0,
/* tm_zone */ 0,
#endif
};

return rk_timegm(&tm);
}

/**************************************************************************************
* NAMESPACE
**************************************************************************************/

} /* nmea */
24 changes: 24 additions & 0 deletions src/nmea/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
#ifndef ARDUINO_MTK3333_NMEA_TYPES_H_
#define ARDUINO_MTK3333_NMEA_TYPES_H_

/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include <time.h>
#include <math.h>

/**************************************************************************************
* NAMESPACE
**************************************************************************************/
Expand Down Expand Up @@ -52,6 +59,23 @@ typedef struct
Date date;
} RmcData;

/**************************************************************************************
* CONST
**************************************************************************************/

Time const INVALID_TIME = {-1, -1, -1, -1};
Date const INVALID_DATE = {-1, -1, -1};
RmcData const INVALID_RMC = {false, RmcSource::Unknown, NAN, NAN, NAN, NAN, NAN, INVALID_TIME, INVALID_DATE};

/**************************************************************************************
* FUNCTION DECLARATION
**************************************************************************************/

bool isValid (Date const & date);
bool isValid (Time const & time);
bool isValid (Date const & date, Time const & time);
time_t toPosixTimestamp(Date const & date, Time const & time);

/**************************************************************************************
* NAMESPACE
**************************************************************************************/
Expand Down
Loading

0 comments on commit f428605

Please sign in to comment.