Skip to content

Commit 6ccf229

Browse files
authored
Merge pull request #18 from UT2UH/getUnixEpoch
overloaded getUnixEpoch() 'rounded' added
2 parents c931a04 + c889a32 commit 6ccf229

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

Diff for: examples/Example24_GetUnixEpochAndMicros/Example24_GetUnixEpochAndMicros.ino

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
Getting Unix Epoch Time and micros using u-blox commands
33
By: UT2UH
4-
Date: March 30th, 2021
4+
Date: March 31th, 2021
55
License: MIT. See license file for more information but you can
66
basically do whatever you want with this code.
77
@@ -69,8 +69,11 @@ void loop()
6969
// getUnixEpoch marks the PVT data as stale so you will get Unix time and PVT time on alternate seconds
7070

7171
uint32_t us; //microseconds returned by getUnixEpoch()
72-
uint32_t epoch = myGNSS.getUnixEpoch(us);
73-
Serial.print("Unix Epoch: ");
72+
uint32_t epoch = myGNSS.getUnixEpoch();
73+
Serial.print("Unix Epoch rounded: ");
74+
Serial.print(epoch, DEC);
75+
epoch = myGNSS.getUnixEpoch(us);
76+
Serial.print(" Exact Unix Epoch: ");
7477
Serial.print(epoch, DEC);
7578
Serial.print(" micros: ");
7679
Serial.println(us, DEC);
@@ -105,4 +108,4 @@ void loop()
105108
Serial.print(F(" SIV: "));
106109
Serial.println(SIV);
107110
}
108-
}
111+
}

Diff for: src/SparkFun_u-blox_GNSS_Arduino_Library.cpp

+36-15
Original file line numberDiff line numberDiff line change
@@ -8954,16 +8954,41 @@ int32_t SFE_UBLOX_GNSS::getNanosecond(uint16_t maxWait)
89548954
return (packetUBXNAVPVT->data.nano);
89558955
}
89568956

8957-
//Get the current Unix epoch - includes microseconds
8957+
//Get the current Unix epoch time rounded up to the nearest second
8958+
uint32_t SFE_UBLOX_GNSS::getUnixEpoch(uint16_t maxWait)
8959+
{
8960+
if (packetUBXNAVPVT == NULL) initPacketUBXNAVPVT(); //Check that RAM has been allocated for the PVT data
8961+
if (packetUBXNAVPVT == NULL) //Bail if the RAM allocation failed
8962+
return 0;
8963+
8964+
if (packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.sec == false)
8965+
getPVT(maxWait);
8966+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.year = false;
8967+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.month = false;
8968+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.day = false;
8969+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.hour = false;
8970+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.min = false;
8971+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.sec = false;
8972+
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.all = false;
8973+
// assemble time elements into time_t - credits to Thomas Roell @ https://github.com/GrumpyOldPizza
8974+
uint32_t t = (uint32_t)(((((((packetUBXNAVPVT->data.year - 1970) * 365) + (((packetUBXNAVPVT->data.year - 1970) + 3) / 4)) +
8975+
DAYS_SINCE_MONTH[(packetUBXNAVPVT->data.year - 1970) & 3][packetUBXNAVPVT->data.month] +
8976+
(packetUBXNAVPVT->data.day - 1)) * 24 +
8977+
packetUBXNAVPVT->data.hour) * 60 +
8978+
packetUBXNAVPVT->data.min) * 60 +
8979+
packetUBXNAVPVT->data.sec);
8980+
return t;
8981+
}
8982+
8983+
//Get the current Unix epoch including microseconds
89588984
uint32_t SFE_UBLOX_GNSS::getUnixEpoch(uint32_t& microsecond, uint16_t maxWait)
89598985
{
89608986
if (packetUBXNAVPVT == NULL) initPacketUBXNAVPVT(); //Check that RAM has been allocated for the PVT data
89618987
if (packetUBXNAVPVT == NULL) //Bail if the RAM allocation failed
89628988
return 0;
89638989

8964-
if (packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.confirmedTime == false)
8990+
if (packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.nano == false)
89658991
getPVT(maxWait);
8966-
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.confirmedTime = false;
89678992
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.year = false;
89688993
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.month = false;
89698994
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.day = false;
@@ -8972,23 +8997,19 @@ uint32_t SFE_UBLOX_GNSS::getUnixEpoch(uint32_t& microsecond, uint16_t maxWait)
89728997
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.sec = false;
89738998
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.nano = false;
89748999
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.all = false;
8975-
uint32_t t = 0;
8976-
if((bool)packetUBXNAVPVT->data.flags2.bits.confirmedTime)
8977-
{
8978-
// assemble time elements into time_t - credits to Thomas Roell @ https://github.com/GrumpyOldPizza
8979-
t = (uint32_t)(((((((packetUBXNAVPVT->data.year - 1970) * 365) + (((packetUBXNAVPVT->data.year - 1970) + 3) / 4)) +
9000+
// assemble time elements into time_t - credits to Thomas Roell @ https://github.com/GrumpyOldPizza
9001+
uint32_t t = (uint32_t)(((((((packetUBXNAVPVT->data.year - 1970) * 365) + (((packetUBXNAVPVT->data.year - 1970) + 3) / 4)) +
89809002
DAYS_SINCE_MONTH[(packetUBXNAVPVT->data.year - 1970) & 3][packetUBXNAVPVT->data.month] +
89819003
(packetUBXNAVPVT->data.day - 1)) * 24 +
89829004
packetUBXNAVPVT->data.hour) * 60 +
89839005
packetUBXNAVPVT->data.min) * 60 +
89849006
packetUBXNAVPVT->data.sec);
8985-
int32_t us = packetUBXNAVPVT->data.nano / 1000;
8986-
microsecond = (uint32_t)us;
8987-
// adjust t if nano is negative
8988-
if(us < 0) {
8989-
microsecond = (uint32_t)(us + 1000000);
8990-
t--;
8991-
}
9007+
int32_t us = packetUBXNAVPVT->data.nano / 1000;
9008+
microsecond = (uint32_t)us;
9009+
// adjust t if nano is negative
9010+
if(us < 0) {
9011+
microsecond = (uint32_t)(us + 1000000);
9012+
t--;
89929013
}
89939014
return t;
89949015
}

Diff for: src/SparkFun_u-blox_GNSS_Arduino_Library.h

+1
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,7 @@ class SFE_UBLOX_GNSS
938938
uint8_t getSecond(uint16_t maxWait = defaultMaxWait);
939939
uint16_t getMillisecond(uint16_t maxWait = defaultMaxWait);
940940
int32_t getNanosecond(uint16_t maxWait = defaultMaxWait);
941+
uint32_t getUnixEpoch(uint16_t maxWait = defaultMaxWait);
941942
uint32_t getUnixEpoch(uint32_t& microsecond, uint16_t maxWait = defaultMaxWait);
942943

943944
bool getDateValid(uint16_t maxWait = defaultMaxWait);

0 commit comments

Comments
 (0)