Changeset 1247
- Timestamp:
- Jul 20, 2004, 3:29:33 PM (22 years ago)
- Location:
- trunk/psLib/src
- Files:
-
- 4 edited
-
astro/psTime.c (modified) (6 diffs)
-
astro/psTime.h (modified) (9 diffs)
-
astronomy/psTime.c (modified) (6 diffs)
-
astronomy/psTime.h (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/astro/psTime.c
r1223 r1247 12 12 * @author Ross Harman, MHPCC 13 13 * 14 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $15 * @date $Date: 2004-07- 15 19:02:13 $14 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2004-07-21 01:29:33 $ 16 16 * 17 17 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 24 24 25 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 26 28 27 29 #include "psTime.h" 28 30 #include "psError.h" 31 #define PS_ALLOW_MALLOC 32 #include "psMemory.h" 29 33 30 34 /******************************************************************************/ … … 32 36 /******************************************************************************/ 33 37 34 // Number of available leapsecond updates 35 #define NUM_LEAPSECOND_UPDATES 23 38 /** Preprocessor macro to generate error for negative time in struct */ 39 #define CHECK_NEGATIVE_TIME_STRUCT(NAME,RETURN) \ 40 if(NAME.tv_sec < 0) { \ 41 psError(__func__,"Negative seconds are not allowed: %ld", NAME.tv_sec); \ 42 return RETURN; \ 43 } else if(NAME.tv_usec<0) { \ 44 psError(__func__,"Negative microseconds are not allowed: %ld", NAME.tv_usec); \ 45 return RETURN; \ 46 } 47 48 /** Preprocessor macro to generate error for negative time */ 49 #define CHECK_NEGATIVE_TIME(NAME,RETURN) \ 50 if(NAME < 0.0) { \ 51 psError(__func__,"Negative time is not allowed: %f", NAME); \ 52 return RETURN; \ 53 } \ 36 54 37 55 /******************************************************************************/ … … 55 73 the psTimeGetTime() function to calculate TAI time from UTC time. 56 74 57 1972 JAN 1 =JD 2441317.5 TAI-UTC=10.058 1972 JUL 1 =JD 2441499.5 TAI-UTC=11.059 1973 JAN 1 =JD 2441683.5 TAI-UTC=12.060 1974 JAN 1 =JD 2442048.5 TAI-UTC=13.061 1975 JAN 1 =JD 2442413.5 TAI-UTC=14.062 1976 JAN 1 =JD 2442778.5 TAI-UTC=15.063 1977 JAN 1 =JD 2443144.5 TAI-UTC=16.064 1978 JAN 1 =JD 2443509.5 TAI-UTC=17.065 1979 JAN 1 =JD 2443874.5 TAI-UTC=18.066 1980 JAN 1 =JD 2444239.5 TAI-UTC=19.067 1981 JUL 1 =JD 2444786.5 TAI-UTC=20.068 1982 JUL 1 =JD 2445151.5 TAI-UTC=21.069 1983 JUL 1 =JD 2445516.5 TAI-UTC=22.070 1985 JUL 1 =JD 2446247.5 TAI-UTC=23.071 1988 JAN 1 =JD 2447161.5 TAI-UTC=24.072 1990 JAN 1 =JD 2447892.5 TAI-UTC=25.073 1991 JAN 1 =JD 2448257.5 TAI-UTC=26.074 1992 JUL 1 =JD 2448804.5 TAI-UTC=27.075 1993 JUL 1 =JD 2449169.5 TAI-UTC=28.076 1994 JUL 1 =JD 2449534.5 TAI-UTC=29.077 1996 JAN 1 =JD 2450083.5 TAI-UTC=30.078 1997 JUL 1 =JD 2450630.5 TAI-UTC=31.079 1999 JAN 1 =JD 2451179.5 TAI-UTC=32.075 1972 JAN 1 = JD 2441317.5 TAI-UTC = 10.0 76 1972 JUL 1 = JD 2441499.5 TAI-UTC = 11.0 77 1973 JAN 1 = JD 2441683.5 TAI-UTC = 12.0 78 1974 JAN 1 = JD 2442048.5 TAI-UTC = 13.0 79 1975 JAN 1 = JD 2442413.5 TAI-UTC = 14.0 80 1976 JAN 1 = JD 2442778.5 TAI-UTC = 15.0 81 1977 JAN 1 = JD 2443144.5 TAI-UTC = 16.0 82 1978 JAN 1 = JD 2443509.5 TAI-UTC = 17.0 83 1979 JAN 1 = JD 2443874.5 TAI-UTC = 18.0 84 1980 JAN 1 = JD 2444239.5 TAI-UTC = 19.0 85 1981 JUL 1 = JD 2444786.5 TAI-UTC = 20.0 86 1982 JUL 1 = JD 2445151.5 TAI-UTC = 21.0 87 1983 JUL 1 = JD 2445516.5 TAI-UTC = 22.0 88 1985 JUL 1 = JD 2446247.5 TAI-UTC = 23.0 89 1988 JAN 1 = JD 2447161.5 TAI-UTC = 24.0 90 1990 JAN 1 = JD 2447892.5 TAI-UTC = 25.0 91 1991 JAN 1 = JD 2448257.5 TAI-UTC = 26.0 92 1992 JUL 1 = JD 2448804.5 TAI-UTC = 27.0 93 1993 JUL 1 = JD 2449169.5 TAI-UTC = 28.0 94 1994 JUL 1 = JD 2449534.5 TAI-UTC = 29.0 95 1996 JAN 1 = JD 2450083.5 TAI-UTC = 30.0 96 1997 JUL 1 = JD 2450630.5 TAI-UTC = 31.0 97 1999 JAN 1 = JD 2451179.5 TAI-UTC = 32.0 80 98 */ 81 99 82 // Julian date of leapsecond update and current total number of leapseconds forthat date100 // Table for Julian date of leapsecond update and current total number of leapseconds at that date 83 101 static double leapseconds[NUM_LEAPSECOND_UPDATES][2] = 84 102 { … … 135 153 time.tv_usec = now.tv_usec; 136 154 137 // Add last known leapsecondsto UTC time to get TAI time155 // Add most current leapseconds value to UTC time to get TAI time 138 156 time.tv_sec += leapseconds[NUM_LEAPSECOND_UPDATES][1]; 139 157 … … 143 161 char* psTimeToISO(psTime time) 144 162 { 145 struct tm *tm; 146 tm = localtime (&time.tv_sec); 147 printf("%d\n", tm->tm_sec); 148 return NULL; 149 } 163 int ms = 0; 164 char *timeString = NULL; 165 char *tempString = NULL; 166 struct tm *tmTime = NULL; 167 168 CHECK_NEGATIVE_TIME_STRUCT(time,NULL); 169 170 tempString = psAlloc(MAX_TIME_STRING_LENGTH); 171 timeString = psAlloc(MAX_TIME_STRING_LENGTH); 172 173 // Converts psTime to YYYY/MM/DD,HH:MM:SS.SSS in string form 174 ms = time.tv_usec/1000; 175 tmTime = gmtime(&time.tv_sec); 176 if(!strftime(tempString, MAX_TIME_STRING_LENGTH, "%Y/%m/%d,%H:%M:%S", tmTime)) { 177 psError(__func__, " : Line %d - Failed strftime conversion", __LINE__); 178 } 179 180 if(snprintf(timeString,MAX_TIME_STRING_LENGTH,"%s.%3.3d", tempString, ms) < 0) { 181 psError(__func__, " : Line %d - Failed snprintf conversion", __LINE__); 182 } 183 psFree(tempString); 184 free(tmTime); 185 186 return timeString; 187 } 188 189 psTime psTimeToUTC(psTime time) 190 { 191 int i = 0; 192 double jd = 0.0; 193 double ls = 0.0; 194 double *jdTable = NULL; 195 psTime outTime; 196 197 CHECK_NEGATIVE_TIME_STRUCT(time,outTime); 198 199 // Find leapseconds to subtract from psTime to get UTC time 200 jd = psTimeToJD(time); 201 jdTable = leapseconds[0]; 202 for(i=0; i<NUM_LEAPSECOND_UPDATES; i++, jdTable+=2) { 203 if(jd > *jdTable) { 204 ls = *(jdTable + 1); 205 } 206 } 207 208 outTime.tv_sec = time.tv_sec - ls; 209 outTime.tv_usec = time.tv_usec; 210 211 CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime); 212 213 return outTime; 214 } 215 216 double psTimeToMJD(psTime time) 217 { 218 double mjd = 0.0; 219 220 CHECK_NEGATIVE_TIME_STRUCT(time,mjd); 221 222 // Modified Julian date conversion courtesy of Eugene Magnier 223 mjd = time.tv_sec/SEC_PER_DAY + time.tv_usec/USEC_PER_DAY + 40587.0; 224 225 return mjd; 226 } 227 228 double psTimeToJD(psTime time) 229 { 230 double jd = 0.0; 231 232 CHECK_NEGATIVE_TIME_STRUCT(time,jd); 233 234 // Julian date conversion courtesy of Eugene Magnier 235 jd = time.tv_sec/SEC_PER_DAY + time.tv_usec/USEC_PER_DAY + 2440587.5; 236 237 return jd; 238 } 239 240 struct timeval psTimeToTimeval(psTime time) 241 { 242 struct timeval timevalTime; 243 244 CHECK_NEGATIVE_TIME_STRUCT(time,timevalTime); 245 timevalTime.tv_sec = time.tv_sec; 246 timevalTime.tv_usec = time.tv_usec; 247 248 return timevalTime; 249 } 250 251 struct tm* psTimeToTM(psTime time) 252 { 253 struct tm *tmTime = NULL; 254 255 CHECK_NEGATIVE_TIME_STRUCT(time,tmTime); 256 tmTime = gmtime(&time.tv_sec); 257 258 return tmTime; 259 } 260 261 psTime psISOToTime(char *time) 262 { 263 char tempString[MAX_TIME_STRING_LENGTH]; 264 int month; 265 int day; 266 int year; 267 int hour; 268 int minute; 269 int second; 270 int millisecond; 271 struct tm tmTime; 272 psTime outTime; 273 274 strncpy(tempString, time, MAX_TIME_STRING_LENGTH); 275 276 // Convert YYYY/MM/DD,HH:MM:SS.SSS in string form to tm time 277 year = atoi(strtok(tempString, "/")); 278 if(year < 0) { 279 psError(__func__,"Years less than 1900 not allowed. Value: %d", year); 280 } 281 282 month = atoi(strtok(tempString, "/")); 283 if(month<1 || month>12) { 284 psError(__func__,"Month must have a value from 0 to 11. Value: %d", month); 285 } 286 287 day = atoi(strtok(tempString, ",")); 288 if(day<1 || day>31) { 289 psError(__func__,"Day must have a value from 1 to 31. Value: %d", day); 290 } 291 292 hour = atoi(strtok(tempString, ":")); 293 if(hour<0 || hour>23) { 294 psError(__func__,"Hour must have a value from 0 to 23. Value: %d", hour); 295 } 296 297 second = atoi(strtok(tempString, ".")); 298 if(second<0 || second>60) { 299 psError(__func__,"Second must have a value from 0 to 59. Value: %d", second); 300 } 301 302 millisecond = atoi(strtok(tempString, "X")); 303 if(millisecond<0 || millisecond>1000) { 304 psError(__func__,"Second must have a value from 0 to 999. Value: %d", millisecond); 305 } 306 307 tmTime.tm_year = year - 1900; 308 tmTime.tm_mon = month - 1; 309 tmTime.tm_mday = day; 310 tmTime.tm_hour = hour; 311 tmTime.tm_min = minute; 312 tmTime.tm_sec = second; 313 314 // Convert tm time to psTime 315 outTime = psTMToTime(&tmTime); 316 outTime.tv_usec = millisecond*1000; 317 318 return outTime; 319 } 320 321 psTime psUTCToTime(psTime time) 322 { 323 psTime outTime; 324 325 CHECK_NEGATIVE_TIME_STRUCT(time,outTime); 326 327 // Convert UTC time to psTime/TAI 328 outTime.tv_sec = time.tv_sec + leapseconds[NUM_LEAPSECOND_UPDATES][1]; 329 ; 330 outTime.tv_usec = time.tv_usec; 331 CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime); 332 333 return time; 334 } 335 336 psTime psMJDToTime(double time) 337 { 338 psTime outTime; 339 double days = 0.0; 340 double seconds = 0.0; 341 342 CHECK_NEGATIVE_TIME(time,outTime); 343 344 // Modified Julian date conversion courtesy of Eugene Magnier 345 days = time - 40587.0; 346 347 // Convert to psTime/TAI 348 seconds = days*SEC_PER_DAY; 349 outTime.tv_usec = (seconds -(long)seconds)*1000000.0; 350 outTime.tv_sec = seconds; 351 352 CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime); 353 354 return outTime; 355 } 356 357 psTime psJDToTime(double time) 358 { 359 double days = 0.0; 360 double seconds = 0.0; 361 psTime outTime; 362 363 CHECK_NEGATIVE_TIME(time,outTime); 364 365 // Julian date conversion courtesy of Eugene Magnier 366 days = time - 2440587.5; 367 368 // Convert to psTime/TAI 369 seconds = days*SEC_PER_DAY; 370 outTime.tv_sec = seconds; 371 outTime.tv_usec = (seconds -(long)seconds)*1000000.0; 372 373 CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime); 374 375 return outTime; 376 } 377 378 psTime psTimevalToTime(struct timeval *time) 379 { 380 psTime outTime; 381 if(time == NULL) 382 { 383 psError(__func__,"Null value for timeval arg not allowed"); 384 return outTime; 385 } else if(time->tv_sec < 0) 386 { 387 psError(__func__,"Negative seconds are not allowed: %ld", time->tv_sec); 388 return outTime; 389 } else if(time->tv_usec<0) 390 { 391 psError(__func__,"Negative microseconds are not allowed: %ld", time->tv_usec); 392 return outTime; 393 } 394 395 // Convert to psTime/TAI 396 outTime.tv_sec = time->tv_sec; 397 outTime.tv_usec = time->tv_usec; 398 399 CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime); 400 401 return outTime; 402 } 403 404 405 psTime psTMToTime(struct tm *time) 406 { 407 int i; 408 int m; 409 int n; 410 int y; 411 int mon [] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 412 long epoch; 413 psTime outTime; 414 415 i= 0; 416 m = 0; 417 n = 0; 418 y = 0; 419 epoch = 0; 420 421 if(time == NULL) 422 { 423 psError(__func__,"Null value for tm arg not allowed"); 424 return outTime; 425 } else if(time->tm_year < 70) 426 { 427 psError(__func__,"Input times earlier than 1970 not allowed"); 428 return outTime; 429 } 430 431 n = time->tm_year + 1900 - 1; 432 epoch = (time->tm_year - 70) * SEC_PER_YEAR + ((n/4 - n/100 + n/400) - 433 (1969/4 - 1969/100 + 1969/400)) * SEC_PER_DAY; 434 435 y = time->tm_year + 1900; 436 437 for(i = 0; i < time->tm_mon; i++) 438 { 439 epoch += mon [m] * SEC_PER_DAY; 440 if(m == 1 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) { 441 epoch += SEC_PER_DAY; 442 } 443 444 if(++m > 11) { 445 m = 0; 446 y++; 447 } 448 } 449 450 epoch += (time->tm_mday - 1) * SEC_PER_DAY; 451 epoch += time->tm_hour *SEC_PER_HOUR + time->tm_min * SEC_PER_MINUTE + time->tm_sec; 452 453 outTime.tv_usec = 0; 454 outTime.tv_sec = epoch; 455 456 return outTime; 457 } -
trunk/psLib/src/astro/psTime.h
r1223 r1247 12 12 * @author Ross Harman, MHPCC 13 13 * 14 * @version $Revision: 1. 3$ $Name: not supported by cvs2svn $15 * @date $Date: 2004-07- 15 19:02:13$14 * @version $Revision: 1.4 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2004-07-21 01:29:22 $ 16 16 * 17 17 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 29 29 /// @addtogroup Time 30 30 /// @{ 31 32 /******************************************************************************/ 33 /* DEFINE STATEMENTS */ 34 /******************************************************************************/ 35 36 /** Number of available leapsecond updates */ 37 #define NUM_LEAPSECOND_UPDATES 23 38 39 /** Maximum length of time string */ 40 #define MAX_TIME_STRING_LENGTH 256 41 42 /** Seconds per minute */ 43 #define SEC_PER_MINUTE 60 44 45 /** Seconds per hour */ 46 #define SEC_PER_HOUR 60*SEC_PER_MINUTE 47 48 /** Seconds per day */ 49 #define SEC_PER_DAY 24*SEC_PER_HOUR 50 51 /** Seconds per year */ 52 #define SEC_PER_YEAR 365*SEC_PER_DAY 53 54 /** Microseconds per day */ 55 #define USEC_PER_DAY 86400000000.0 31 56 32 57 /******************************************************************************/ … … 63 88 /** Convert psTime to ISO time in TAI units. 64 89 * 65 * Converts psTime to a null terminated string in the form of: YYYY/MM/DD,HH:MM:SS.SSS. Th e result from this66 * function is in TAI units.67 * 68 * @return char*: Pointer null terminated array of chars in ISO /TAItime.90 * Converts psTime to a null terminated string in the form of: YYYY/MM/DD,HH:MM:SS.SSS. This function assumes 91 * the input time already is in TAI time and does not add or subtract leapseconds. 92 * 93 * @return char*: Pointer null terminated array of chars in ISO time. 69 94 */ 70 95 char* psTimeToISO( … … 74 99 /** Convert psTime to UTC time. 75 100 * 76 * Converts psTime to UTC time in double precision floating point notation. The result from this function is 77 * not in TAI units, but that of UTC, which does not contain leapseconds. 78 * 79 * @return double: UTC time in floating point notation. 80 */ 81 double psTimeToUTC( 101 * Converts psTime to UTC time in double precision floating point notation. The input to this must already be 102 * int TAI time. The result from this function is not in TAI units, but that of UTC, which does not contain 103 * leapseconds. 104 * 105 * @return psTime: UTC time psTime format. 106 */ 107 psTime psTimeToUTC( 82 108 psTime time /** Input time to be converted. */ 83 109 ); … … 85 111 /** Convert psTime to modified Julian date time. 86 112 * 87 * Converts psTime to modified Julian date (MJD) time. The result from this function is in TAI units. 88 * 89 * @return double: MJD/TAI time in floating point notation. 113 * Converts psTime to modified Julian date (MJD) time. This function assumes the input time already is in TAI 114 * time and does not add or subtract leapseconds. 115 * 116 * @return double: Modified Julian Days (MJD) time. 90 117 */ 91 118 double psTimeToMJD( … … 95 122 /** Convert psTime to Julian date time. 96 123 * 97 * Converts psTime to Julian date (JD) time. The result from this function is in TAI units. 98 * 99 * @return double: JD/TAI time in floating point notation. 124 * Converts psTime to Julian date (JD) time. This function assumes the input time already is in TAI time and 125 * does not add or subtract leapseconds. 126 * 127 * @return double: Julian Date (JD) time. 100 128 */ 101 129 double psTimeToJD( … … 105 133 /** Convert psTime to timeval time. 106 134 * 107 * Converts psTime to timeval time. The result from this function is in TAI units. 108 * 109 * @return timeval: timeval/TAI time. 135 * Converts psTime to timeval time. This function assumes the input time already is in TAI time and does not 136 * add or subtract leapseconds. 137 * 138 * @return timeval: timeval struct time. 110 139 */ 111 140 struct timeval psTimeToTimeval( … … 115 144 /** Convert psTime to tm time. 116 145 * 117 * Converts psTime to tm time. The result from this function is in TAI units. 118 * 119 * @return tm: tm/TAI time. 146 * Converts psTime to tm time. This function assumes the input time already is in TAI time and does not add or 147 * subtract leapseconds. 148 * 149 * @return tm: tm struct time. 120 150 */ 121 151 struct tm* psTimeToTM( … … 125 155 /** Convert ISO to psTime. 126 156 * 127 * Converts ISO time to psTime. The result from this function is in TAI units. 157 * Converts ISO time to psTime. This function assumes the input time already is in TAI time and does not add 158 * or subtract leapseconds. 159 * 160 * @return psTime: time 161 */ 162 psTime psISOToTime( 163 char *time /** Input time to be converted. */ 164 ); 165 166 /** Convert UTC to psTime. 167 * 168 * Converts UTC time to psTime. This function assumes the input time already is in TAI time and add or 169 * subtracts the necessary leapseconds. 128 170 * 129 171 * @return psTime: time in TAI units. 130 172 */ 131 psTime psISOToTime(char *time); 132 133 /** Convert UTC to psTime. 134 * 135 * Converts UTC time to psTime. The result from this function is in TAI units. 136 * 137 * @return psTime: time in TAI units. 138 */ 139 psTime psUTCToTime(double time); 173 psTime psUTCToTime( 174 psTime time /** Input time to be converted. */ 175 ); 140 176 141 177 /** Convert MJD to psTime. 142 178 * 143 * Converts MJD time to psTime. The result from this function is in TAI units. 144 * 145 * @return psTime: time in TAI units. 146 */ 147 psTime psMJDToTime(double time); 179 * Converts MJD time to psTime. This function assumes the input time already is in TAI time and does not add 180 * or subtract leapseconds. 181 * 182 * @return psTime: time. 183 */ 184 psTime psMJDToTime( 185 double time /** Input time to be converted. */ 186 ); 148 187 149 188 /** Convert JD to psTime. 150 189 * 151 * Converts JD time to psTime. The result from this function is in TAI units. 152 * 153 * @return psTime: time in TAI units. 154 */ 155 psTime psJDToTime(double time); 190 * Converts JD time to psTime. This function assumes the input time already is in TAI time and does not add 191 * or subtract leapseconds. 192 * 193 * @return psTime: time. 194 */ 195 psTime psJDToTime( 196 double time /** Input time to be converted. */ 197 ); 156 198 157 199 /** Convert timeval to psTime. 158 200 * 159 * Converts timeval time to psTime. The result from this function is in TAI units. 160 * 161 * @return psTime: time in TAI units. 162 */ 163 psTime psTimevalToTime(struct timeval *time); 201 * Converts timeval time to psTime. This function assumes the input time already is in TAI time and does not 202 * add or subtract leapseconds. 203 * 204 * @return psTime: time. 205 */ 206 psTime psTimevalToTime( 207 struct timeval *time /** Input time to be converted. */ 208 ); 164 209 165 210 /** Convert tm time to psTime. 166 211 * 167 * Converts tm time to psTime. The result from this function is in TAI units. 168 * 169 * @return psTime: time in TAI units. 170 */ 171 psTime psTMToTime(struct tm *time); 212 * Converts tm time to psTime. This function assumes the input time already is in TAI time and does not add 213 * or subtract leapseconds. 214 * 215 * @return psTime: time. 216 */ 217 psTime psTMToTime( 218 struct tm *time /** Input time to be converted. */ 219 ); 172 220 /// @} 173 221 -
trunk/psLib/src/astronomy/psTime.c
r1223 r1247 12 12 * @author Ross Harman, MHPCC 13 13 * 14 * @version $Revision: 1. 1$ $Name: not supported by cvs2svn $15 * @date $Date: 2004-07- 15 19:02:13 $14 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2004-07-21 01:29:33 $ 16 16 * 17 17 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 24 24 25 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 26 28 27 29 #include "psTime.h" 28 30 #include "psError.h" 31 #define PS_ALLOW_MALLOC 32 #include "psMemory.h" 29 33 30 34 /******************************************************************************/ … … 32 36 /******************************************************************************/ 33 37 34 // Number of available leapsecond updates 35 #define NUM_LEAPSECOND_UPDATES 23 38 /** Preprocessor macro to generate error for negative time in struct */ 39 #define CHECK_NEGATIVE_TIME_STRUCT(NAME,RETURN) \ 40 if(NAME.tv_sec < 0) { \ 41 psError(__func__,"Negative seconds are not allowed: %ld", NAME.tv_sec); \ 42 return RETURN; \ 43 } else if(NAME.tv_usec<0) { \ 44 psError(__func__,"Negative microseconds are not allowed: %ld", NAME.tv_usec); \ 45 return RETURN; \ 46 } 47 48 /** Preprocessor macro to generate error for negative time */ 49 #define CHECK_NEGATIVE_TIME(NAME,RETURN) \ 50 if(NAME < 0.0) { \ 51 psError(__func__,"Negative time is not allowed: %f", NAME); \ 52 return RETURN; \ 53 } \ 36 54 37 55 /******************************************************************************/ … … 55 73 the psTimeGetTime() function to calculate TAI time from UTC time. 56 74 57 1972 JAN 1 =JD 2441317.5 TAI-UTC=10.058 1972 JUL 1 =JD 2441499.5 TAI-UTC=11.059 1973 JAN 1 =JD 2441683.5 TAI-UTC=12.060 1974 JAN 1 =JD 2442048.5 TAI-UTC=13.061 1975 JAN 1 =JD 2442413.5 TAI-UTC=14.062 1976 JAN 1 =JD 2442778.5 TAI-UTC=15.063 1977 JAN 1 =JD 2443144.5 TAI-UTC=16.064 1978 JAN 1 =JD 2443509.5 TAI-UTC=17.065 1979 JAN 1 =JD 2443874.5 TAI-UTC=18.066 1980 JAN 1 =JD 2444239.5 TAI-UTC=19.067 1981 JUL 1 =JD 2444786.5 TAI-UTC=20.068 1982 JUL 1 =JD 2445151.5 TAI-UTC=21.069 1983 JUL 1 =JD 2445516.5 TAI-UTC=22.070 1985 JUL 1 =JD 2446247.5 TAI-UTC=23.071 1988 JAN 1 =JD 2447161.5 TAI-UTC=24.072 1990 JAN 1 =JD 2447892.5 TAI-UTC=25.073 1991 JAN 1 =JD 2448257.5 TAI-UTC=26.074 1992 JUL 1 =JD 2448804.5 TAI-UTC=27.075 1993 JUL 1 =JD 2449169.5 TAI-UTC=28.076 1994 JUL 1 =JD 2449534.5 TAI-UTC=29.077 1996 JAN 1 =JD 2450083.5 TAI-UTC=30.078 1997 JUL 1 =JD 2450630.5 TAI-UTC=31.079 1999 JAN 1 =JD 2451179.5 TAI-UTC=32.075 1972 JAN 1 = JD 2441317.5 TAI-UTC = 10.0 76 1972 JUL 1 = JD 2441499.5 TAI-UTC = 11.0 77 1973 JAN 1 = JD 2441683.5 TAI-UTC = 12.0 78 1974 JAN 1 = JD 2442048.5 TAI-UTC = 13.0 79 1975 JAN 1 = JD 2442413.5 TAI-UTC = 14.0 80 1976 JAN 1 = JD 2442778.5 TAI-UTC = 15.0 81 1977 JAN 1 = JD 2443144.5 TAI-UTC = 16.0 82 1978 JAN 1 = JD 2443509.5 TAI-UTC = 17.0 83 1979 JAN 1 = JD 2443874.5 TAI-UTC = 18.0 84 1980 JAN 1 = JD 2444239.5 TAI-UTC = 19.0 85 1981 JUL 1 = JD 2444786.5 TAI-UTC = 20.0 86 1982 JUL 1 = JD 2445151.5 TAI-UTC = 21.0 87 1983 JUL 1 = JD 2445516.5 TAI-UTC = 22.0 88 1985 JUL 1 = JD 2446247.5 TAI-UTC = 23.0 89 1988 JAN 1 = JD 2447161.5 TAI-UTC = 24.0 90 1990 JAN 1 = JD 2447892.5 TAI-UTC = 25.0 91 1991 JAN 1 = JD 2448257.5 TAI-UTC = 26.0 92 1992 JUL 1 = JD 2448804.5 TAI-UTC = 27.0 93 1993 JUL 1 = JD 2449169.5 TAI-UTC = 28.0 94 1994 JUL 1 = JD 2449534.5 TAI-UTC = 29.0 95 1996 JAN 1 = JD 2450083.5 TAI-UTC = 30.0 96 1997 JUL 1 = JD 2450630.5 TAI-UTC = 31.0 97 1999 JAN 1 = JD 2451179.5 TAI-UTC = 32.0 80 98 */ 81 99 82 // Julian date of leapsecond update and current total number of leapseconds forthat date100 // Table for Julian date of leapsecond update and current total number of leapseconds at that date 83 101 static double leapseconds[NUM_LEAPSECOND_UPDATES][2] = 84 102 { … … 135 153 time.tv_usec = now.tv_usec; 136 154 137 // Add last known leapsecondsto UTC time to get TAI time155 // Add most current leapseconds value to UTC time to get TAI time 138 156 time.tv_sec += leapseconds[NUM_LEAPSECOND_UPDATES][1]; 139 157 … … 143 161 char* psTimeToISO(psTime time) 144 162 { 145 struct tm *tm; 146 tm = localtime (&time.tv_sec); 147 printf("%d\n", tm->tm_sec); 148 return NULL; 149 } 163 int ms = 0; 164 char *timeString = NULL; 165 char *tempString = NULL; 166 struct tm *tmTime = NULL; 167 168 CHECK_NEGATIVE_TIME_STRUCT(time,NULL); 169 170 tempString = psAlloc(MAX_TIME_STRING_LENGTH); 171 timeString = psAlloc(MAX_TIME_STRING_LENGTH); 172 173 // Converts psTime to YYYY/MM/DD,HH:MM:SS.SSS in string form 174 ms = time.tv_usec/1000; 175 tmTime = gmtime(&time.tv_sec); 176 if(!strftime(tempString, MAX_TIME_STRING_LENGTH, "%Y/%m/%d,%H:%M:%S", tmTime)) { 177 psError(__func__, " : Line %d - Failed strftime conversion", __LINE__); 178 } 179 180 if(snprintf(timeString,MAX_TIME_STRING_LENGTH,"%s.%3.3d", tempString, ms) < 0) { 181 psError(__func__, " : Line %d - Failed snprintf conversion", __LINE__); 182 } 183 psFree(tempString); 184 free(tmTime); 185 186 return timeString; 187 } 188 189 psTime psTimeToUTC(psTime time) 190 { 191 int i = 0; 192 double jd = 0.0; 193 double ls = 0.0; 194 double *jdTable = NULL; 195 psTime outTime; 196 197 CHECK_NEGATIVE_TIME_STRUCT(time,outTime); 198 199 // Find leapseconds to subtract from psTime to get UTC time 200 jd = psTimeToJD(time); 201 jdTable = leapseconds[0]; 202 for(i=0; i<NUM_LEAPSECOND_UPDATES; i++, jdTable+=2) { 203 if(jd > *jdTable) { 204 ls = *(jdTable + 1); 205 } 206 } 207 208 outTime.tv_sec = time.tv_sec - ls; 209 outTime.tv_usec = time.tv_usec; 210 211 CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime); 212 213 return outTime; 214 } 215 216 double psTimeToMJD(psTime time) 217 { 218 double mjd = 0.0; 219 220 CHECK_NEGATIVE_TIME_STRUCT(time,mjd); 221 222 // Modified Julian date conversion courtesy of Eugene Magnier 223 mjd = time.tv_sec/SEC_PER_DAY + time.tv_usec/USEC_PER_DAY + 40587.0; 224 225 return mjd; 226 } 227 228 double psTimeToJD(psTime time) 229 { 230 double jd = 0.0; 231 232 CHECK_NEGATIVE_TIME_STRUCT(time,jd); 233 234 // Julian date conversion courtesy of Eugene Magnier 235 jd = time.tv_sec/SEC_PER_DAY + time.tv_usec/USEC_PER_DAY + 2440587.5; 236 237 return jd; 238 } 239 240 struct timeval psTimeToTimeval(psTime time) 241 { 242 struct timeval timevalTime; 243 244 CHECK_NEGATIVE_TIME_STRUCT(time,timevalTime); 245 timevalTime.tv_sec = time.tv_sec; 246 timevalTime.tv_usec = time.tv_usec; 247 248 return timevalTime; 249 } 250 251 struct tm* psTimeToTM(psTime time) 252 { 253 struct tm *tmTime = NULL; 254 255 CHECK_NEGATIVE_TIME_STRUCT(time,tmTime); 256 tmTime = gmtime(&time.tv_sec); 257 258 return tmTime; 259 } 260 261 psTime psISOToTime(char *time) 262 { 263 char tempString[MAX_TIME_STRING_LENGTH]; 264 int month; 265 int day; 266 int year; 267 int hour; 268 int minute; 269 int second; 270 int millisecond; 271 struct tm tmTime; 272 psTime outTime; 273 274 strncpy(tempString, time, MAX_TIME_STRING_LENGTH); 275 276 // Convert YYYY/MM/DD,HH:MM:SS.SSS in string form to tm time 277 year = atoi(strtok(tempString, "/")); 278 if(year < 0) { 279 psError(__func__,"Years less than 1900 not allowed. Value: %d", year); 280 } 281 282 month = atoi(strtok(tempString, "/")); 283 if(month<1 || month>12) { 284 psError(__func__,"Month must have a value from 0 to 11. Value: %d", month); 285 } 286 287 day = atoi(strtok(tempString, ",")); 288 if(day<1 || day>31) { 289 psError(__func__,"Day must have a value from 1 to 31. Value: %d", day); 290 } 291 292 hour = atoi(strtok(tempString, ":")); 293 if(hour<0 || hour>23) { 294 psError(__func__,"Hour must have a value from 0 to 23. Value: %d", hour); 295 } 296 297 second = atoi(strtok(tempString, ".")); 298 if(second<0 || second>60) { 299 psError(__func__,"Second must have a value from 0 to 59. Value: %d", second); 300 } 301 302 millisecond = atoi(strtok(tempString, "X")); 303 if(millisecond<0 || millisecond>1000) { 304 psError(__func__,"Second must have a value from 0 to 999. Value: %d", millisecond); 305 } 306 307 tmTime.tm_year = year - 1900; 308 tmTime.tm_mon = month - 1; 309 tmTime.tm_mday = day; 310 tmTime.tm_hour = hour; 311 tmTime.tm_min = minute; 312 tmTime.tm_sec = second; 313 314 // Convert tm time to psTime 315 outTime = psTMToTime(&tmTime); 316 outTime.tv_usec = millisecond*1000; 317 318 return outTime; 319 } 320 321 psTime psUTCToTime(psTime time) 322 { 323 psTime outTime; 324 325 CHECK_NEGATIVE_TIME_STRUCT(time,outTime); 326 327 // Convert UTC time to psTime/TAI 328 outTime.tv_sec = time.tv_sec + leapseconds[NUM_LEAPSECOND_UPDATES][1]; 329 ; 330 outTime.tv_usec = time.tv_usec; 331 CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime); 332 333 return time; 334 } 335 336 psTime psMJDToTime(double time) 337 { 338 psTime outTime; 339 double days = 0.0; 340 double seconds = 0.0; 341 342 CHECK_NEGATIVE_TIME(time,outTime); 343 344 // Modified Julian date conversion courtesy of Eugene Magnier 345 days = time - 40587.0; 346 347 // Convert to psTime/TAI 348 seconds = days*SEC_PER_DAY; 349 outTime.tv_usec = (seconds -(long)seconds)*1000000.0; 350 outTime.tv_sec = seconds; 351 352 CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime); 353 354 return outTime; 355 } 356 357 psTime psJDToTime(double time) 358 { 359 double days = 0.0; 360 double seconds = 0.0; 361 psTime outTime; 362 363 CHECK_NEGATIVE_TIME(time,outTime); 364 365 // Julian date conversion courtesy of Eugene Magnier 366 days = time - 2440587.5; 367 368 // Convert to psTime/TAI 369 seconds = days*SEC_PER_DAY; 370 outTime.tv_sec = seconds; 371 outTime.tv_usec = (seconds -(long)seconds)*1000000.0; 372 373 CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime); 374 375 return outTime; 376 } 377 378 psTime psTimevalToTime(struct timeval *time) 379 { 380 psTime outTime; 381 if(time == NULL) 382 { 383 psError(__func__,"Null value for timeval arg not allowed"); 384 return outTime; 385 } else if(time->tv_sec < 0) 386 { 387 psError(__func__,"Negative seconds are not allowed: %ld", time->tv_sec); 388 return outTime; 389 } else if(time->tv_usec<0) 390 { 391 psError(__func__,"Negative microseconds are not allowed: %ld", time->tv_usec); 392 return outTime; 393 } 394 395 // Convert to psTime/TAI 396 outTime.tv_sec = time->tv_sec; 397 outTime.tv_usec = time->tv_usec; 398 399 CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime); 400 401 return outTime; 402 } 403 404 405 psTime psTMToTime(struct tm *time) 406 { 407 int i; 408 int m; 409 int n; 410 int y; 411 int mon [] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 412 long epoch; 413 psTime outTime; 414 415 i= 0; 416 m = 0; 417 n = 0; 418 y = 0; 419 epoch = 0; 420 421 if(time == NULL) 422 { 423 psError(__func__,"Null value for tm arg not allowed"); 424 return outTime; 425 } else if(time->tm_year < 70) 426 { 427 psError(__func__,"Input times earlier than 1970 not allowed"); 428 return outTime; 429 } 430 431 n = time->tm_year + 1900 - 1; 432 epoch = (time->tm_year - 70) * SEC_PER_YEAR + ((n/4 - n/100 + n/400) - 433 (1969/4 - 1969/100 + 1969/400)) * SEC_PER_DAY; 434 435 y = time->tm_year + 1900; 436 437 for(i = 0; i < time->tm_mon; i++) 438 { 439 epoch += mon [m] * SEC_PER_DAY; 440 if(m == 1 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) { 441 epoch += SEC_PER_DAY; 442 } 443 444 if(++m > 11) { 445 m = 0; 446 y++; 447 } 448 } 449 450 epoch += (time->tm_mday - 1) * SEC_PER_DAY; 451 epoch += time->tm_hour *SEC_PER_HOUR + time->tm_min * SEC_PER_MINUTE + time->tm_sec; 452 453 outTime.tv_usec = 0; 454 outTime.tv_sec = epoch; 455 456 return outTime; 457 } -
trunk/psLib/src/astronomy/psTime.h
r1223 r1247 12 12 * @author Ross Harman, MHPCC 13 13 * 14 * @version $Revision: 1. 3$ $Name: not supported by cvs2svn $15 * @date $Date: 2004-07- 15 19:02:13$14 * @version $Revision: 1.4 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2004-07-21 01:29:22 $ 16 16 * 17 17 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 29 29 /// @addtogroup Time 30 30 /// @{ 31 32 /******************************************************************************/ 33 /* DEFINE STATEMENTS */ 34 /******************************************************************************/ 35 36 /** Number of available leapsecond updates */ 37 #define NUM_LEAPSECOND_UPDATES 23 38 39 /** Maximum length of time string */ 40 #define MAX_TIME_STRING_LENGTH 256 41 42 /** Seconds per minute */ 43 #define SEC_PER_MINUTE 60 44 45 /** Seconds per hour */ 46 #define SEC_PER_HOUR 60*SEC_PER_MINUTE 47 48 /** Seconds per day */ 49 #define SEC_PER_DAY 24*SEC_PER_HOUR 50 51 /** Seconds per year */ 52 #define SEC_PER_YEAR 365*SEC_PER_DAY 53 54 /** Microseconds per day */ 55 #define USEC_PER_DAY 86400000000.0 31 56 32 57 /******************************************************************************/ … … 63 88 /** Convert psTime to ISO time in TAI units. 64 89 * 65 * Converts psTime to a null terminated string in the form of: YYYY/MM/DD,HH:MM:SS.SSS. Th e result from this66 * function is in TAI units.67 * 68 * @return char*: Pointer null terminated array of chars in ISO /TAItime.90 * Converts psTime to a null terminated string in the form of: YYYY/MM/DD,HH:MM:SS.SSS. This function assumes 91 * the input time already is in TAI time and does not add or subtract leapseconds. 92 * 93 * @return char*: Pointer null terminated array of chars in ISO time. 69 94 */ 70 95 char* psTimeToISO( … … 74 99 /** Convert psTime to UTC time. 75 100 * 76 * Converts psTime to UTC time in double precision floating point notation. The result from this function is 77 * not in TAI units, but that of UTC, which does not contain leapseconds. 78 * 79 * @return double: UTC time in floating point notation. 80 */ 81 double psTimeToUTC( 101 * Converts psTime to UTC time in double precision floating point notation. The input to this must already be 102 * int TAI time. The result from this function is not in TAI units, but that of UTC, which does not contain 103 * leapseconds. 104 * 105 * @return psTime: UTC time psTime format. 106 */ 107 psTime psTimeToUTC( 82 108 psTime time /** Input time to be converted. */ 83 109 ); … … 85 111 /** Convert psTime to modified Julian date time. 86 112 * 87 * Converts psTime to modified Julian date (MJD) time. The result from this function is in TAI units. 88 * 89 * @return double: MJD/TAI time in floating point notation. 113 * Converts psTime to modified Julian date (MJD) time. This function assumes the input time already is in TAI 114 * time and does not add or subtract leapseconds. 115 * 116 * @return double: Modified Julian Days (MJD) time. 90 117 */ 91 118 double psTimeToMJD( … … 95 122 /** Convert psTime to Julian date time. 96 123 * 97 * Converts psTime to Julian date (JD) time. The result from this function is in TAI units. 98 * 99 * @return double: JD/TAI time in floating point notation. 124 * Converts psTime to Julian date (JD) time. This function assumes the input time already is in TAI time and 125 * does not add or subtract leapseconds. 126 * 127 * @return double: Julian Date (JD) time. 100 128 */ 101 129 double psTimeToJD( … … 105 133 /** Convert psTime to timeval time. 106 134 * 107 * Converts psTime to timeval time. The result from this function is in TAI units. 108 * 109 * @return timeval: timeval/TAI time. 135 * Converts psTime to timeval time. This function assumes the input time already is in TAI time and does not 136 * add or subtract leapseconds. 137 * 138 * @return timeval: timeval struct time. 110 139 */ 111 140 struct timeval psTimeToTimeval( … … 115 144 /** Convert psTime to tm time. 116 145 * 117 * Converts psTime to tm time. The result from this function is in TAI units. 118 * 119 * @return tm: tm/TAI time. 146 * Converts psTime to tm time. This function assumes the input time already is in TAI time and does not add or 147 * subtract leapseconds. 148 * 149 * @return tm: tm struct time. 120 150 */ 121 151 struct tm* psTimeToTM( … … 125 155 /** Convert ISO to psTime. 126 156 * 127 * Converts ISO time to psTime. The result from this function is in TAI units. 157 * Converts ISO time to psTime. This function assumes the input time already is in TAI time and does not add 158 * or subtract leapseconds. 159 * 160 * @return psTime: time 161 */ 162 psTime psISOToTime( 163 char *time /** Input time to be converted. */ 164 ); 165 166 /** Convert UTC to psTime. 167 * 168 * Converts UTC time to psTime. This function assumes the input time already is in TAI time and add or 169 * subtracts the necessary leapseconds. 128 170 * 129 171 * @return psTime: time in TAI units. 130 172 */ 131 psTime psISOToTime(char *time); 132 133 /** Convert UTC to psTime. 134 * 135 * Converts UTC time to psTime. The result from this function is in TAI units. 136 * 137 * @return psTime: time in TAI units. 138 */ 139 psTime psUTCToTime(double time); 173 psTime psUTCToTime( 174 psTime time /** Input time to be converted. */ 175 ); 140 176 141 177 /** Convert MJD to psTime. 142 178 * 143 * Converts MJD time to psTime. The result from this function is in TAI units. 144 * 145 * @return psTime: time in TAI units. 146 */ 147 psTime psMJDToTime(double time); 179 * Converts MJD time to psTime. This function assumes the input time already is in TAI time and does not add 180 * or subtract leapseconds. 181 * 182 * @return psTime: time. 183 */ 184 psTime psMJDToTime( 185 double time /** Input time to be converted. */ 186 ); 148 187 149 188 /** Convert JD to psTime. 150 189 * 151 * Converts JD time to psTime. The result from this function is in TAI units. 152 * 153 * @return psTime: time in TAI units. 154 */ 155 psTime psJDToTime(double time); 190 * Converts JD time to psTime. This function assumes the input time already is in TAI time and does not add 191 * or subtract leapseconds. 192 * 193 * @return psTime: time. 194 */ 195 psTime psJDToTime( 196 double time /** Input time to be converted. */ 197 ); 156 198 157 199 /** Convert timeval to psTime. 158 200 * 159 * Converts timeval time to psTime. The result from this function is in TAI units. 160 * 161 * @return psTime: time in TAI units. 162 */ 163 psTime psTimevalToTime(struct timeval *time); 201 * Converts timeval time to psTime. This function assumes the input time already is in TAI time and does not 202 * add or subtract leapseconds. 203 * 204 * @return psTime: time. 205 */ 206 psTime psTimevalToTime( 207 struct timeval *time /** Input time to be converted. */ 208 ); 164 209 165 210 /** Convert tm time to psTime. 166 211 * 167 * Converts tm time to psTime. The result from this function is in TAI units. 168 * 169 * @return psTime: time in TAI units. 170 */ 171 psTime psTMToTime(struct tm *time); 212 * Converts tm time to psTime. This function assumes the input time already is in TAI time and does not add 213 * or subtract leapseconds. 214 * 215 * @return psTime: time. 216 */ 217 psTime psTMToTime( 218 struct tm *time /** Input time to be converted. */ 219 ); 172 220 /// @} 173 221
Note:
See TracChangeset
for help on using the changeset viewer.
