48 #include <sys/timeb.h> 54 #define TIMECONV_JULIAN_DATE_START_OF_GPS_TIME (2444244.5) // [days] 55 #define TIMECONV_JULIAN_DATE_START_OF_PC_TIME (2440587.5) // [days] 56 #define TIMECONV_DAYS_IN_JAN 31 57 #define TIMECONV_DAYS_IN_MAR 31 58 #define TIMECONV_DAYS_IN_APR 30 59 #define TIMECONV_DAYS_IN_MAY 31 60 #define TIMECONV_DAYS_IN_JUN 30 61 #define TIMECONV_DAYS_IN_JUL 31 62 #define TIMECONV_DAYS_IN_AUG 31 63 #define TIMECONV_DAYS_IN_SEP 30 64 #define TIMECONV_DAYS_IN_OCT 31 65 #define TIMECONV_DAYS_IN_NOV 30 66 #define TIMECONV_DAYS_IN_DEC 31 72 const unsigned short utc_year,
73 const unsigned char utc_month,
74 const unsigned char utc_day,
75 const unsigned char utc_hour,
76 const unsigned char utc_minute,
77 const float utc_seconds
82 const unsigned short utc_year,
83 const unsigned char utc_month,
84 const unsigned char utc_day,
85 const unsigned char utc_hour,
86 const unsigned char utc_minute,
87 const float utc_seconds
90 unsigned char daysInMonth;
92 if( utc_month == 0 || utc_month > 12 )
103 if( utc_day == 0 || utc_day > daysInMonth )
113 if( utc_minute > 59 )
118 if( utc_seconds > 60 )
129 unsigned short* utc_year,
130 unsigned char* utc_month,
131 unsigned char* utc_day,
132 unsigned char* utc_hour,
133 unsigned char* utc_minute,
135 unsigned char* utc_offset,
137 unsigned short* gps_week,
144 struct _timeb timebuffer;
146 struct timeb timebuffer;
148 double timebuffer_time_in_days;
149 double timebuffer_time_in_seconds;
153 _ftime( &timebuffer );
155 ftime( &timebuffer );
161 timebuffer_time_in_seconds = timebuffer.time + timebuffer.millitm / 1000.0;
167 timebuffer_time_in_days = timebuffer_time_in_seconds/
SECONDS_IN_DAY;
186 GNSS_ERROR_MSG(
"TIMECONV_GetGPSTimeFromJulianDate returned 0." );
209 int TIMECONV_SetSystemTime(
210 const unsigned short utc_year,
211 const unsigned char utc_month,
212 const unsigned char utc_day,
213 const unsigned char utc_hour,
214 const unsigned char utc_minute,
215 const float utc_seconds
220 double julian_date = 0;
221 unsigned char day_of_week = 0;
241 GNSS_ERROR_MSG(
"TIMECONV_GetDayOfWeekFromJulianDate returned 0.");
245 t.wDayOfWeek = day_of_week;
247 t.wMonth = utc_month;
250 t.wMinute = utc_minute;
251 t.wSecond = (
WORD)(floor(utc_seconds));
252 t.wMilliseconds = (
WORD)((utc_seconds - t.wSecond)*1000);
255 result = SetSystemTime( &t );
263 const double julian_date,
264 unsigned char *day_of_week
276 if( julian_date - floor(julian_date) > 0.5 )
278 jd = (int)floor(julian_date+0.5);
282 jd = (int)floor(julian_date);
288 case 0: *day_of_week = 1;
break;
289 case 1: *day_of_week = 2;
break;
290 case 2: *day_of_week = 3;
break;
291 case 3: *day_of_week = 4;
break;
292 case 4: *day_of_week = 5;
break;
293 case 5: *day_of_week = 6;
break;
294 case 6: *day_of_week = 0;
break;
295 default:
return 0;
break;
303 const unsigned short gps_week,
304 const double gps_tow,
305 const unsigned char utc_offset,
309 if( gps_tow < 0.0 || gps_tow > 604800.0 )
322 const unsigned short utc_year,
323 const unsigned char utc_month,
324 const unsigned char utc_day,
325 const unsigned char utc_hour,
326 const unsigned char utc_minute,
327 const float utc_seconds,
354 *julian_date = (int)(365.25*y) + (int)(30.6001*(m+1.0)) + utc_day + utc_hour/24.0 + utc_minute/1440.0 + utc_seconds/86400.0 + 1720981.5;
361 const double julian_date,
362 const unsigned char utc_offset,
363 unsigned short* gps_week,
368 if( julian_date < 0.0 )
380 *gps_tow += utc_offset;
381 if( *gps_tow > SECONDS_IN_WEEK )
391 const double julian_date,
392 unsigned short* utc_year,
393 unsigned char* utc_month,
394 unsigned char* utc_day,
395 unsigned char* utc_hour,
396 unsigned char* utc_minute,
406 unsigned char minute;
407 unsigned char days_in_month = 0;
413 if( julian_date < 0.0 )
419 a = (int)(julian_date+0.5);
421 c = (int)( ((
double)b-122.1)/365.25 );
423 e = (int)( ((
double)(b-d))/30.6001 );
425 td = b - d - (int)(30.6001*e) + fmod( julian_date+0.5, 1.0 );
426 day = (
unsigned char)td;
429 hour = (
unsigned char)td;
432 minute = (
unsigned char)td;
436 month = (
unsigned char)(e - 1 - 12*(
int)(e/14));
437 year = (
unsigned short)(c - 4715 - (
int)( (7.0+(double)month) / 10.0 ));
440 if( seconds >= 60.0 )
460 if( day > days_in_month )
478 *utc_minute = minute;
479 *utc_seconds = (float)seconds;
485 unsigned short utc_year,
486 unsigned char utc_month,
487 unsigned char utc_day,
488 unsigned char utc_hour,
489 unsigned char utc_minute,
491 unsigned short* gps_week,
495 double julian_date=0.0;
496 unsigned char utc_offset=0;
517 GNSS_ERROR_MSG(
"TIMECONV_GetJulianDateFromUTCTime returned 0." );
535 GNSS_ERROR_MSG(
"TIMECONV_GetGPSTimeFromJulianDate returned 0." );
545 unsigned short utc_year,
546 unsigned char utc_month,
547 unsigned char utc_day,
548 unsigned char utc_hour,
549 unsigned char utc_minute,
551 unsigned short* gps_week,
555 double julian_date=0.0;
556 unsigned char utc_offset=0;
577 GNSS_ERROR_MSG(
"TIMECONV_GetJulianDateFromUTCTime returned 0." );
588 GNSS_ERROR_MSG(
"TIMECONV_GetGPSTimeFromJulianDate returned 0." );
598 unsigned short gps_week,
600 unsigned short* utc_year,
601 unsigned char* utc_month,
602 unsigned char* utc_day,
603 unsigned char* utc_hour,
604 unsigned char* utc_minute,
608 double julian_date = 0.0;
609 unsigned char utc_offset = 0;
613 if( gps_tow < 0.0 || gps_tow > 604800.0 )
620 for( i = 0; i < 4; i++ )
629 GNSS_ERROR_MSG(
"TIMECONV_GetJulianDateFromGPSTime returned 0." );
651 GNSS_ERROR_MSG(
"TIMECONV_GetUTCTimeFromJulianDate returned 0." );
662 unsigned char* utc_offset
665 if( julian_date < 0.0 )
671 if( julian_date < 2444786.5000 ) *utc_offset = 0;
672 else if( julian_date < 2445151.5000 ) *utc_offset = 1;
673 else if( julian_date < 2445516.5000 ) *utc_offset = 2;
674 else if( julian_date < 2446247.5000 ) *utc_offset = 3;
675 else if( julian_date < 2447161.5000 ) *utc_offset = 4;
676 else if( julian_date < 2447892.5000 ) *utc_offset = 5;
677 else if( julian_date < 2448257.5000 ) *utc_offset = 6;
678 else if( julian_date < 2448804.5000 ) *utc_offset = 7;
679 else if( julian_date < 2449169.5000 ) *utc_offset = 8;
680 else if( julian_date < 2449534.5000 ) *utc_offset = 9;
681 else if( julian_date < 2450083.5000 ) *utc_offset = 10;
682 else if( julian_date < 2450630.5000 ) *utc_offset = 11;
683 else if( julian_date < 2451179.5000 ) *utc_offset = 12;
684 else if( julian_date < 2453736.5000 ) *utc_offset = 13;
685 else *utc_offset = 14;
694 const unsigned short year,
695 const unsigned char month,
696 unsigned char* days_in_month
700 unsigned char utmp = 0;
707 case 2:
if( is_a_leapyear ){ utmp = 29; }
else{ utmp = 28; }
break;
726 *days_in_month = utmp;
736 int is_a_leap_year = 0;
741 if( (year%100) == 0 )
743 if( (year%400) == 0 )
768 const unsigned short utc_year,
769 const unsigned char utc_month,
770 const unsigned char utc_day,
771 unsigned short* dayofyear
774 unsigned char days_in_feb = 0;
785 case 1: *dayofyear = utc_day;
break;
788 case 4: *dayofyear = (
unsigned short)(62 + days_in_feb + utc_day);
break;
789 case 5: *dayofyear = (
unsigned short)(92 + days_in_feb + utc_day);
break;
790 case 6: *dayofyear = (
unsigned short)(123 + days_in_feb + utc_day);
break;
791 case 7: *dayofyear = (
unsigned short)(153 + days_in_feb + utc_day);
break;
792 case 8: *dayofyear = (
unsigned short)(184 + days_in_feb + utc_day);
break;
793 case 9: *dayofyear = (
unsigned short)(215 + days_in_feb + utc_day);
break;
794 case 10: *dayofyear = (
unsigned short)(245 + days_in_feb + utc_day);
break;
795 case 11: *dayofyear = (
unsigned short)(276 + days_in_feb + utc_day);
break;
796 case 12: *dayofyear = (
unsigned short)(306 + days_in_feb + utc_day);
break;
810 const unsigned short year,
811 const unsigned short dayofyear,
812 unsigned short* gps_week,
817 double julian_date = 0;
819 if( gps_week ==
NULL )
824 if( gps_tow ==
NULL )
829 if( dayofyear > 366 )
846 GNSS_ERROR_MSG(
"TIMECONV_GetJulianDateFromUTCTime returned 0." );
850 julian_date += dayofyear - 1;
859 GNSS_ERROR_MSG(
"TIMECONV_GetGPSTimeFromJulianDate returned 0." );
#define TIMECONV_DAYS_IN_APR
#define TIMECONV_DAYS_IN_DEC
int TIMECONV_GetDayOfWeekFromJulianDate(const double julian_date, unsigned char *day_of_week)
Computes the day of the week from the Julian date.
int TIMECONV_GetGPSTimeFromJulianDate(const double julian_date, const unsigned char utc_offset, unsigned short *gps_week, double *gps_tow)
Computes GPS time from the Julian date.
int TIMECONV_GetJulianDateFromUTCTime(const unsigned short utc_year, const unsigned char utc_month, const unsigned char utc_day, const unsigned char utc_hour, const unsigned char utc_minute, const float utc_seconds, double *julian_date)
Computes the Julian date from UTC time.
GNSS core 'c' function library: converting time information.
#define TIMECONV_DAYS_IN_JUL
int TIMECONV_DetermineUTCOffset(double julian_date, unsigned char *utc_offset)
This function is a look up table to determine the UTC offset from the Julian Date.
#define TIMECONV_DAYS_IN_NOV
#define TIMECONV_DAYS_IN_JUN
#define GNSS_ERROR_MSG(errorMsg)
int TIMECONV_GetGPSTimeFromUTCTime(unsigned short utc_year, unsigned char utc_month, unsigned char utc_day, unsigned char utc_hour, unsigned char utc_minute, float utc_seconds, unsigned short *gps_week, double *gps_tow)
Computes GPS time from UTC time.
#define TIMECONV_DAYS_IN_OCT
static int TIMECONV_IsUTCTimeValid(const unsigned short utc_year, const unsigned char utc_month, const unsigned char utc_day, const unsigned char utc_hour, const unsigned char utc_minute, const float utc_seconds)
int TIMECONV_GetSystemTime(unsigned short *utc_year, unsigned char *utc_month, unsigned char *utc_day, unsigned char *utc_hour, unsigned char *utc_minute, float *utc_seconds, unsigned char *utc_offset, double *julian_date, unsigned short *gps_week, double *gps_tow)
Obtains the UTC time, GPS time, and Julian date from PC system time.
#define TIMECONV_DAYS_IN_MAY
int TIMECONV_GetNumberOfDaysInMonth(const unsigned short year, const unsigned char month, unsigned char *days_in_month)
Determines the number of days in a month, given the month and year.
int TIMECONV_IsALeapYear(const unsigned short year)
Determines if the given year is a leap year.
int TIMECONV_GetUTCTimeFromGPSTime(unsigned short gps_week, double gps_tow, unsigned short *utc_year, unsigned char *utc_month, unsigned char *utc_day, unsigned char *utc_hour, unsigned char *utc_minute, float *utc_seconds)
Computes UTC time from GPS time.
#define TIMECONV_DAYS_IN_JAN
#define TIMECONV_JULIAN_DATE_START_OF_PC_TIME
#define TIMECONV_DAYS_IN_SEP
int TIMECONV_GetGPSTimeFromRinexTime(unsigned short utc_year, unsigned char utc_month, unsigned char utc_day, unsigned char utc_hour, unsigned char utc_minute, float utc_seconds, unsigned short *gps_week, double *gps_tow)
Computes GPS time from RINEX time. RINEX time looks like UTC but it is GPS time in year...
#define TIMECONV_DAYS_IN_AUG
#define TIMECONV_JULIAN_DATE_START_OF_GPS_TIME
#define TIMECONV_DAYS_IN_MAR
int TIMECONV_GetUTCTimeFromJulianDate(const double julian_date, unsigned short *utc_year, unsigned char *utc_month, unsigned char *utc_day, unsigned char *utc_hour, unsigned char *utc_minute, float *utc_seconds)
Computes UTC time from the Julian date.
int TIMECONV_GetGPSTimeFromYearAndDayOfYear(const unsigned short year, const unsigned short dayofyear, unsigned short *gps_week, double *gps_tow)
Determines the GPS time of the start of a day from the day of year and the year.
int TIMECONV_GetDayOfYear(const unsigned short utc_year, const unsigned char utc_month, const unsigned char utc_day, unsigned short *dayofyear)
Determines the day of year given the year, month, and day.
int TIMECONV_GetJulianDateFromGPSTime(const unsigned short gps_week, const double gps_tow, const unsigned char utc_offset, double *julian_date)
Computes the Julian date from GPS time.