Changeset 7700
- Timestamp:
- Jun 26, 2006, 5:45:25 PM (20 years ago)
- Location:
- trunk/psLib/src/astro
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/astro/psTime.c
r7608 r7700 10 10 * @author Ross Harman, MHPCC 11 11 * 12 * @version $Revision: 1.8 6$ $Name: not supported by cvs2svn $13 * @date $Date: 2006-06-2 1 17:46:06$12 * @version $Revision: 1.87 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2006-06-27 03:45:25 $ 14 14 * 15 15 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 1326 1326 char *timeString = NULL; 1327 1327 char *tempString = NULL; 1328 struct tm *tmTime = NULL;1329 time_t sec;1330 1328 1331 1329 // Error checks … … 1342 1340 // Convert nanoseconds to decaseconds 1343 1341 ds = time->nsec / 100000000; 1344 sec = time->sec;1345 1346 // If leapsecond use previous day1347 if(time->leapsecond) {1348 sec--;1349 }1350 1342 1351 1343 // tmTime variable is statically allocated, no need to free 1352 tmTime = gmtime(&sec);1344 struct tm *tmTime = psTimeToTM(time); 1353 1345 1354 1346 // Converts psTime to YYYY-MM-DDThh:mm:ss.sss in string form … … 1356 1348 psError(PS_ERR_OS_CALL_FAILED, true, PS_ERRORTEXT_psTime_CONVERT_TIME_TO_STRING_FAILED); 1357 1349 } 1350 psFree(tmTime); 1358 1351 1359 1352 // Check if time is UTC and leapsecond … … 1373 1366 } 1374 1367 1368 struct tm *psTimeToTM(const psTime *time) 1369 { 1370 PS_ASSERT_PTR_NON_NULL(time,NULL); 1371 1372 // XXX is it safe to assume that time_t is always an integer value? 1373 time_t sec = time->sec; 1374 1375 // if this is NOT a UTC time then we want to make sure tm.tm_sec does not 1376 // end up being set to 60 1377 if (!time->type == PS_TIME_UTC) 1378 { 1379 // If leapsecond use previous day 1380 if (time->leapsecond) { 1381 sec--; 1382 } 1383 } 1384 1385 // struct tm can handle leapseconds 1386 struct tm *tmTime = psAlloc(sizeof(struct tm)); 1387 gmtime_r(&sec, tmTime); 1388 1389 return tmTime; 1390 } 1391 1375 1392 struct timeval* psTimeToTimeval(const psTime *time) 1376 1393 { … … 1391 1408 return timevalTime; 1392 1409 } 1393 1394 /*1395 struct tm* p_psTimeToTM(const psTime *time)1396 {1397 psS64 cent = 0;1398 psS64 year = 0;1399 psS64 month = 0;1400 psS64 day = 0;1401 psS64 hour = 0;1402 psS64 minute = 0;1403 psS64 seconds = 0;1404 psS64 temp = 0;1405 struct tm* tmTime = NULL;1406 1407 1408 // Error checks1409 PS_ASSERT_PTR_NON_NULL(time,NULL);1410 PS_ASSERT_INT_WITHIN_RANGE(time->nsec,0,(psU32)((1e9)-1),NULL);1411 1412 seconds = time->sec%60;1413 minute = time->sec/60%60;1414 hour = time->sec/3600%24;1415 day = (time->sec+62135596800)/86400;1416 1417 // Add 306 days to make relative to Mar 1, 0; also adjust day to be within a range (1..2**28-1) where our1418 // calculations will work with 32bit ints1419 if(day > (pow(2, 28)-307))1420 {1421 temp = (day - 146097+306)/146097+1; // Avoid overflow if day close to maxint1422 day -= temp * 146097-306;1423 } else if((day += 306) <= 0)1424 {1425 temp = -( -day / 146097 + 1); // Avoid ambiguity in C division of negatives1426 day -= temp * 146097;1427 }1428 1429 cent = (day*4-1)/146097; // Calc number of centuries day is after 29 Feb of yr 01430 day -= cent*146097/4; // 4 centuries = 146097 days1431 year = (day*4-1)/1461; // Calc number of years into the century1432 day -= year*1461/4; // Again March-based (4 yrs =\u02dc 146[01] days)1433 month = (day*12+1093)/367; // Get the month (3..14 represent March through1434 day -= (month*367-1094)/12; // February of following year)1435 year += cent*100+temp*400; // Get the real year, which is off by1436 1437 // One if month is January or February1438 if(month > 12)1439 {1440 year++;1441 month -= 12;1442 }1443 1444 // Allocate output1445 tmTime = (struct tm*)psAlloc(sizeof(struct tm));1446 1447 tmTime->tm_year = year - 1900;1448 tmTime->tm_mon = month - 1;1449 tmTime->tm_mday = day + 1;1450 tmTime->tm_hour = hour;1451 tmTime->tm_min = minute;1452 tmTime->tm_sec = seconds;1453 tmTime->tm_isdst = -1;1454 1455 return tmTime;1456 }1457 */1458 1410 1459 1411 psTime* psTimeFromJD(double jd) … … 1539 1491 1540 1492 // Convert tm time to psTime 1541 outTime = p _psTimeFromTM(&tmTime);1493 outTime = psTimeFromTM(&tmTime); 1542 1494 outTime->nsec = millisecond * 1000000; 1543 1495 // outTime->type = type; … … 1611 1563 } 1612 1564 1613 psTime* p _psTimeFromTM(const struct tm* time)1565 psTime* psTimeFromTM(const struct tm* time) 1614 1566 { 1615 1567 psS64 year; … … 1662 1614 // days to adjust from Mar 1, year 0-relative to Jan 1, year 1-relative. Add hours, minutes, and seconds. 1663 1615 day += (month * 367 - 1094) / 12 + year % 100 * 1461 / 4 + (year/100 * 36524 + year/400) - 306; 1664 outTime->sec = (((day - 1) * SEC_PER_DAY) - 62135596800) + hour*SEC_PER_HOUR 1665 + minute*SEC_PER_MINUTE + seconds; 1616 //outTime->sec = (((day - 1) * SEC_PER_DAY) - 62135596800) + hour*SEC_PER_HOUR 1617 outTime->sec = ((day - 1) * SEC_PER_DAY - 62135596800) 1618 + (hour * SEC_PER_HOUR) 1619 + (minute * SEC_PER_MINUTE) 1620 + seconds; 1666 1621 1667 1622 // C's TM does not define a microsecond field. Microseconds must be manipulated by calling function. -
trunk/psLib/src/astro/psTime.h
r6184 r7700 11 11 * @author Ross Harman, MHPCC 12 12 * 13 * @version $Revision: 1.4 5$ $Name: not supported by cvs2svn $14 * @date $Date: 2006-0 1-23 20:04:31$13 * @version $Revision: 1.46 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2006-06-27 03:45:25 $ 15 15 * 16 16 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 256 256 ); 257 257 258 /** Convert psTime to struct tm time. 259 * 260 * Converts psTime to struct tm time. This function should handle 261 * UTC leapseconds correctly. 262 * 263 * @return tm*: tm struct. 264 */ 265 struct tm *psTimeToTM( 266 const psTime* time ///< Input time to be converted. 267 ); 268 258 269 /** Convert psTime to timeval time. 259 270 * … … 351 362 * @return psTime*: time. 352 363 */ 353 psTime* p _psTimeFromTM(364 psTime* psTimeFromTM( 354 365 const struct tm *time ///< Input time to be converted. 355 366 );
Note:
See TracChangeset
for help on using the changeset viewer.
