IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 22680


Ignore:
Timestamp:
Feb 26, 2009, 9:44:56 AM (17 years ago)
Author:
Paul Price
Message:

psTimeToISO now produces 9 decimal places. A new function, psTimeToString, allows an arbitrary number of decimal places.

Location:
trunk/psLib/src/astro
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/astro/psTime.c

    r22678 r22680  
    4141#define R2DEG = (180.0/M_PI)            /// Conversion from radians to degrees
    4242#define MAX_STRING_LENGTH 256           /// Maximum length of string
    43 #define MAX_TIME_STRING_LENGTH 256      /// Maximum length of time string
     43#define MAX_TIME_STRING_LENGTH 30       /// Maximum length of time string: 1234-67-90T23:56:89.123456789
    4444#define SEC_PER_MINUTE 60.0             /// Seconds per minute
    4545#define SEC_PER_HOUR (60.0*SEC_PER_MINUTE) /// Seconds per hour
     
    13321332}
    13331333
    1334 psString psTimeToISO(const psTime *time)
    1335 {
    1336     psS32 ds = 0;
    1337     char *timeString = NULL;
    1338     char *tempString = NULL;
    1339 
     1334
     1335// Format a time as a string, allowing for a specified number of decimals for the seconds field
     1336static psString timeToString(const psTime *time, // Time to format as string
     1337                             int decimals // Number of decimals to permit for seconds
     1338                             )
     1339{
    13401340    // Error checks
    1341     PS_ASSERT_PTR_NON_NULL(time,NULL);
    1342     PS_ASSERT_INT_WITHIN_RANGE(time->nsec,0,(psU32)((1e9)-1),NULL);
    1343 
    1344     // Check valid year range
    1345     if ((time->sec) < ((psS64)YEAR_0000_SEC) || (time->sec) > ((psS64)YEAR_9999_SEC)) {
    1346       psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Error: %s, %" PRId64 ", is out of range.  Must be between %" PRId64 " and %" PRId64 ".", "time->sec", time->sec, ((psS64)YEAR_0000_SEC), ((psS64)YEAR_9999_SEC));
    1347       return NULL;
    1348     }
    1349 
    1350     PS_ASSERT_S64_WITHIN_RANGE(time->sec, (psS64)YEAR_0000_SEC, (psS64)YEAR_9999_SEC, NULL);
    1351 
    1352     // Allocate temp strings
    1353     tempString = psAlloc(MAX_TIME_STRING_LENGTH);
    1354     timeString = psAlloc(MAX_TIME_STRING_LENGTH);
    1355 
    1356     // Convert nanoseconds to decaseconds
    1357     ds = time->nsec / 100000000;
    1358 
    1359     // tmTime variable is statically allocated, no need to free
    1360     struct tm *tmTime = psTimeToTM(time);
    1361 
    1362     // Converts psTime to YYYY-MM-DDThh:mm:ss.sss in string form
    1363     if (!strftime(tempString, MAX_TIME_STRING_LENGTH, "%Y-%m-%dT%H:%M:%S", tmTime)) {
     1341    PS_ASSERT_PTR_NON_NULL(time, NULL);
     1342    PS_ASSERT_INT_WITHIN_RANGE(time->nsec, 0, (psU32)((1e9)-1), NULL);
     1343    if (time->sec < YEAR_0000_SEC || time->sec > YEAR_9999_SEC) {
     1344        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     1345                "Time (%" PRId64 " sec) is not between year 0000 or 9999.\n", time->sec);
     1346        return NULL;
     1347    }
     1348    PS_ASSERT_INT_WITHIN_RANGE(decimals, 0, 9, NULL);
     1349
     1350    struct tm *tmTime = psTimeToTM(time); // Unix time structure
     1351
     1352    // Convert psTime to YYYY-MM-DDThh:mm:ss in string form
     1353    psString string = psStringAlloc(MAX_TIME_STRING_LENGTH); // Time string
     1354    if (!strftime(string, MAX_TIME_STRING_LENGTH, "%Y-%m-%dT%H:%M:%S", tmTime)) {
    13641355        psError(PS_ERR_OS_CALL_FAILED, true, _("Failed to convert a time via strftime function."));
    13651356        return NULL;
     
    13681359
    13691360    // Check if time is UTC and leapsecond
    1370     if (((time->type==PS_TIME_UTC)||(time->type==PS_TIME_UT1)) && (time->leapsecond)) {
     1361    if ((time->type == PS_TIME_UTC || time->type == PS_TIME_UT1) && time->leapsecond) {
    13711362        // Modify second to be 60
    1372         tempString[17] = '6';
    1373         tempString[18] = '0';
    1374     }
    1375 
    1376     // Create string with milliseconds
    1377     if (snprintf(timeString, MAX_TIME_STRING_LENGTH, "%s.%1d", tempString, ds) < 0) {
    1378         psError(PS_ERR_OS_CALL_FAILED, true, _("Failed to append millisecond to time string with snprintf function."));
    1379         return NULL;
    1380     }
    1381     psFree(tempString);
    1382 
    1383     return timeString;
     1363        string[17] = '6';
     1364        string[18] = '0';
     1365    }
     1366
     1367    // Add in the nanoseconds
     1368    if (decimals > 0) {
     1369        int partial = round((double)time->nsec / pow(10.0, 9 - decimals)); // Partial part
     1370        psString format = NULL;             // Format for printing partial part
     1371        psStringAppend(&format, ".%%0%dd", decimals);
     1372        psStringAppend(&string, format, partial);
     1373        psFree(format);
     1374    }
     1375
     1376    return string;
     1377}
     1378
     1379psString psTimeToString(const psTime *time, int decimals)
     1380{
     1381    return timeToString(time, decimals);
     1382}
     1383
     1384psString psTimeToISO(const psTime *time)
     1385{
     1386    return timeToString(time, 9);       // Use all 9 decimals allowed by the nanoseconds
    13841387}
    13851388
  • trunk/psLib/src/astro/psTime.h

    r22678 r22680  
    241241/** Convert psTime to ISO8601 formatted string.
    242242 *
    243  *  Converts psTime to a null terminated string in the form of YYYY-MM-DDThh:mm:ss.sss.
     243 *  Converts psTime to a null terminated string in the form of YYYY-MM-DDThh:mm:ss.sssssssss.
    244244 *  This function does not add or subtract leapseconds.
    245245 *
     
    249249    const psTime* time                  ///< Input time to be converted.
    250250);
     251
     252/** Convert psTime to ISO8601 formatted string with limited decimal places
     253 *
     254 *  Converts psTime to a null terminated string in the form of YYYY-MM-DDThh:mm:ss.s, with as
     255 *  many decimal places as specified.
     256 *  This function does not add or subtract leapseconds.
     257 *
     258 *  @return psString:     Pointer null terminated array of chars in ISO time.
     259 */
     260psString psTimeToString(
     261    const psTime *time,                 ///< Input time to be converted
     262    int decimals                        ///< Number of decimals to use
     263    );
     264
    251265
    252266/** Convert psTime to struct tm time.
Note: See TracChangeset for help on using the changeset viewer.