This repository has been archived by the owner on May 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from 107-systems/date-time-to-posix
Adding conversion function converting NMEA date/time to POSIX timestamp
- Loading branch information
Showing
7 changed files
with
333 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.