Skip to content

overloaded getUnixEpoch() 'rounded' added #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -105,4 +108,4 @@ void loop()
Serial.print(F(" SIV: "));
Serial.println(SIV);
}
}
}
51 changes: 36 additions & 15 deletions src/SparkFun_u-blox_GNSS_Arduino_Library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/SparkFun_u-blox_GNSS_Arduino_Library.h
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,7 @@ class SFE_UBLOX_GNSS
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);
Expand Down