gps week sec shift peking time

typedef struct {        /* time struct */
    time_t time;        /* time (s) expressed by standard time_t */
    double sec;         /* fraction of second under 1 s */
} gtime_t;


const static double gpst0[]={1980,1, 6,0,0,0}; /* gps time reference */

extern gtime_t epoch2time(const double *ep)
{
    const int doy[]={1,32,60,91,121,152,182,213,244,274,305,335};
    gtime_t time={0};
    int days,sec,year=(int)ep[0],mon=(int)ep[1],day=(int)ep[2];
    
    if (year<1970||2099<year||mon<1||12<mon) return time;
    
    /* leap year if year%4==0 in 1901-2099 */
 days=(year-1970)*365+(year-1969)/4+doy[mon-1]+day-2+(year%4==0&&mon>=3?1:0);
    sec=(int)floor(ep[5]);
    time.time=(time_t)days*86400+(int)ep[3]*3600+(int)ep[4]*60+sec;
    time.sec=ep[5]-sec;
    return time;
}


static void Time2Selfstr(gtime_t t,u1 *ymd,u1 *hour,u1 *min,u1 *sec,u4 n)
{
    double ep[6];

    if (n<0) n=0; else if (n>12) n=12;
    if (1.0-t.sec<0.5/pow(10.0,n)) {t.time++; t.sec=0.0;};
    time2epoch(t,ep);
    sprintf(ymd,"%04.0f%02.0f%02.0f",ep[0],ep[1],ep[2]);
    //sprintf(hour,"%02.0f",(ep[3]+8) >= 25 ? (ep[3]+8-24) : (ep[3]+8));//beijing time xg@2017-9-29
    sprintf(hour,"%02.0f",ep[3]);
    sprintf(min,"%02.0f",ep[4]);
    sprintf(sec,"%0*.*f",n <= 0 ? 2 : n + 3, n <= 0 ? 0 : n, ep[5]);
}
/*
gps time to peking time
*/
extern gtime_t gpst2pktime(int week, double sec)
{
    gtime_t t=epoch2time(gpst0);
    
    if (sec<-1E9||1E9<sec) sec=0.0;
    t.time+=86400*7*week+(int)sec+28800;
    t.sec=sec-(int)sec;
    return t;
}



void Get_LocalTime(u1 *buff,int len,u2 type)
{
 	gtime_t time;
	u2 week = 0;
	u4 sec = 0;
    char ymd[10];
    char hour[10];
    char min[10];
    char zsec[10];

    if(type == LOG_RANGE)
    {
		memcpy(&week,buff+14,2);//all is gps time about rangeb
		memcpy(&sec,buff+16,4);
		memset(ymd,0,sizeof(ymd));
        memset(hour,0,sizeof(hour));
        memset(min,0,sizeof(min));
        sec = sec * 0.001;
		time = gpst2pktime(week,sec);
		Time2Selfstr(time,ymd,hour,min,zsec,3);
     if(f_live == 0)//when minutes is 00
        {
            if(atoi(min) == 0 && atof(zsec) == 0.0) //when time is 00.00
                f_live = 3;//enable create a new file to store

            if((atoi(zsec)!=f_timekeeping) && (atoi(zsec)%SYNCTIME==0))
            {
                sync();//sync once -store data
            }
            f_timekeeping = atoi(zsec);
        }
   if(atoi(ymd)>19800106 && f_live == 3) //when the app is started at first or when minutes is 00
        {
            memcpy(f_ymd,ymd,8);
            memcpy(f_hour,hour,2);
            memcpy(f_min,min,2);
            memcpy(f_sec,zsec,10);
            f_live = 0;
        }
    }
}

關鍵點在於北京時間比utc時間晚8個小時,也就是8*60*60=28800s,因此若是用utc時間算北京時間的話還須要加上28800s這個差值,整體來講,都是用s換算成年月日時分秒的。express

相關文章
相關標籤/搜索