|
|
View previous topic :: View next topic |
Author |
Message |
picprogrammer
Joined: 10 Sep 2003 Posts: 35
|
GMT to local time |
Posted: Wed Mar 07, 2012 9:53 am |
|
|
Does some made a routine to calculate the local time from GMT. The GMT is from a GPS receiver.
Basic it is only calculate the minute of year, plus timezone and convert back to date and time. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Wed Mar 07, 2012 10:03 am |
|
|
Your GPS doesn't have an option to change the time to 'local'? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Mar 07, 2012 10:30 am |
|
|
You should already have what is needed.
Key is that you need to calculate the 'offset' between UTC (no longer GMT), and the local time. time.h, has routines to convert a 'time', into a tick count (mktime), and back to a string (ctime). So you just take the UTC time, convert it to a 'tick' count, then add or subtract the offset, and convert it back to a string. If you do a search online, you will find dozens of examples, normally with the 'offset' called something like GMT_OFFSET. All you need to calculate this is the timezone, and a flag for DST.
Best Wishes |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Wed Mar 07, 2012 10:58 am |
|
|
If this is a stationary application (home etc), then the above answers work. If this is mobile, you have the added issue of having to periodically figure out which time zone you are in (and if DaylightSavings applies). My older Magellan hand-held gps doesn't know which time zone it is in which surprised me since it knows where it is - I would have thought they would have put that feature in, but nope. So if you are mobile (at least to the extent of crossing time zones), then you will also need to figure out how to determine which time zone you are in before you can correct for it. _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
JamesW
Joined: 23 Apr 2007 Posts: 91 Location: Rochester, England
|
|
Posted: Fri Mar 09, 2012 12:32 pm |
|
|
Funnily enough, I am doing exactly the same at the moment.
I have tried the above method, and get some weird results
My data from the satellite is reporting in perfectly, and displaying OK on the terminal - however when converting to ticks using mktime and then back to a string using ctime - I am getting an incorrect date (time is OK).
At the moment I am not introducing an offset into the ticks, to cater for local time. (But when I do - the hours adjust fine!)
It did occur to me, that maybe I needed to add 2000 to the year, as the satellite data is only 2 digits - but this crashed the PIC.
MY RESULTS (CAPTURED FROM HYPERTERMINAL) :
SATELLITE TIME : 18:24:13 09/03/12
SECONDS SINCE EPOCH 8706253
LOCAL TIME : Sat Apr 11 18:24:13 1970
Code: |
if ( (ValidPacket == 1) && (GPS.PosError == 0) )
{
printf(DebugPutc, "\r\n SATELLITE TIME : %02u:%02u:%02u %02u/%02u/%02u", GPS.SatelliteHours,
GPS.SatelliteMinutes,
GPS.SatelliteSeconds,
GPS.SatelliteDayOfMonth,
GPS.SatelliteMonth,
GPS.SatelliteYear);
// Convert time to local time
struct_tm tmUTC;
tmUTC.tm_sec = GPS.SatelliteSeconds;
tmUTC.tm_min = GPS.SatelliteMinutes;
tmUTC.tm_hour = GPS.SatelliteHours;
tmUTC.tm_mday = GPS.SatelliteDayOfMonth;
tmUTC.tm_mon = GPS.SatelliteMonth;
//tmUTC.tm_year = ( (unsigned int16) ((unsigned int16) GPS.SatelliteYear + 2000)) ;
tmUTC.tm_year = (unsigned int16) GPS.SatelliteYear;
tmUTC.tm_wday = 0;
tmUTC.tm_yday = 0;
time_t UTCticks = 0;
UTCticks = mktime(&tmUTC);
signed int32 timediff = ReadRegionTimeDifference();
GPS.LocalSecondsSinceEpoch = (UTCticks + timediff);
char tbuff[128];
printf(DebugPutc, "\r\n SECONDS SINCE EPOCH %Ld", GPS.LocalSecondsSinceEpoch);
ctime(&GPS.LocalSecondsSinceEpoch, tbuff);
printf(DebugPutc, "\r\n LOCAL TIME : %s", tbuff);
}
|
Can anyone offer any useful pointers please?
Cheers
James |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Fri Mar 09, 2012 4:32 pm |
|
|
The most likely thing is a variable is being destroyed/overwritten.
In C, It is _not_ legal to declare variables in the middle of the code. CCS does not complain about this, and sometimes seems to accept it, but most normally it results in variables that overwrite one another. Variables can be declared in a function, but _must_ be declared at the start of the function section.
Second, it is not legal in CCS, to declare a variable and initialise it from a function in the same line.
Best Wishes |
|
|
picprogrammer
Joined: 10 Sep 2003 Posts: 35
|
|
Posted: Sat Mar 10, 2012 8:41 am |
|
|
My own way was the quickest:
Code: |
static int16 month_days[2][13] = { { 0, 0, 31, 59, 90, 120, 151,
181, 212, 243, 273, 304, 334 },
{ 0, 0, 31, 60, 91, 121, 152,
182, 213, 244, 274, 305, 335 } };
/* cumulative number of days prior to beginning of month */
daynum = utc_day + month_days[0][utc_month];
/* (adjust for leap year) */
if ( ((utc_year % 4) == 0) &&(((utc_year % 100) != 0) || ((utc_year % 400) == 0) ) && (utc_month > 2) )
daynum += 1;
moy= (1440 * (int32)daynum) + (60 * (int32)utcHour) + (int32)utcMinutes;
moy += ((int16)timezone * 60); // the timezone correction
daynum = moy /1440;
minuteleft = moy- (daynum * 1440);
local_hour = minuteleft/ 60;
local_min = minuteleft - ((int16)local_hour*60);
/* Set the leap year switch */
if ( ((utc_year % 4) == 0) && ( ((utc_year % 100) != 0) || ((utc_year % 400) == 0) ) )
leap = 1;
else
leap = 0;
/* Find the month */
imon = 12;
while ( daynum <= month_days [leap][imon] )
--imon;
/* Set the month and day of month */
local_month = imon;
local_day = daynum - month_days[leap][imon];
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Mar 11, 2012 5:18 am |
|
|
Just some minor remarks.
1) You are calculating the leap year correction for number of days with two different methods. Once using two different arrays and once by adding +1.
Both methods are correct, but your code is larger and more difficult to understand because you use two methods.
2) You can save some code space by using a year calculation that is limited to the year span 1901 - 2099. Sufficient for most applications: Code: | #define IS_LEAP(year) (year%4 == 0) |
Here a link to another GPS implementation with time correction in the Code Library: http://www.ccsinfo.com/forum/viewtopic.php?t=45954 |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|