deanbrowne / j4me

Automatically exported from code.google.com/p/j4me
1 stars 1 forks source link

The time field of a bluetooth GPS is parsed wrongly #45

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Connect a bluetooth GPS.
2. Print the locations time.

According to the comments in the code and variable name the time field in
the NMEA is seconds since midnight, but according to the NMEA
specifications the field is of format hhmmss.sss. See NMEA Reference
Manual--January 2005 for Sirf, table 1-3.

I suggest to use the implementation below (BluetoothGPS.java):

    public static long convertUTCTime (String date, String time)
    {
        if ( (date == null) || (time == null) )
        {
            return System.currentTimeMillis();
        }

      // Parse the date.
      int hour = Integer.parseInt( time.substring(0, 2) );
      int minute = Integer.parseInt( time.substring(2, 4) );
      int millisecond = (int) (Double.parseDouble( time.substring(4) )*1000);
      // Parse the date.
        int day = Integer.parseInt( date.substring(0, 2) );
        int month = Integer.parseInt( date.substring(2, 4) );
        int year = Integer.parseInt( date.substring(4, 6) );

      Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
      cal.set(Calendar.YEAR, 2000 + year);
      cal.set(Calendar.MONTH, month-1);
      cal.set(Calendar.DAY_OF_MONTH, day);
      cal.set(Calendar.HOUR_OF_DAY, hour);
      cal.set(Calendar.MINUTE, minute);
      cal.set(Calendar.SECOND, millisecond/1000);
      cal.set(Calendar.MILLISECOND, millisecond%1000);
      return cal.getTime().getTime();   
    }

Original issue reported on code.google.com by peter.ro...@gmail.com on 7 Aug 2008 at 7:17