Index: trunk/psLib/src/astro/psTime.c
===================================================================
--- trunk/psLib/src/astro/psTime.c	(revision 7608)
+++ trunk/psLib/src/astro/psTime.c	(revision 7700)
@@ -10,6 +10,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.86 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-06-21 17:46:06 $
+ *  @version $Revision: 1.87 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-06-27 03:45:25 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -1326,6 +1326,4 @@
     char *timeString = NULL;
     char *tempString = NULL;
-    struct tm *tmTime = NULL;
-    time_t sec;
 
     // Error checks
@@ -1342,13 +1340,7 @@
     // Convert nanoseconds to decaseconds
     ds = time->nsec / 100000000;
-    sec = time->sec;
-
-    // If leapsecond use previous day
-    if(time->leapsecond) {
-        sec--;
-    }
 
     // tmTime variable is statically allocated, no need to free
-    tmTime = gmtime(&sec);
+    struct tm *tmTime = psTimeToTM(time);
 
     // Converts psTime to YYYY-MM-DDThh:mm:ss.sss in string form
@@ -1356,4 +1348,5 @@
         psError(PS_ERR_OS_CALL_FAILED, true, PS_ERRORTEXT_psTime_CONVERT_TIME_TO_STRING_FAILED);
     }
+    psFree(tmTime);
 
     // Check if time is UTC and leapsecond
@@ -1373,4 +1366,28 @@
 }
 
+struct tm *psTimeToTM(const psTime *time)
+{
+    PS_ASSERT_PTR_NON_NULL(time,NULL);
+
+    // XXX is it safe to assume that time_t is always an integer value?
+    time_t sec = time->sec;
+
+    // if this is NOT a UTC time then we want to make sure tm.tm_sec does not
+    // end up being set to 60
+    if (!time->type == PS_TIME_UTC)
+    {
+        // If leapsecond use previous day
+        if (time->leapsecond) {
+            sec--;
+        }
+    }
+
+    // struct tm can handle leapseconds
+    struct tm *tmTime = psAlloc(sizeof(struct tm));
+    gmtime_r(&sec, tmTime);
+
+    return tmTime;
+}
+
 struct timeval* psTimeToTimeval(const psTime *time)
 {
@@ -1391,69 +1408,4 @@
     return timevalTime;
 }
-
-/*
-struct tm* p_psTimeToTM(const psTime *time)
-{
-    psS64 cent = 0;
-    psS64 year = 0;
-    psS64 month = 0;
-    psS64 day = 0;
-    psS64 hour = 0;
-    psS64 minute = 0;
-    psS64 seconds = 0;
-    psS64 temp = 0;
-    struct tm* tmTime = NULL;
- 
- 
-    // Error checks
-    PS_ASSERT_PTR_NON_NULL(time,NULL);
-    PS_ASSERT_INT_WITHIN_RANGE(time->nsec,0,(psU32)((1e9)-1),NULL);
- 
-    seconds = time->sec%60;
-    minute = time->sec/60%60;
-    hour = time->sec/3600%24;
-    day = (time->sec+62135596800)/86400;
- 
-    // Add 306 days to make relative to Mar 1, 0; also adjust day to be within a range (1..2**28-1) where our
-    // calculations will work with 32bit ints
-    if(day > (pow(2, 28)-307))
-    {
-        temp = (day - 146097+306)/146097+1;         // Avoid overflow if day close to maxint
-        day -= temp * 146097-306;
-    } else if((day += 306) <= 0)
-    {
-        temp = -( -day / 146097 + 1);               // Avoid ambiguity in C division of negatives
-        day -= temp * 146097;
-    }
- 
-    cent = (day*4-1)/146097;                        // Calc number of centuries day is after 29 Feb of yr 0
-    day -= cent*146097/4;                           // 4 centuries = 146097 days
-    year = (day*4-1)/1461;                          // Calc number of years into the century
-    day -= year*1461/4;                             // Again March-based (4 yrs =\u02dc 146[01] days)
-    month = (day*12+1093)/367;                      // Get the month (3..14 represent March through
-    day -= (month*367-1094)/12;                     // February of following year)
-    year += cent*100+temp*400;                      // Get the real year, which is off by
- 
-    // One if month is January or February
-    if(month > 12)
-    {
-        year++;
-        month -= 12;
-    }
- 
-    // Allocate output
-    tmTime = (struct tm*)psAlloc(sizeof(struct tm));
- 
-    tmTime->tm_year = year - 1900;
-    tmTime->tm_mon = month - 1;
-    tmTime->tm_mday = day + 1;
-    tmTime->tm_hour = hour;
-    tmTime->tm_min = minute;
-    tmTime->tm_sec = seconds;
-    tmTime->tm_isdst = -1;
- 
-    return tmTime;
-}
-*/
 
 psTime* psTimeFromJD(double jd)
@@ -1539,5 +1491,5 @@
 
     // Convert tm time to psTime
-    outTime = p_psTimeFromTM(&tmTime);
+    outTime = psTimeFromTM(&tmTime);
     outTime->nsec = millisecond * 1000000;
     //    outTime->type = type;
@@ -1611,5 +1563,5 @@
 }
 
-psTime* p_psTimeFromTM(const struct tm* time)
+psTime* psTimeFromTM(const struct tm* time)
 {
     psS64 year;
@@ -1662,6 +1614,9 @@
     // days to adjust from Mar 1, year 0-relative to Jan 1, year 1-relative. Add hours, minutes, and seconds.
     day += (month * 367 - 1094) / 12 + year % 100 * 1461 / 4 + (year/100 * 36524 + year/400) - 306;
-    outTime->sec = (((day - 1) * SEC_PER_DAY) - 62135596800) + hour*SEC_PER_HOUR
-                   + minute*SEC_PER_MINUTE + seconds;
+    //outTime->sec = (((day - 1) * SEC_PER_DAY) - 62135596800) + hour*SEC_PER_HOUR
+    outTime->sec = ((day - 1) * SEC_PER_DAY - 62135596800)
+                   + (hour * SEC_PER_HOUR)
+                   + (minute * SEC_PER_MINUTE)
+                   + seconds;
 
     // C's TM does not define a microsecond field. Microseconds must be manipulated by calling function.
