Index: trunk/psLib/src/astro/psTime.c
===================================================================
--- trunk/psLib/src/astro/psTime.c	(revision 22678)
+++ trunk/psLib/src/astro/psTime.c	(revision 22680)
@@ -41,5 +41,5 @@
 #define R2DEG = (180.0/M_PI)            /// Conversion from radians to degrees
 #define MAX_STRING_LENGTH 256           /// Maximum length of string
-#define MAX_TIME_STRING_LENGTH 256      /// Maximum length of time string
+#define MAX_TIME_STRING_LENGTH 30       /// Maximum length of time string: 1234-67-90T23:56:89.123456789
 #define SEC_PER_MINUTE 60.0             /// Seconds per minute
 #define SEC_PER_HOUR (60.0*SEC_PER_MINUTE) /// Seconds per hour
@@ -1332,34 +1332,25 @@
 }
 
-psString psTimeToISO(const psTime *time)
-{
-    psS32 ds = 0;
-    char *timeString = NULL;
-    char *tempString = NULL;
-
+
+// Format a time as a string, allowing for a specified number of decimals for the seconds field
+static psString timeToString(const psTime *time, // Time to format as string
+                             int decimals // Number of decimals to permit for seconds
+                             )
+{
     // Error checks
-    PS_ASSERT_PTR_NON_NULL(time,NULL);
-    PS_ASSERT_INT_WITHIN_RANGE(time->nsec,0,(psU32)((1e9)-1),NULL);
-
-    // Check valid year range
-    if ((time->sec) < ((psS64)YEAR_0000_SEC) || (time->sec) > ((psS64)YEAR_9999_SEC)) {
-      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));
-      return NULL;
-    }
-
-    PS_ASSERT_S64_WITHIN_RANGE(time->sec, (psS64)YEAR_0000_SEC, (psS64)YEAR_9999_SEC, NULL);
-
-    // Allocate temp strings
-    tempString = psAlloc(MAX_TIME_STRING_LENGTH);
-    timeString = psAlloc(MAX_TIME_STRING_LENGTH);
-
-    // Convert nanoseconds to decaseconds
-    ds = time->nsec / 100000000;
-
-    // tmTime variable is statically allocated, no need to free
-    struct tm *tmTime = psTimeToTM(time);
-
-    // Converts psTime to YYYY-MM-DDThh:mm:ss.sss in string form
-    if (!strftime(tempString, MAX_TIME_STRING_LENGTH, "%Y-%m-%dT%H:%M:%S", tmTime)) {
+    PS_ASSERT_PTR_NON_NULL(time, NULL);
+    PS_ASSERT_INT_WITHIN_RANGE(time->nsec, 0, (psU32)((1e9)-1), NULL);
+    if (time->sec < YEAR_0000_SEC || time->sec > YEAR_9999_SEC) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "Time (%" PRId64 " sec) is not between year 0000 or 9999.\n", time->sec);
+        return NULL;
+    }
+    PS_ASSERT_INT_WITHIN_RANGE(decimals, 0, 9, NULL);
+
+    struct tm *tmTime = psTimeToTM(time); // Unix time structure
+
+    // Convert psTime to YYYY-MM-DDThh:mm:ss in string form
+    psString string = psStringAlloc(MAX_TIME_STRING_LENGTH); // Time string
+    if (!strftime(string, MAX_TIME_STRING_LENGTH, "%Y-%m-%dT%H:%M:%S", tmTime)) {
         psError(PS_ERR_OS_CALL_FAILED, true, _("Failed to convert a time via strftime function."));
         return NULL;
@@ -1368,18 +1359,30 @@
 
     // Check if time is UTC and leapsecond
-    if (((time->type==PS_TIME_UTC)||(time->type==PS_TIME_UT1)) && (time->leapsecond)) {
+    if ((time->type == PS_TIME_UTC || time->type == PS_TIME_UT1) && time->leapsecond) {
         // Modify second to be 60
-        tempString[17] = '6';
-        tempString[18] = '0';
-    }
-
-    // Create string with milliseconds
-    if (snprintf(timeString, MAX_TIME_STRING_LENGTH, "%s.%1d", tempString, ds) < 0) {
-        psError(PS_ERR_OS_CALL_FAILED, true, _("Failed to append millisecond to time string with snprintf function."));
-        return NULL;
-    }
-    psFree(tempString);
-
-    return timeString;
+        string[17] = '6';
+        string[18] = '0';
+    }
+
+    // Add in the nanoseconds
+    if (decimals > 0) {
+        int partial = round((double)time->nsec / pow(10.0, 9 - decimals)); // Partial part
+        psString format = NULL;             // Format for printing partial part
+        psStringAppend(&format, ".%%0%dd", decimals);
+        psStringAppend(&string, format, partial);
+        psFree(format);
+    }
+
+    return string;
+}
+
+psString psTimeToString(const psTime *time, int decimals)
+{
+    return timeToString(time, decimals);
+}
+
+psString psTimeToISO(const psTime *time)
+{
+    return timeToString(time, 9);       // Use all 9 decimals allowed by the nanoseconds
 }
 
