This is an attempt to convert mb_system's poorly defined time_i to adjusted GPS standard time (GPS standard time offset by 1.0E9) Most of this is from converting struct tm (assuming UTC) to time_t from Eric Raymond.
| 171 | // Most of this is from converting struct tm (assuming UTC) to time_t |
| 172 | // from Eric Raymond. |
| 173 | double timeconvert(int time_i[7]) |
| 174 | { |
| 175 | const uint8_t yearIdx = 0; |
| 176 | const uint8_t monthIdx = 1; |
| 177 | const uint8_t dayIdx = 2; |
| 178 | const uint8_t hourIdx = 3; |
| 179 | const uint8_t minuteIdx = 4; |
| 180 | const uint8_t secondIdx = 5; |
| 181 | const uint8_t microsecIdx = 6; |
| 182 | const uint8_t monthsPerYear = 12; |
| 183 | const int cumdays[monthsPerYear] = |
| 184 | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; |
| 185 | |
| 186 | long year; |
| 187 | time_t result; |
| 188 | |
| 189 | year = 1900 + time_i[yearIdx] + time_i[monthIdx] / monthsPerYear; |
| 190 | result = (year - 1970) * 365 + cumdays[time_i[monthIdx] % monthsPerYear]; |
| 191 | result += (year - 1968) / 4; |
| 192 | result -= (year - 1900) / 100; |
| 193 | result += (year - 1600) / 400; |
| 194 | if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0) && |
| 195 | (time_i[monthIdx] % monthsPerYear) < 2) |
| 196 | result--; |
| 197 | result += time_i[dayIdx] - 1; |
| 198 | result *= 24; |
| 199 | result += time_i[hourIdx]; |
| 200 | result *= 60; |
| 201 | result += time_i[minuteIdx]; |
| 202 | result *= 60; |
| 203 | result += time_i[secondIdx]; |
| 204 | /** |
| 205 | if (t->tm_isdst == 1) |
| 206 | result -= 3600; |
| 207 | **/ |
| 208 | // We should now have time_t in result. Adjust offset to GPS standard |
| 209 | // 1/1/1970 to 6/1/1980 in seconds. Perhaps cleaner than messing with |
| 210 | // the calculations above which are known to work. |
| 211 | result -= 315964800; |
| 212 | // Now subtract seconds for each leap second that has happened between |
| 213 | // the GPS time root. These are "announced", so we can't really plan here. |
| 214 | result -= leapSeconds(time_i[yearIdx], (time_i[monthIdx] / 12) + 1); |
| 215 | // Now subtract for "adjusted" GPS standard time. |
| 216 | result -= 1000000000UL; |
| 217 | // Add back the microseconds as a fraction of a second and convert |
| 218 | // to double. |
| 219 | return result + (time_i[microsecIdx] / 1000000.0); |
| 220 | } |
| 221 | |
| 222 | } // namespace |
| 223 |