Merge pull request #18 from UT2UH/getUnixEpoch

overloaded getUnixEpoch() 'rounded' added
This commit is contained in:
Paul
2021-03-31 16:21:12 +01:00
committed by GitHub
3 changed files with 44 additions and 19 deletions
@@ -1,7 +1,7 @@
/*
Getting Unix Epoch Time and micros using u-blox commands
By: UT2UH
Date: March 30th, 2021
Date: March 31th, 2021
License: MIT. See license file for more information but you can
basically do whatever you want with this code.
@@ -69,8 +69,11 @@ void loop()
// getUnixEpoch marks the PVT data as stale so you will get Unix time and PVT time on alternate seconds
uint32_t us; //microseconds returned by getUnixEpoch()
uint32_t epoch = myGNSS.getUnixEpoch(us);
Serial.print("Unix Epoch: ");
uint32_t epoch = myGNSS.getUnixEpoch();
Serial.print("Unix Epoch rounded: ");
Serial.print(epoch, DEC);
epoch = myGNSS.getUnixEpoch(us);
Serial.print(" Exact Unix Epoch: ");
Serial.print(epoch, DEC);
Serial.print(" micros: ");
Serial.println(us, DEC);
@@ -105,4 +108,4 @@ void loop()
Serial.print(F(" SIV: "));
Serial.println(SIV);
}
}
}
+36 -15
View File
@@ -8954,16 +8954,41 @@ int32_t SFE_UBLOX_GNSS::getNanosecond(uint16_t maxWait)
return (packetUBXNAVPVT->data.nano);
}
//Get the current Unix epoch - includes microseconds
//Get the current Unix epoch time rounded up to the nearest second
uint32_t SFE_UBLOX_GNSS::getUnixEpoch(uint16_t maxWait)
{
if (packetUBXNAVPVT == NULL) initPacketUBXNAVPVT(); //Check that RAM has been allocated for the PVT data
if (packetUBXNAVPVT == NULL) //Bail if the RAM allocation failed
return 0;
if (packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.sec == false)
getPVT(maxWait);
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.year = false;
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.month = false;
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.day = false;
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.hour = false;
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.min = false;
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.sec = false;
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.all = false;
// assemble time elements into time_t - credits to Thomas Roell @ https://github.com/GrumpyOldPizza
uint32_t t = (uint32_t)(((((((packetUBXNAVPVT->data.year - 1970) * 365) + (((packetUBXNAVPVT->data.year - 1970) + 3) / 4)) +
DAYS_SINCE_MONTH[(packetUBXNAVPVT->data.year - 1970) & 3][packetUBXNAVPVT->data.month] +
(packetUBXNAVPVT->data.day - 1)) * 24 +
packetUBXNAVPVT->data.hour) * 60 +
packetUBXNAVPVT->data.min) * 60 +
packetUBXNAVPVT->data.sec);
return t;
}
//Get the current Unix epoch including microseconds
uint32_t SFE_UBLOX_GNSS::getUnixEpoch(uint32_t& microsecond, uint16_t maxWait)
{
if (packetUBXNAVPVT == NULL) initPacketUBXNAVPVT(); //Check that RAM has been allocated for the PVT data
if (packetUBXNAVPVT == NULL) //Bail if the RAM allocation failed
return 0;
if (packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.confirmedTime == false)
if (packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.nano == false)
getPVT(maxWait);
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.confirmedTime = false;
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.year = false;
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.month = false;
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.day = false;
@@ -8972,23 +8997,19 @@ uint32_t SFE_UBLOX_GNSS::getUnixEpoch(uint32_t& microsecond, uint16_t maxWait)
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.sec = false;
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.nano = false;
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.all = false;
uint32_t t = 0;
if((bool)packetUBXNAVPVT->data.flags2.bits.confirmedTime)
{
// assemble time elements into time_t - credits to Thomas Roell @ https://github.com/GrumpyOldPizza
t = (uint32_t)(((((((packetUBXNAVPVT->data.year - 1970) * 365) + (((packetUBXNAVPVT->data.year - 1970) + 3) / 4)) +
// assemble time elements into time_t - credits to Thomas Roell @ https://github.com/GrumpyOldPizza
uint32_t t = (uint32_t)(((((((packetUBXNAVPVT->data.year - 1970) * 365) + (((packetUBXNAVPVT->data.year - 1970) + 3) / 4)) +
DAYS_SINCE_MONTH[(packetUBXNAVPVT->data.year - 1970) & 3][packetUBXNAVPVT->data.month] +
(packetUBXNAVPVT->data.day - 1)) * 24 +
packetUBXNAVPVT->data.hour) * 60 +
packetUBXNAVPVT->data.min) * 60 +
packetUBXNAVPVT->data.sec);
int32_t us = packetUBXNAVPVT->data.nano / 1000;
microsecond = (uint32_t)us;
// adjust t if nano is negative
if(us < 0) {
microsecond = (uint32_t)(us + 1000000);
t--;
}
int32_t us = packetUBXNAVPVT->data.nano / 1000;
microsecond = (uint32_t)us;
// adjust t if nano is negative
if(us < 0) {
microsecond = (uint32_t)(us + 1000000);
t--;
}
return t;
}
@@ -938,6 +938,7 @@ public:
uint8_t getSecond(uint16_t maxWait = defaultMaxWait);
uint16_t getMillisecond(uint16_t maxWait = defaultMaxWait);
int32_t getNanosecond(uint16_t maxWait = defaultMaxWait);
uint32_t getUnixEpoch(uint16_t maxWait = defaultMaxWait);
uint32_t getUnixEpoch(uint32_t& microsecond, uint16_t maxWait = defaultMaxWait);
bool getDateValid(uint16_t maxWait = defaultMaxWait);