fu-hsi / FuGPS

Arduino library for parsing NMEA 0183 (GPS) sentences.
MIT License
11 stars 6 forks source link

[Days, Months, Years] Issue #8

Closed aloveric closed 4 years ago

aloveric commented 4 years ago

Could change to fix: float time = atoi(_tokens[1]); -> float time = atof(_tokens[1]);

fu-hsi commented 4 years ago

What error do you get? It is intentional. I don't support milliseconds (after optional dot), so I'm only getting an integer.

int time = atoi(time); is equivalent of: int time = (int)atof(time);

All values like hours, minutes and seconds are integer, not float.

atoi() The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many base-10 digits as possible, and interprets them as a numerical value.

http://www.cplusplus.com/reference/cstdlib/atoi/

aloveric commented 4 years ago

The GNSS chip which support more than one category satellite, data looks like: $GNGGA,190012.000,xx16.88468,N,xxx27.75270,E,1,09,1.6,30.8,M,0.0,M,,43 $GNRMC,190012.000,A,xx16.88468,N,xxx27.75270,E,0.02,0.00,280620,,,A78

So, the "time" is float, if "atoi" will have error in output.

Screen Shot 2020-06-30 at 3 27 10 PM

Btw, it is Arduino Nano w/ ATmega328P and NeoSWSerial.

fu-hsi commented 4 years ago

I know it can be float, but this is not a bug. You don't need milliseconds to calculate seconds, minutes and hours.

Take a look:

/******************************************************************************

                            Online C Compiler.
                Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char* timeString = "190012.000";
    printf("%d\n", (int)atoi(timeString));
    printf("%d\n", (int)atof(timeString));
    return 0;
}

Output:

190012 190012

Online compiler and debugger for c/c++: https://www.onlinegdb.com/online_c_compiler

No errors. I don't need floating point precision.

In the future I might consider adding a new millisecond variable (as int).

aloveric commented 4 years ago

I know the results of atoi and atof both are correct time format in this case. However, only atof function works on my Nano board with Arduino IDE 1.8.13 with unknown reason...

Thanks for your feedback :)

fu-hsi commented 4 years ago

However, only atof function works on my Nano board with Arduino IDE 1.8.13 with unknown reason...

Now I understand your problem, but without error message I can't help more now. If I have the opportunity I will check it.