Index: trunk/psLib/src/astro/psTime.c
===================================================================
--- trunk/psLib/src/astro/psTime.c	(revision 2204)
+++ trunk/psLib/src/astro/psTime.c	(revision 2273)
@@ -11,6 +11,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.38 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-10-27 00:57:30 $
+ *  @version $Revision: 1.39 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-11-04 01:04:57 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -31,5 +31,5 @@
 #include "psMetadata.h"
 #include "psMetadataIO.h"
-
+#include "psConstants.h"
 #include "psAstronomyErrors.h"
 
@@ -151,5 +151,5 @@
     char line[LINESIZE];
     psS32 j = 0;
-    psS32 maxLines = 100;
+    psS32 maxLines = 1000;
     psF64 *ptr = NULL;
     psImage *table = NULL;
@@ -159,5 +159,7 @@
     fd=fopen(fileName, "r");
     if(fd==NULL) {
-        psError(__func__, " : Line %d - Couldn't open %s", __LINE__, fileName);
+        psError(PS_ERR_IO,true,
+                PS_ERRORTEXT_psTime_FILE_NOT_FOUND,
+                fileName,"Tai-Utc");
         return NULL;
     }
@@ -168,5 +170,7 @@
     while(fgets(line, LINESIZE, fd) != NULL) {
         if(j>=maxLines) {
-            psError(__func__, " : Line %d - Too many rows in file. Max size: %d", __LINE__, maxLines);
+            psError(PS_ERR_IO,true,
+                    PS_ERRORTEXT_psTime_FILE_TOO_MANY_ROWS,
+                    fileName,maxLines);
             return NULL;
             psFree(table);
@@ -201,5 +205,7 @@
     fd=fopen(fileName, "r");
     if(fd==NULL) {
-        psError(__func__, " : Line %d - Couldn't open %s", __LINE__, fileName);
+        psError(PS_ERR_IO,true,
+                PS_ERRORTEXT_psTime_FILE_NOT_FOUND,
+                fileName,"Ser7");
         return NULL ;
     }
@@ -218,5 +224,7 @@
 
             if(j>=maxLines) {
-                psError(__func__, " : Line %d - Too many rows in file. Max size: %d", __LINE__, maxLines);
+                psError(PS_ERR_IO,true,
+                        PS_ERRORTEXT_psTime_FILE_TOO_MANY_ROWS,
+                        fileName,maxLines);
                 return NULL;
                 psFree(table);
@@ -240,115 +248,4 @@
 }
 
-/* Not yet used...
-static psImage* readEopcFile(char *fileName)
-{
-    char line[LINESIZE];
-    psS32 i = 0;
-    psS32 j = 0;
-    psS32 maxLines = 2500;
-    psImage *table = NULL;
-    FILE *fd = NULL;
- 
- 
-    fd=fopen(fileName, "r");
-    if(fd==NULL) {
-        psError(__func__, " : Line %d - Couldn't open %s", __LINE__, fileName);
-        return NULL ;
-    }
- 
-    // Table shouldn't be larger than 2500 rows. All columns are read.
-    table  = psImageAlloc(11, maxLines, PS_TYPE_F64);
-    for(i=0; i<3; i++) {
-        if((fgets(line, LINESIZE, fd) != NULL)) {
-            psError(__func__, " : Line %d - Error reading %s", __LINE__, fileName);
-        }
-    }
- 
-    while(fgets(line, LINESIZE, fd) != NULL) {
-        if(j>=maxLines) {
-            psError(__func__, " : Line %d - Too many rows in file. Max size: %d", __LINE__, maxLines);
-            return NULL;
-            psFree(table);
-        }
-        psF64 *ptr = table->data.F64[j++];
-        sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
-               ptr, ptr+1, ptr+2, ptr+3, ptr+4, ptr+5, ptr+6, ptr+7, ptr+8, ptr+9, ptr+10);
-    }
- 
-    *(psU32 *)&table->numRows = j;
- 
-    return table;
-}
- 
-static psImage* readFinalsFile(char *fileName)
-{
-    char line[LINESIZE];
-    psS32 j = 0;
-    psS32 maxLines = 15000;
-    psImage *table = NULL;
-    FILE *fd = NULL;
- 
- 
-    fd=fopen(fileName, "r");
-    if(fd==NULL) {
-        psError(__func__, " : Line %d - Couldn't open %s", __LINE__, fileName);
-        return NULL ;
-    }
- 
-    // Table shouldn't be larger than 15000 rows. Select columns are read.
-    table  = psImageAlloc(6, maxLines, PS_TYPE_F64);
-    while(fgets(line, LINESIZE, fd) != NULL) {
-        if(j>=maxLines) {
-            psError(__func__, " : Line %d - Too many rows in file. Max size: %d", __LINE__, maxLines);
-            return NULL;
-            psFree(table);
-        }
- 
-        psF64 *ptr = table->data.F64[j++];
-        ptr = table->data.F64[j++];
- 
-        // Sometimes there are blank entries in this table, so check for a space to determine if there is no entry
-        if(*(line+7) == ' ') {                   // MJD
-            *ptr = 0.0;
-        } else {
-            *ptr = atof(line+7);
-        }
- 
-        if(*(line+18) == ' ') {                  // Polar motion X
-            *(ptr+1) = 0.0;
-        } else {
-            *(ptr+1) = atof(line+18);
-        }
- 
-        if(*(line+27) == ' ') {                  // Polar motion X error
-            *(ptr+2) = 0.0;
-        } else {
-            *(ptr+2) = atof(line+27);
-        }
- 
-        if(*(line+37) == ' ') {                  // Polar motion Y
-            *(ptr+3) = 0.0;
-        } else {
-            *(ptr+3) = atof(line+37);
-        }
- 
-        if(*(line+46) == ' ') {                  // Polar motion Y error
-            *(ptr+4) = 0.0;
-        } else {
-            *(ptr+4) = atof(line+46);
-        }
- 
-        if(*(line+58) == ' ') {                  // UT1-UTC
-            *(ptr+5) = 0.0;
-        } else {
-            *(ptr+5) = atof(line+58);
-        }
-    }
- 
-    *(psU32 *)&table->numRows = j;
- 
-    return table;
-}
-*/
 
 static double lookupTaiUtcTable(const psTime *time)
@@ -389,5 +286,7 @@
             hiIdx++;
             if(hiIdx >= numRows) {
-                psError(__func__, " : Line %d - High index too big. Value: %d", __LINE__, hiIdx);
+                psError(PS_ERR_BAD_PARAMETER_VALUE,true,
+                        PS_ERRORTEXT_psTime_TIME_POSTDATES_TABLE,
+                        jd, "TAI-UTC");
                 return 0.0;
             }
@@ -395,14 +294,18 @@
         loIdx = hiIdx--;
         if(loIdx < 0) {
-            psError(__func__, " : Line %d - Low index too small. Value: %d", __LINE__, loIdx);
+            psError(PS_ERR_BAD_PARAMETER_VALUE,true,
+                    PS_ERRORTEXT_psTime_TIME_PREDATES_TABLE,
+                    jd, "TAI-UTC");
             return 0.0;
         }
 
+        // since rows of table can not have same jd, this check is probably not needed.
         denom = table->data.F64[hiIdx][0]-table->data.F64[loIdx][0];
         if(fabs(denom) < FLT_EPSILON) {
-            psError(__func__, " : Line %d - Divide by zero error during interpolation", __LINE__);
+            psError(PS_ERR_UNKNOWN,true,
+                    PS_ERRORTEXT_psTime_TABLE_DUPLICATE_ROWS
+                    "Ser7");
             return 0.0;
         } else {
-
             // Interpolate three constants required to calculate TAI-UTC
             const1 = table->data.F64[loIdx][1]+(table->data.F64[hiIdx][1]-table->data.F64[loIdx][1])
@@ -445,13 +348,19 @@
     mjdUtc = psTimeToMJD(time);
     if(mjdUtc < table->data.F64[0][3]) {                                    // MJD time before start of table
-        psError(__func__, " : Line %d - MJD too small. Value: %d", __LINE__, mjdUtc);
+        psError(PS_ERR_BAD_PARAMETER_VALUE,true,
+                PS_ERRORTEXT_psTime_TIME_PREDATES_TABLE,
+                mjdUtc, "Ser7");
 
     } else if(mjdUtc > table->data.F64[numRows-1][3]) {                     // MJD time after end of table
-        psError(__func__, " : Line %d - MJD too big. Value: %d", __LINE__, mjdUtc);
+        psError(PS_ERR_BAD_PARAMETER_VALUE,true,
+                PS_ERRORTEXT_psTime_TIME_POSTDATES_TABLE,
+                mjdUtc, "Ser7");
     } else {                                                                // All other times..interpolate
         while(mjdUtc > table->data.F64[hiIdx][3]) {
             hiIdx++;
             if(hiIdx >= numRows) {
-                psError(__func__, " : Line %d - High index too big. Value: %d", __LINE__, hiIdx);
+                psError(PS_ERR_BAD_PARAMETER_VALUE,true,
+                        PS_ERRORTEXT_psTime_TIME_POSTDATES_TABLE,
+                        mjdUtc, "Ser7");
                 return 0.0;
             }
@@ -460,10 +369,14 @@
         loIdx = hiIdx--;
         if(loIdx < 0) {
-            psError(__func__, " : Line %d - Low index too small. Value: %d", __LINE__, loIdx);
+            psError(PS_ERR_BAD_PARAMETER_VALUE,true,
+                    PS_ERRORTEXT_psTime_TIME_PREDATES_TABLE,
+                    mjdUtc, "Ser7");
         }
 
         denom = table->data.F64[hiIdx][3] - table->data.F64[loIdx][3];
         if(fabs(denom) < FLT_EPSILON) {
-            psError(__func__, " : Line %d - Divide by zero error during interpolation", __LINE__);
+            psError(PS_ERR_UNKNOWN,true,
+                    PS_ERRORTEXT_psTime_TABLE_DUPLICATE_ROWS,
+                    "Ser7");
         } else {
             out = table->data.F64[loIdx][col]+(table->data.F64[hiIdx][col]-table->data.F64[loIdx][col])
@@ -525,12 +438,11 @@
     // Error checks
     if(type!=PS_TIME_TAI && type!=PS_TIME_UTC) {
-        psError(__func__, " : Line %d - Incorrect type. Type: %d", __LINE__, type);
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                PS_ERRORTEXT_psTime_TYPE_UNKNOWN,
+                type);
         return NULL;
     }
 
     outTime = (psTime*)psAlloc(sizeof(psTime));
-    if(outTime == NULL) {
-        psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__);
-    }
 
     outTime->sec = 0;
@@ -551,5 +463,6 @@
 
     if (gettimeofday(&now, (struct timezone *)0) == -1) {
-        psError(__func__, " : Line %d - Failed to get time", __LINE__);
+        psError(PS_ERR_OS_CALL_FAILED, true,
+                PS_ERRORTEXT_psTime_GET_TOD_FAILED);
         return NULL;
     }
@@ -573,23 +486,21 @@
 
     // Error checks
-    if(time == NULL) {
-        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
-        return NULL;
-    } else if(time->usec >= 1e6) {
-        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
-        return NULL;
-    } else if(time->type == type) {
-        psError(__func__, " : Line %d - Time already in correct psTime type. Time type: %d", __LINE__, type);
-        return NULL;
+    PS_PTR_CHECK_NULL(time,NULL);
+    PS_INT_CHECK_RANGE(time->usec,0,1e6-1,NULL);
+
+    if (time->type == type) { // time already right type.  That was easy!
+        return time;
     }
 
     delta = psTimeGetTAIDelta(time);
 
-    if(type == PS_TIME_UTC) {
+    if (type == PS_TIME_UTC) {
         time->sec = time->sec - delta;    // User wants UTC time. Subtract leapseconds.
     } else if(type == PS_TIME_TAI) {
         time->sec = time->sec + delta;    // User wants TAI time. Add leapseconds.
     } else {
-        psError(__func__, " : Line %d - Invalid psTimeType: %d", __LINE__, type);
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                PS_ERRORTEXT_psTime_TYPE_UNKNOWN,
+                type);
     }
 
@@ -623,11 +534,6 @@
 
     // Error checks
-    if(time == NULL) {
-        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
-        return 0;
-    } else if(time->usec >= 1e6) {
-        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
-        return 0;
-    }
+    PS_PTR_CHECK_NULL(time,NAN);
+    PS_INT_CHECK_RANGE(time->usec,0,1e6-1,NAN);
 
     // Calculate TAI or UTC time based on type of time user passes
@@ -698,11 +604,6 @@
 
     // Error checks
-    if(time == NULL) {
-        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
-        return 0.0;
-    } else if(time->usec >= 1e6) {
-        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
-        return 0.0;
-    }
+    PS_PTR_CHECK_NULL(time,NAN);
+    PS_INT_CHECK_RANGE(time->usec,0,1e6-1,NAN);
 
     return lookupSer7Table(time, 6);
@@ -717,13 +618,6 @@
 
     // Error checks
-    if(time == NULL)
-    {
-        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
-        return NULL;
-    } else if(time->usec >= 1e6)
-    {
-        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
-        return NULL;
-    }
+    PS_PTR_CHECK_NULL(time,NULL);
+    PS_INT_CHECK_RANGE(time->usec,0,1e6-1,NULL);
 
     x = lookupSer7Table((psTime*)time, 4);
@@ -741,11 +635,6 @@
 
     // Error checks
-    if(time == NULL) {
-        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
-        return 0.0;
-    } else if(time->usec >= 1e6) {
-        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
-        return 0.0;
-    }
+    PS_PTR_CHECK_NULL(time,NAN);
+    PS_INT_CHECK_RANGE(time->usec,0,1e6-1,NAN);
 
     return lookupTaiUtcTable(time);
@@ -758,17 +647,8 @@
 
     // Error checks
-    if(time1 == NULL) {
-        psError(__func__, " : Line %d - NULL value not allowed for first input", __LINE__);
-        return 0;
-    } else if(time1->usec >= 1e6) {
-        psError(__func__, " : Line %d - First input time microseconds too big. Value %u", __LINE__, time1->usec);
-        return 0;
-    } else if(time2 == NULL) {
-        psError(__func__, " : Line %d - NULL value not allowed for second input", __LINE__);
-        return 0;
-    } else if(time2->usec >= 1e6) {
-        psError(__func__, " : Line %d - First input time microseconds too big. Value %u", __LINE__, time2->usec);
-        return 0;
-    }
+    PS_PTR_CHECK_NULL(time1,0);
+    PS_PTR_CHECK_NULL(time2,0);
+    PS_INT_CHECK_RANGE(time1->usec,0,1e6-1,0);
+    PS_INT_CHECK_RANGE(time1->usec,0,1e6-1,0);
 
     diff = abs(psTimeGetTAIDelta((psTime*)time1)-psTimeGetTAIDelta((psTime*)time2));
@@ -781,13 +661,7 @@
     double jd = 0.0;
 
-
-    // Error checks
-    if(time == NULL) {
-        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
-        return 0.0;
-    } else if(time->usec >= 1e6) {
-        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
-        return 0.0;
-    }
+    // Error checks
+    PS_PTR_CHECK_NULL(time,NAN);
+    PS_INT_CHECK_RANGE(time->usec,0,1e6-1,NAN);
 
     // Julian date conversion
@@ -807,11 +681,6 @@
 
     // Error checks
-    if(time == NULL) {
-        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
-        return 0.0;
-    } else if(time->usec >= 1e6) {
-        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
-        return 0.0;
-    }
+    PS_PTR_CHECK_NULL(time,NAN);
+    PS_INT_CHECK_RANGE(time->usec,0,1e6-1,NAN);
 
     // Modified Julian date conversion
@@ -835,11 +704,6 @@
 
     // Error checks
-    if(time == NULL) {
-        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
-        return NULL;
-    } else if(time->usec >= 1e6) {
-        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
-        return NULL;
-    }
+    PS_PTR_CHECK_NULL(time,NULL);
+    PS_INT_CHECK_RANGE(time->usec,0,1e6-1,NULL);
 
     tempString = psAlloc(MAX_TIME_STRING_LENGTH);
@@ -854,9 +718,11 @@
     // 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)) {
-        psError(__func__, " : Line %d - Failed strftime conversion", __LINE__);
+        psError(PS_ERR_OS_CALL_FAILED, true,
+                PS_ERRORTEXT_psTime_CONVERT_TIME_TO_STRING_FAILED);
     }
 
     if (snprintf(timeString, MAX_TIME_STRING_LENGTH, "%s.%3.3dZ", tempString, ms) < 0) {
-        psError(__func__, " : Line %d - Failed snprintf conversion", __LINE__);
+        psError(PS_ERR_OS_CALL_FAILED, true,
+                PS_ERRORTEXT_psTime_APPEND_MSEC_FAILED);
     }
     psFree(tempString);
@@ -869,15 +735,10 @@
     struct timeval timevalTime;
 
-
-    // Error checks
-    if(time == NULL)
-    {
-        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
-        return timevalTime;
-    } else if(time->usec >= 1e6)
-    {
-        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
-        return timevalTime;
-    }
+    timevalTime.tv_sec = 0;
+    timevalTime.tv_usec = 0;
+
+    // Error checks
+    PS_PTR_CHECK_NULL(time,timevalTime);
+    PS_INT_CHECK_RANGE(time->usec,0,1e6-1,timevalTime);
 
     timevalTime.tv_sec = time->sec;
@@ -901,13 +762,6 @@
 
     // Error checks
-    if(time == NULL)
-    {
-        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
-        return NULL;
-    } else if(time->usec >= 1e6)
-    {
-        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
-        return NULL;
-    }
+    PS_PTR_CHECK_NULL(time,NULL);
+    PS_INT_CHECK_RANGE(time->usec,0,1e6-1,NULL);
 
     seconds = time->sec%60;
@@ -945,8 +799,4 @@
     // Allocate output
     tmTime = (struct tm*)psAlloc(sizeof(struct tm));
-    if (tmTime == NULL)
-    {
-        psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__);
-    }
 
     tmTime->tm_year = year - 1900;
@@ -982,7 +832,5 @@
 
     // Error check
-    if(outTime->usec >= 1e6) {
-        psError(__func__, " : Line %d - Output time microseconds too big. Value %u", __LINE__, outTime->usec);
-    }
+    PS_INT_CHECK_RANGE(outTime->usec,0,1e6-1,outTime);
 
     return outTime;
@@ -1010,7 +858,5 @@
 
     // Error check
-    if(outTime->usec >= 1e6) {
-        psError(__func__, " : Line %d - Output time microseconds too big. Value %u", __LINE__, outTime->usec);
-    }
+    PS_INT_CHECK_RANGE(outTime->usec,0,1e6-1,outTime);
 
     return outTime;
@@ -1020,67 +866,28 @@
 {
     char tempString[MAX_TIME_STRING_LENGTH];
-    psS32 month;
-    psS32 day;
-    psS32 year;
-    psS32 hour;
-    psS32 minute;
-    psS32 second;
     psS32 millisecond;
     struct tm tmTime;
     psTime *outTime = NULL;
 
-
-    // Preserve input string by creating duplicate
-    strncpy(tempString, time, MAX_TIME_STRING_LENGTH);
-
     // Convert YYYY-MM-DDThh:mm:ss.sss in string form to tm time
-    year = atoi(strtok(tempString, "-"));
-    if (year < 0) {
-        psError(__func__, "Years less than 0 not allowed. Value: %d", year);
-        return outTime;
-    }
-
-    month = atoi(strtok(NULL, "-"));
-    if (month < 1 || month > 12) {
-        psError(__func__, "Month must have a value from 1 to 12. Value: %d", month);
-        return outTime;
-    }
-
-    day = atoi(strtok(NULL, "T"));
-    if (day < 1 || day > 31) {
-        psError(__func__, "Day must have a value from 1 to 31. Value: %d", day);
-        return outTime;
-    }
-
-    hour = atoi(strtok(NULL, ":"));
-    if (hour < 0 || hour > 23) {
-        psError(__func__, "Hour must have a value from 0 to 23. Value: %d", hour);
-        return outTime;
-    }
-
-    minute = atoi(strtok(NULL, ":"));
-    if (minute < 0 || minute > 59) {
-        psError(__func__, "Minute must have a value from 0 to 59. Value: %d", minute);
-        return outTime;
-    }
-
-    second = atoi(strtok(NULL, "."));
-    if (second < 0 || second > 59) {
-        psError(__func__, "Second must have a value from 0 to 59. Value: %d", second);
-        return outTime;
-    }
-
-    millisecond = atoi(strtok(NULL, "Z"));
-    if (millisecond < 0 || millisecond > 1000) {
-        psError(__func__, "Millisecond must have a value from 0 to 999. Value: %d", millisecond);
-        return outTime;
-    }
-
-    tmTime.tm_year = year - 1900;
-    tmTime.tm_mon = month - 1;
-    tmTime.tm_mday = day;
-    tmTime.tm_hour = hour;
-    tmTime.tm_min = minute;
-    tmTime.tm_sec = second;
+    if (sscanf(time, "%d-%d-%dT%d:%d:%d.%d",
+               &tmTime.tm_year,&tmTime.tm_mon,&tmTime.tm_mday,
+               &tmTime.tm_hour,&tmTime.tm_min,&tmTime.tm_sec,&millisecond) < 7) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                PS_ERRORTEXT_psTime_ISOTIME_MALFORMED,
+                time);
+        return NULL;
+    }
+
+    PS_INT_CHECK_NON_NEGATIVE(tmTime.tm_year, outTime);
+    PS_INT_CHECK_RANGE(tmTime.tm_mon,1,12,outTime);
+    PS_INT_CHECK_RANGE(tmTime.tm_mday,1,31,outTime);
+    PS_INT_CHECK_RANGE(tmTime.tm_hour,0,23,outTime);
+    PS_INT_CHECK_RANGE(tmTime.tm_min,0,59,outTime);
+    PS_INT_CHECK_RANGE(tmTime.tm_sec,0,59,outTime);
+    PS_INT_CHECK_RANGE(millisecond,0,999,outTime);
+
+    tmTime.tm_year -= 1900;
+    tmTime.tm_mon--;
     tmTime.tm_isdst = -1;
 
@@ -1089,23 +896,14 @@
     outTime->usec = millisecond * 1000;
 
+    return outTime;
+}
+
+psTime* psTimeFromTimeval(const struct timeval *time)
+{
+    psTime *outTime = NULL;
+
+
     // Error check
-    if(outTime->usec >= 1e6) {
-        psError(__func__, " : Line %d - Output time microseconds too big. Value %u", __LINE__, outTime->usec);
-    }
-
-    return outTime;
-}
-
-psTime* psTimeFromTimeval(const struct timeval *time)
-{
-    psTime *outTime = NULL;
-
-
-    // Error check
-    if(time == NULL)
-    {
-        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
-        return NULL;
-    }
+    PS_PTR_CHECK_NULL(time,NULL);
 
     // Allocate psTime struct
@@ -1117,8 +915,5 @@
 
     // Error check
-    if(outTime->usec >= 1e6)
-    {
-        psError(__func__, " : Line %d - Output time microseconds too big. Value %u", __LINE__, outTime->usec);
-    }
+    PS_INT_CHECK_RANGE(outTime->usec,0,1e6-1,outTime);
 
     return outTime;
@@ -1138,9 +933,5 @@
 
     // Error check
-    if(time == NULL)
-    {
-        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
-        return NULL;
-    }
+    PS_PTR_CHECK_NULL(time,NULL);
 
     // Allocate psTime struct
@@ -1183,8 +974,5 @@
 
     // Error check
-    if(outTime->usec >= 1e6)
-    {
-        psError(__func__, " : Line %d - Output time microseconds too big. Value %u", __LINE__, outTime->usec);
-    }
+    PS_INT_CHECK_RANGE(outTime->usec,0,1e6-1,outTime);
 
     return outTime;
@@ -1199,18 +987,12 @@
 
     // Error checks
+    PS_PTR_CHECK_NULL(tai1,NULL);
+    PS_PTR_CHECK_NULL(tai2,NULL);
+    PS_INT_CHECK_RANGE(tai1->usec,0,1e6-1,NULL);
+    PS_INT_CHECK_RANGE(tai2->usec,0,1e6-1,NULL);
+
     if(tai1->type != tai2->type) {
-        psError(__func__, " : Line %d - Argument types inconsistent for time addition", __LINE__);
-        return NULL;
-    } else if(tai1 == NULL) {
-        psError(__func__, " : Line %d - NULL value not allowed for first input", __LINE__);
-        return NULL;
-    } else if(tai1->usec >= 1e6) {
-        psError(__func__, " : Line %d - Argument 1 microseconds too big. Value %u", __LINE__, tai1->usec);
-        return NULL;
-    } else if(tai2 == NULL) {
-        psError(__func__, " : Line %d - NULL value not allowed for second input", __LINE__);
-        return NULL;
-    } else if(tai2->usec >= 1e6) {
-        psError(__func__, " : Line %d - Argument 2 microseconds too big. Value %u", __LINE__, tai2->usec);
+        psError(PS_ERR_BAD_PARAMETER_TYPE, true,
+                PS_ERRORTEXT_psTime_TYPE_MISMATCH);
         return NULL;
     }
@@ -1233,7 +1015,5 @@
 
     // Error check
-    if(outTime->usec >= 1e6) {
-        psError(__func__, " : Line %d - Output time microseconds too big. Value %u", __LINE__, outTime->usec);
-    }
+    PS_INT_CHECK_RANGE(outTime->usec,0,1e6-1,outTime);
 
     return outTime;
@@ -1248,18 +1028,11 @@
 
     // Error checks
+    PS_PTR_CHECK_NULL(tai1,NULL);
+    PS_PTR_CHECK_NULL(tai2,NULL);
+    PS_INT_CHECK_RANGE(tai1->usec,0,1e6-1,NULL);
+    PS_INT_CHECK_RANGE(tai2->usec,0,1e6-1,NULL);
     if(tai1->type != tai2->type) {
-        psError(__func__, " : Line %d - Argument types inconsistent for time subtraction", __LINE__);
-        return NULL;
-    } else if(tai1 == NULL) {
-        psError(__func__, " : Line %d - NULL value not allowed for first input", __LINE__);
-        return NULL;
-    } else if(tai1->usec >= 1e6) {
-        psError(__func__, " : Line %d - Argument 1 microseconds too big. Value %u", __LINE__, tai1->usec);
-        return NULL;
-    } else if(tai2 == NULL) {
-        psError(__func__, " : Line %d - NULL value not allowed for second input", __LINE__);
-        return NULL;
-    } else if(tai2->usec >= 1e6) {
-        psError(__func__, " : Line %d - Argument 2 microseconds too big. Value %u", __LINE__, tai2->usec);
+        psError(PS_ERR_BAD_PARAMETER_TYPE, true,
+                PS_ERRORTEXT_psTime_TYPE_MISMATCH);
         return NULL;
     }
@@ -1294,18 +1067,12 @@
 
     // Error checks
+    // Error checks
+    PS_PTR_CHECK_NULL(tai1,NULL);
+    PS_PTR_CHECK_NULL(tai2,NULL);
+    PS_INT_CHECK_RANGE(tai1->usec,0,1e6-1,NULL);
+    PS_INT_CHECK_RANGE(tai2->usec,0,1e6-1,NULL);
     if(tai1->type != tai2->type) {
-        psError(__func__, " : Line %d - Argument types inconsistent for time subtraction", __LINE__);
-        return NULL;
-    } else if(tai1 == NULL) {
-        psError(__func__, " : Line %d - NULL value not allowed for first input", __LINE__);
-        return NULL;
-    } else if(tai1->usec >= 1e6) {
-        psError(__func__, " : Line %d - Argument 1 microseconds too big. Value %u", __LINE__, tai1->usec);
-        return NULL;
-    } else if(tai2 == NULL) {
-        psError(__func__, " : Line %d - NULL value not allowed for second input", __LINE__);
-        return NULL;
-    } else if(tai2->usec >= 1e6) {
-        psError(__func__, " : Line %d - Argument 2 microseconds too big. Value %u", __LINE__, tai2->usec);
+        psError(PS_ERR_BAD_PARAMETER_TYPE, true,
+                PS_ERRORTEXT_psTime_TYPE_MISMATCH);
         return NULL;
     }
