Changeset 1407 for trunk/psLib/src/astro/psTime.c
- Timestamp:
- Aug 6, 2004, 2:06:06 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/astro/psTime.c (modified) (27 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/astro/psTime.c
r1406 r1407 1 1 2 /** @file psTime.c 2 3 * … … 12 13 * @author Ross Harman, MHPCC 13 14 * 14 * @version $Revision: 1.1 3$ $Name: not supported by cvs2svn $15 * @date $Date: 2004-08-0 6 22:34:05$15 * @version $Revision: 1.14 $ $Name: not supported by cvs2svn $ 16 * @date $Date: 2004-08-07 00:06:06 $ 16 17 * 17 18 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 18 19 */ 19 20 20 21 21 /******************************************************************************/ 22 22 23 /* INCLUDE FILES */ 24 23 25 /******************************************************************************/ 24 26 … … 32 34 33 35 /******************************************************************************/ 36 34 37 /* DEFINE STATEMENTS */ 38 35 39 /******************************************************************************/ 36 40 … … 73 77 } \ 74 78 79 75 80 /******************************************************************************/ 81 76 82 /* TYPE DEFINITIONS */ 83 77 84 /******************************************************************************/ 78 85 … … 80 87 81 88 /*****************************************************************************/ 89 82 90 /* GLOBAL VARIABLES */ 91 83 92 /*****************************************************************************/ 84 93 … … 86 95 87 96 /*****************************************************************************/ 97 88 98 /* FILE STATIC VARIABLES */ 99 89 100 /*****************************************************************************/ 90 101 … … 119 130 120 131 // Table for Julian date of leapsecond update and current total number of leapseconds at that date 121 static double leapseconds[NUM_LEAPSECOND_UPDATES][2] = 122 { 123 { 124 2441317.5, 10.0 125 }, 126 { 127 2441499.5, 11.0 128 }, 129 { 130 2441683.5, 12.0 131 }, 132 { 133 2442048.5, 13.0 134 }, 135 { 136 2442413.5, 14.0 137 }, 138 { 139 2442778.5, 15.0 140 }, 141 { 142 2443144.5, 16.0 143 }, 144 { 145 2443509.5, 17.0 146 }, 147 { 148 2443874.5, 18.0 149 }, 150 { 151 2444239.5, 19.0 152 }, 153 { 154 2444786.5, 20.0 155 }, 156 { 157 2445151.5, 21.0 158 }, 159 { 160 2445516.5, 22.0 161 }, 162 { 163 2446247.5, 23.0 164 }, 165 { 166 2447161.5, 24.0 167 }, 168 { 169 2447892.5, 25.0 170 }, 171 { 172 2448257.5, 26.0 173 }, 174 { 175 2448804.5, 27.0 176 }, 177 { 178 2449169.5, 28.0 179 }, 180 { 181 2449534.5, 29.0 182 }, 183 { 184 2450083.5, 30.0 185 }, 186 { 187 2450630.5, 31.0 188 }, 189 { 190 2451179.5, 32.0 191 } 192 }; 193 194 /*****************************************************************************/ 132 static double leapseconds[NUM_LEAPSECOND_UPDATES][2] = { 133 { 134 2441317.5, 10.0}, 135 { 136 2441499.5, 11.0}, 137 { 138 2441683.5, 12.0}, 139 { 140 2442048.5, 13.0}, 141 { 142 2442413.5, 14.0}, 143 { 144 2442778.5, 15.0}, 145 { 146 2443144.5, 16.0}, 147 { 148 2443509.5, 17.0}, 149 { 150 2443874.5, 18.0}, 151 { 152 2444239.5, 19.0}, 153 { 154 2444786.5, 20.0}, 155 { 156 2445151.5, 21.0}, 157 { 158 2445516.5, 22.0}, 159 { 160 2446247.5, 23.0}, 161 { 162 2447161.5, 24.0}, 163 { 164 2447892.5, 25.0}, 165 { 166 2448257.5, 26.0}, 167 { 168 2448804.5, 27.0}, 169 { 170 2449169.5, 28.0}, 171 { 172 2449534.5, 29.0}, 173 { 174 2450083.5, 30.0}, 175 { 176 2450630.5, 31.0}, 177 { 178 2451179.5, 32.0} 179 }; 180 181 /*****************************************************************************/ 182 195 183 /* FUNCTION IMPLEMENTATION - LOCAL */ 184 196 185 /*****************************************************************************/ 197 186 … … 199 188 200 189 /*****************************************************************************/ 190 201 191 /* FUNCTION IMPLEMENTATION - PUBLIC */ 192 202 193 /*****************************************************************************/ 203 194 … … 210 201 time.tv_usec = 0; 211 202 212 if (gettimeofday(&now,(struct timezone *)0) == -1) {203 if (gettimeofday(&now, (struct timezone *)0) == -1) { 213 204 psError(__func__, " : Line %d - Failed to get time", __LINE__); 214 205 return time; 215 206 } 216 217 207 // Convert timeval time to psTime 218 208 time.tv_sec = now.tv_sec; … … 220 210 221 211 // Add most current leapseconds value to UTC time to get TAI time 222 time.tv_sec += leapseconds[NUM_LEAPSECOND_UPDATES -1][1];212 time.tv_sec += leapseconds[NUM_LEAPSECOND_UPDATES - 1][1]; 223 213 224 214 return time; 225 215 } 226 216 227 char *psTimeToISO(psTime time)217 char *psTimeToISO(psTime time) 228 218 { 229 219 int ms = 0; … … 232 222 struct tm *tmTime = NULL; 233 223 234 CHECK_NEGATIVE_TIME_STRUCT(time, NULL);224 CHECK_NEGATIVE_TIME_STRUCT(time, NULL); 235 225 236 226 tempString = psAlloc(MAX_TIME_STRING_LENGTH); … … 238 228 239 229 // Converts psTime to YYYY/MM/DD,HH:MM:SS.SSS in string form 240 ms = time.tv_usec /1000;230 ms = time.tv_usec / 1000; 241 231 242 232 // tmTime variable is statically allocated, no need to free 243 233 tmTime = gmtime(&time.tv_sec); 244 if (!strftime(tempString, MAX_TIME_STRING_LENGTH, "%Y/%m/%d,%H:%M:%S", tmTime)) {234 if (!strftime(tempString, MAX_TIME_STRING_LENGTH, "%Y/%m/%d,%H:%M:%S", tmTime)) { 245 235 psError(__func__, " : Line %d - Failed strftime conversion", __LINE__); 246 236 } 247 237 248 if (snprintf(timeString,MAX_TIME_STRING_LENGTH,"%s.%3.3d", tempString, ms) < 0) {238 if (snprintf(timeString, MAX_TIME_STRING_LENGTH, "%s.%3.3d", tempString, ms) < 0) { 249 239 psError(__func__, " : Line %d - Failed snprintf conversion", __LINE__); 250 240 } … … 262 252 psTime outTime; 263 253 264 CHECK_NEGATIVE_TIME_STRUCT(time, outTime);254 CHECK_NEGATIVE_TIME_STRUCT(time, outTime); 265 255 266 256 // Find leapseconds to subtract from psTime to get UTC time 267 257 jd = psTimeToJD(time); 268 258 jdTable = leapseconds[0]; 269 for (i=0; i<NUM_LEAPSECOND_UPDATES; i++, jdTable+=2) {270 if (jd > *jdTable) {259 for (i = 0; i < NUM_LEAPSECOND_UPDATES; i++, jdTable += 2) { 260 if (jd > *jdTable) { 271 261 ls = *(jdTable + 1); 272 262 } … … 276 266 outTime.tv_usec = time.tv_usec; 277 267 278 CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime);268 CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime); 279 269 280 270 return outTime; … … 285 275 double mjd = 0.0; 286 276 287 CHECK_NEGATIVE_TIME_STRUCT(time, mjd);277 CHECK_NEGATIVE_TIME_STRUCT(time, mjd); 288 278 289 279 // Modified Julian date conversion courtesy of Eugene Magnier 290 mjd = time.tv_sec /SEC_PER_DAY + time.tv_usec/USEC_PER_DAY + 40587.0;280 mjd = time.tv_sec / SEC_PER_DAY + time.tv_usec / USEC_PER_DAY + 40587.0; 291 281 292 282 return mjd; … … 297 287 double jd = 0.0; 298 288 299 CHECK_NEGATIVE_TIME_STRUCT(time, jd);289 CHECK_NEGATIVE_TIME_STRUCT(time, jd); 300 290 301 291 // Julian date conversion courtesy of Eugene Magnier 302 jd = time.tv_sec /SEC_PER_DAY + time.tv_usec/USEC_PER_DAY + 2440587.5;292 jd = time.tv_sec / SEC_PER_DAY + time.tv_usec / USEC_PER_DAY + 2440587.5; 303 293 304 294 return jd; … … 309 299 struct timeval timevalTime; 310 300 311 CHECK_NEGATIVE_TIME_STRUCT(time, timevalTime);301 CHECK_NEGATIVE_TIME_STRUCT(time, timevalTime); 312 302 timevalTime.tv_sec = time.tv_sec; 313 303 timevalTime.tv_usec = time.tv_usec; … … 316 306 } 317 307 318 struct tm *psTimeToTM(psTime time)308 struct tm *psTimeToTM(psTime time) 319 309 { 320 310 struct tm *tmTime = NULL; 321 311 322 CHECK_NEGATIVE_TIME_STRUCT(time, tmTime);312 CHECK_NEGATIVE_TIME_STRUCT(time, tmTime); 323 313 tmTime = gmtime(&time.tv_sec); 324 314 … … 343 333 // Convert YYYY/MM/DD,HH:MM:SS.SSS in string form to tm time 344 334 year = atoi(strtok(tempString, "/")); 345 if (year < 1900) {346 psError(__func__, "Years less than 1900 not allowed. Value: %d", year);335 if (year < 1900) { 336 psError(__func__, "Years less than 1900 not allowed. Value: %d", year); 347 337 return outTime; 348 338 } 349 339 350 340 month = atoi(strtok(NULL, "/")); 351 if (month<1 || month>12) {352 psError(__func__, "Month must have a value from 1 to 12. Value: %d", month);341 if (month < 1 || month > 12) { 342 psError(__func__, "Month must have a value from 1 to 12. Value: %d", month); 353 343 return outTime; 354 344 } 355 345 356 346 day = atoi(strtok(NULL, ",")); 357 if (day<1 || day>31) {358 psError(__func__, "Day must have a value from 1 to 31. Value: %d", day);347 if (day < 1 || day > 31) { 348 psError(__func__, "Day must have a value from 1 to 31. Value: %d", day); 359 349 return outTime; 360 350 } 361 351 362 352 hour = atoi(strtok(NULL, ":")); 363 if (hour<0 || hour>23) {364 psError(__func__, "Hour must have a value from 0 to 23. Value: %d", hour);353 if (hour < 0 || hour > 23) { 354 psError(__func__, "Hour must have a value from 0 to 23. Value: %d", hour); 365 355 return outTime; 366 356 } 367 357 368 358 minute = atoi(strtok(NULL, ":")); 369 if (minute<0 || minute>59) {370 psError(__func__, "Minute must have a value from 0 to 59. Value: %d", minute);359 if (minute < 0 || minute > 59) { 360 psError(__func__, "Minute must have a value from 0 to 59. Value: %d", minute); 371 361 return outTime; 372 362 } 373 363 374 364 second = atoi(strtok(NULL, ".")); 375 if (second<0 || second>59) {376 psError(__func__, "Second must have a value from 0 to 59. Value: %d", second);365 if (second < 0 || second > 59) { 366 psError(__func__, "Second must have a value from 0 to 59. Value: %d", second); 377 367 return outTime; 378 368 } 379 369 380 370 millisecond = atoi(strtok(NULL, "X")); 381 if (millisecond<0 || millisecond>1000) {382 psError(__func__, "Millisecond must have a value from 0 to 999. Value: %d", millisecond);371 if (millisecond < 0 || millisecond > 1000) { 372 psError(__func__, "Millisecond must have a value from 0 to 999. Value: %d", millisecond); 383 373 return outTime; 384 374 } … … 394 384 // Convert tm time to psTime 395 385 outTime = psTMToTime(&tmTime); 396 outTime.tv_usec = millisecond *1000;386 outTime.tv_usec = millisecond * 1000; 397 387 398 388 return outTime; … … 403 393 psTime outTime; 404 394 405 CHECK_NEGATIVE_TIME_STRUCT(time, outTime);395 CHECK_NEGATIVE_TIME_STRUCT(time, outTime); 406 396 407 397 // Convert UTC time to psTime/TAI 408 outTime.tv_sec = time.tv_sec + leapseconds[NUM_LEAPSECOND_UPDATES -1][1];398 outTime.tv_sec = time.tv_sec + leapseconds[NUM_LEAPSECOND_UPDATES - 1][1]; 409 399 outTime.tv_usec = time.tv_usec; 410 CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime);400 CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime); 411 401 412 402 return outTime; … … 419 409 double seconds = 0.0; 420 410 421 CHECK_NEGATIVE_TIME(time, outTime);411 CHECK_NEGATIVE_TIME(time, outTime); 422 412 423 413 // Modified Julian date conversion courtesy of Eugene Magnier … … 425 415 426 416 // Convert to psTime/TAI 427 seconds = days *SEC_PER_DAY;428 outTime.tv_usec = (seconds - (long)seconds)*1000000.0;417 seconds = days * SEC_PER_DAY; 418 outTime.tv_usec = (seconds - (long)seconds) * 1000000.0; 429 419 outTime.tv_sec = seconds; 430 420 431 CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime);421 CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime); 432 422 433 423 return outTime; … … 440 430 psTime outTime; 441 431 442 CHECK_NEGATIVE_TIME(time, outTime);432 CHECK_NEGATIVE_TIME(time, outTime); 443 433 444 434 // Julian date conversion courtesy of Eugene Magnier … … 446 436 447 437 // Convert to psTime/TAI 448 seconds = days *SEC_PER_DAY;438 seconds = days * SEC_PER_DAY; 449 439 outTime.tv_sec = seconds; 450 outTime.tv_usec = (seconds -(long)seconds)*1000000.0; 451 452 CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime); 453 454 return outTime; 455 } 456 457 psTime psTimevalToTime(struct timeval *time) 458 { 459 psTime outTime; 460 if(time == NULL) 461 { 462 psError(__func__,"Null value for timeval arg not allowed"); 463 return outTime; 464 } else 465 if(time->tv_sec < 0) 466 { 467 psError(__func__,"Negative seconds are not allowed: %ld", time->tv_sec); 468 return outTime; 469 } else 470 if(time->tv_usec<0) 471 { 472 psError(__func__,"Negative microseconds are not allowed: %ld", time->tv_usec); 473 return outTime; 474 } 475 440 outTime.tv_usec = (seconds - (long)seconds) * 1000000.0; 441 442 CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime); 443 444 return outTime; 445 } 446 447 psTime psTimevalToTime(struct timeval * time) 448 { 449 psTime outTime; 450 451 if (time == NULL) 452 { 453 psError(__func__, "Null value for timeval arg not allowed"); 454 return outTime; 455 } else if (time->tv_sec < 0) 456 { 457 psError(__func__, "Negative seconds are not allowed: %ld", time->tv_sec); 458 return outTime; 459 } else if (time->tv_usec < 0) 460 { 461 psError(__func__, "Negative microseconds are not allowed: %ld", time->tv_usec); 462 return outTime; 463 } 476 464 // Convert to psTime/TAI 477 465 outTime.tv_sec = time->tv_sec; 478 466 outTime.tv_usec = time->tv_usec; 479 467 480 CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime); 481 482 return outTime; 483 } 484 485 486 psTime psTMToTime(struct tm *time) 468 CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime); 469 470 return outTime; 471 } 472 473 psTime psTMToTime(struct tm * time) 487 474 { 488 475 int i; 489 476 int n; 490 477 int y; 491 int mon [] = 492 { 493 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 494 }; 478 int mon[] = { 479 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 480 }; 495 481 long epoch; 496 482 psTime outTime; 497 483 498 i = 0;484 i = 0; 499 485 n = 0; 500 486 y = 0; 501 487 epoch = 0; 502 488 503 if(time == NULL) 504 { 505 psError(__func__,"Null value for tm arg not allowed"); 506 return outTime; 507 } else 508 if(time->tm_year < 70) 509 { 510 psError(__func__,"Input times earlier than 1970 not allowed. Value: %d", time->tm_year+1900); 511 return outTime; 512 } else 513 if(time->tm_mon<0 || time->tm_mon>11) 514 { 515 psError(__func__,"Month must have a value from 0 to 11. Value: %d", time->tm_mon); 516 return outTime; 517 } else 518 if(time->tm_mday<1 || time->tm_mday>31) 519 { 520 psError(__func__,"Day must have a value from 1 to 31. Value: %d", time->tm_mday); 521 return outTime; 522 } else 523 if(time->tm_hour<0 || time->tm_hour>23) 524 { 525 psError(__func__,"Hour must have a value from 0 to 23. Value: %d", time->tm_hour); 526 return outTime; 527 } else 528 if(time->tm_min<0 || time->tm_min>59) 529 { 530 psError(__func__,"Minute must have a value from 0 to 59. Value: %d", time->tm_min); 531 return outTime; 532 } else 533 if(time->tm_sec<0 || time->tm_sec>59) 534 { 535 psError(__func__,"Second must have a value from 0 to 59. Value: %d", time->tm_sec); 536 return outTime; 537 } 489 if (time == NULL) 490 { 491 psError(__func__, "Null value for tm arg not allowed"); 492 return outTime; 493 } else if (time->tm_year < 70) 494 { 495 psError(__func__, "Input times earlier than 1970 not allowed. Value: %d", time->tm_year + 1900); 496 return outTime; 497 } else if (time->tm_mon < 0 || time->tm_mon > 11) 498 { 499 psError(__func__, "Month must have a value from 0 to 11. Value: %d", time->tm_mon); 500 return outTime; 501 } else if (time->tm_mday < 1 || time->tm_mday > 31) 502 { 503 psError(__func__, "Day must have a value from 1 to 31. Value: %d", time->tm_mday); 504 return outTime; 505 } else if (time->tm_hour < 0 || time->tm_hour > 23) 506 { 507 psError(__func__, "Hour must have a value from 0 to 23. Value: %d", time->tm_hour); 508 return outTime; 509 } else if (time->tm_min < 0 || time->tm_min > 59) 510 { 511 psError(__func__, "Minute must have a value from 0 to 59. Value: %d", time->tm_min); 512 return outTime; 513 } else if (time->tm_sec < 0 || time->tm_sec > 59) 514 { 515 psError(__func__, "Second must have a value from 0 to 59. Value: %d", time->tm_sec); 516 return outTime; 517 } 538 518 539 519 n = time->tm_year + 1900 - 1; 540 epoch = (time->tm_year - 70) * SEC_PER_YEAR + ((n /4 - n/100 + n/400) -541 (1969 /4 - 1969/100 + 1969/400)) * SEC_PER_DAY;520 epoch = (time->tm_year - 70) * SEC_PER_YEAR + ((n / 4 - n / 100 + n / 400) - 521 (1969 / 4 - 1969 / 100 + 1969 / 400)) * SEC_PER_DAY; 542 522 543 523 y = time->tm_year + 1900; 544 524 545 525 // Adjust for leap years 546 for (i = 0; i<time->tm_mon; i++)547 { 548 epoch += mon [i] * SEC_PER_DAY;549 if (i == 1 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) {526 for (i = 0; i < time->tm_mon; i++) 527 { 528 epoch += mon[i] * SEC_PER_DAY; 529 if (i == 1 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) { 550 530 epoch += SEC_PER_DAY; 551 531 } … … 554 534 // Add everything 555 535 epoch += (time->tm_mday - 1) * SEC_PER_DAY; 556 epoch += time->tm_hour * SEC_PER_HOUR + time->tm_min * SEC_PER_MINUTE + time->tm_sec;536 epoch += time->tm_hour * SEC_PER_HOUR + time->tm_min * SEC_PER_MINUTE + time->tm_sec; 557 537 558 538 // Create psTime … … 560 540 outTime.tv_sec = epoch; 561 541 562 CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime);563 564 return outTime; 565 } 542 CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime); 543 544 return outTime; 545 }
Note:
See TracChangeset
for help on using the changeset viewer.
