File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes gps week/seconds conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "gps week/seconds conversion" Watch "gps week/seconds conversion" New topic
Author

gps week/seconds conversion

Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Hi all -

I am working on a project in java that requires me to convert the GPS week number and GPS seconds to the current day and time.

I will be glad if can some one help me with the skeleton structure of the algorithm?

Thanks in advance.
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
Have you read this?
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Hi Dimitry -

Thanks for the reply. I have read that before.
I am actually conecerned about the offset between the java time and the gps time.

Say I can get the no. of seconds since Jan 6 1980.
But how to co-relate to the java time is what I am struggling on.

Thanks for ur time.
hari
Rovas Kram
Ranch Hand

Joined: Aug 08, 2003
Posts: 135
java.util.Date stores a number that represents the number of miliseconds since January 1, 1970, 00:00:00 GMT. Now I think you can do the subtraction.
[ August 10, 2004: Message edited by: Rovas Kram ]
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
So I should get the offset and add it to the current seconds and then assign this seconds to the new date object?

say totalTime = offset time + gps seconds

assign totalTime to a new Date()? By doing this, java will give the UTC time right? which I can easily convert to current US time.

But lots of Date methods are deprecated and I think I should use a lot of Calendar method.

Please guide me with this.
Thanks
Rovas Kram
Ranch Hand

Joined: Aug 08, 2003
Posts: 135


[ August 10, 2004: Message edited by: Rovas Kram ]
[ August 10, 2004: Message edited by: Rovas Kram ]
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Heyy Rovas -

Thanks a bunch!
Your reply has helped me a lot. I will proceed further with this and will post back in case of problems.

Hari
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Hi Rovas -

I have another question.

When I instantiate a Date object saying

Date javaDate = new Date(diff + long_gps);

does 'diff + long_gps' indicate the time that elapsed after 1970 ?

I am getting absured results.

Thanks again,
hari
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
I am getting absured results.

Make sure that you have converted your GPS seconds to milliseconds before adding the offset.

By doing this, java will give the UTC time right?

Not exactly. It will give you your "Java time", which is real UTC + a few "leap seconds" (13 to be precise)

I have no idea if having real UTC is important for your application, just want you to be aware of the issue.
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Hi -

This is the code that I wrote. Ready to compile. I hope it is clear.

The gps week = 1283 which is the current week.
The gps sec = 229015 is the no. of seconds elapsed this week.

These numbers should give me the date and time as
Aug-10-2004 15:37:55 [i dont worry about the seconds] Thanks,


Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
Replace int week=1283; //gps week with long week=1283; //gps week

Add gc1.setTimeZone(new SimpleTimeZone(0,"")); before obtaining time from gc1
[ August 10, 2004: Message edited by: Dmitry Melnik ]
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Thanks Dimitry. It works and gives the current time (2:49:25). But how do I know if its AM or PM? I think the hours are 0-23. In which case it should give me 14:49:25.

Also, will this algorithm take the leap seconds into account?

I am concerned about the performance issues in the real time project.
[ August 10, 2004: Message edited by: Hari priya ]
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
But how do I know if its AM or PM? I think the hours are 0-23. In which case it should give me 14:49:25.

Use c.get(Calendar.AM_PM)==Calendar.AM to find out

Also, will this algorithm take the leap seconds into account?

It will not. I'd suggest you to make sure if those seconds are important for your project, before you dig any deeper with them.

I am concerned about the performance issues in the real time project.

You do not need to compute the offset every time you do conversion, it's not going to change Compute it once, and keep the offset as a static member in your class. All arithmetic on constants will be done at compile time. Other than that the conversion takes just 2 additions and 2 multiplications.
[ August 10, 2004: Message edited by: Dmitry Melnik ]
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
thanks Dimitry and Rovas for your valuable suggestions
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Hi -

So far this is working fine (not bothering about the leap seconds).

Hi -

I think I have the necessity to format date and time now.

I can use SimpleDateFormat the date and time but I guess I will be getting a string in in each case. I need to insert these values in the data base with the 'date' and 'time' fields (or just one field 'timestamp' which has both date,time and time zone ).

The requirement is I should be able to query the latest record using time and date. So I am wondering how to do that.

Thanks all of you!
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
The requirement is I should be able to query the latest record using time and date.

I'd store "timestamp" in the DB then.
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
The simpleDateFormat gives me the wrong month [2004-49-11] while it is supposed to give me [2004-8-11]. Could you please tell me what the problem is? Also I will be glad if there is a way to display time in 0-23 hr instead of am or pm. Thank you!

Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
The simpleDateFormat gives me the wrong month [2004-49-11] while it is supposed to give me [2004-8-11]. Could you please tell me what the problem is? Also I will be glad if there is a way to display time in 0-23 hr instead of am or pm.

It gives you the correct number of minutes, rather than a month.
I'd suggest you to read this carefully http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Thanks a bunch Dmitry for your patience.
Its fine now. I just have to work on the database. That was a silly mistake
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
Good luck. And pay attention to the way you handle timezone info (I did not like that spot in your code)
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Heyy -

I am using
SimpleDateFormat formatDate = new SimpleDateFormat( "yyyy-mm-dd HH:mm:ss" );

to get the date and time information. How do I insert this in the database table in the timestamp field?

I tried

String query = "INSERT into Test values ("+formatDate+")";

But it gives error. Thanks,
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
I am using
SimpleDateFormat formatDate = new SimpleDateFormat( "yyyy-mm-dd HH:mm:ss" );


Aren't you placing minutes rather than month again? Be careful and read that SimpleDateFormat class documantation.
Should not it be "yyyy-MM-dd HH:mm:ss" ?

String query = "INSERT into Test values ("+formatDate+")";

Is your SQQL syntax correct? Have you tried anything like this?
INSERT into Test (your_column_name) values ("+formatDate+")

Or even better:



It would save you from having to format the date.
[ August 11, 2004: Message edited by: Dmitry Melnik ]
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
I am using yyyy-MM-dd not mm

anyways I tried insert into test (col_name) values ....

i also tried these -
String query = "INSERT into Test values (timestamp ' 2004-8-11 9:55:06' )";

inserts the value in the timestamp field. It works when hardcoded.

But when I provide the string it doesn't work.

String query = "INSERT into Test values (timestamp ' "+formatDate+" ' )";

I am really struggling to get this work
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
But when I provide the string it doesn't work.

How? Does it give you any error message or exception?
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
It doesnt give an error. It either doesn't insert anything
or just inserts
0000-00-00 00:00:00 (usually inserts 00000)
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
What did the error message say?
What DB are you using, BTW?
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
No error msgs at all..
I am using mySQL
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Hi Dmitry -

I works now. Thanks for getting back to me when I had problems

Regards
Hari
Hari priya
Ranch Hand

Joined: Mar 11, 2004
Posts: 134
Hello -

I am having one more question regarding this.
Can any one tell me if the algorithm I wrote(included in the previous msg) takes care of the daylight savings ?
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
Can any one tell me if the algorithm I wrote(included in the previous msg) takes care of the daylight savings ?

The algorithm that you wrote converts GPS week/time to "Java-UTC time" (which is true UTC + leap seconds). All the conversion to any local time is being done by relevant JDK classes/methods (Date, Calendar, etc). Read class documentation for details on handling timezones and daylight savings.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: gps week/seconds conversion