src/sct_operations.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Aleksander Morgado Juez                         *
00003  *   scitime@aleksander_morgado.mm.st                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Library General Public License as       *
00007  *   published by the Free Software Foundation; either version 2 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU Library General Public     *
00016  *   License along with this program; if not, write to the                 *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "scitime.h"
00022 
00023 SCT_RET sct_add_deltaDSF( \
00024     sct_time_ptr pDest, \
00025     const sct_time_ptr pSource, \
00026     const long nDays, \
00027     const long nSeconds, \
00028     const long nFemtosecs)
00029 {
00030     sct_time deltaElement;
00031 
00032     //Create delta element...
00033     deltaElement.days = nDays;
00034     deltaElement.secs = nSeconds;
00035     deltaElement.fsecs = nFemtosecs;
00036     deltaElement.status = SCT_STATUS_DELTA;
00037     deltaElement.accuracy = SCT_ACCURACY_FEMTOSECS;
00038 
00039     //Return the sum
00040     return sct_add_delta(pDest,pSource, &deltaElement);
00041 }
00042 
00043 
00044 SCT_RET sct_add_delta( \
00045     sct_time_ptr pDest, \
00046     const sct_time_ptr pSource, \
00047     const sct_time_ptr pDelta)
00048 {
00049     sct_time aux;       //To avoid changing source if it's the same as dest
00050     int64_t fsecs = 0;
00051     int64_t days = 0;
00052     int32_t secs = 0;
00053 
00054     //Check element
00055     if(pSource == NULL || pDelta == NULL || pDest == NULL)
00056     {
00057         //Ko, invalid pointer!
00058         #ifdef SCT_DEBUG
00059             if(pSource == NULL)
00060             {
00061                 fprintf(stderr, \
00062                     "[scitime:%s] >> Unable to get source element to add!\n",
00063                     __FUNCTION__);
00064             }
00065             if(pDelta == NULL)
00066             {
00067                 fprintf(stderr, \
00068                     "[scitime:%s] >> Unable to get delta element to add!\n",
00069                     __FUNCTION__);
00070             }
00071             if(pDest == NULL)
00072             {
00073                 fprintf(stderr, \
00074                     "[scitime:%s] >> Unable to get output element!\n",
00075                     __FUNCTION__);
00076             }
00077         #endif
00078         return SCT_RET_INVALID_PTR;
00079     }
00080     if(pSource->status != SCT_STATUS_OK || pDelta->status != SCT_STATUS_DELTA)
00081     {
00082         #ifdef SCT_DEBUG
00083             if(pSource->status != SCT_STATUS_OK)
00084             {
00085                 fprintf(stderr, "[scitime:%s] >> Source element to add not "
00086                     "initialised as epoch element!\n", __FUNCTION__);
00087             }
00088             if(pDelta->status != SCT_STATUS_DELTA)
00089             {
00090                 fprintf(stderr, "[scitime:%s] >> Delta element to add not "
00091                     "initialised as delta element!\n", __FUNCTION__);
00092             }
00093         #endif
00094         return SCT_RET_NOT_INITIALISED;
00095     }
00096 
00097     //Copy source in the aux element
00098     sct_copy(&aux,pSource);
00099 
00100     //Sum femtosecs...
00101     fsecs = aux.fsecs + pDelta->fsecs;
00102     if(fsecs > SCT_FSECS_IN_SEC)
00103     {
00104         fsecs -= SCT_FSECS_IN_SEC;
00105         secs++;
00106     }
00107 
00108     //Sum seconds...
00109     secs += (aux.secs + pDelta->secs);
00110     if(secs > SCT_SECS_IN_DAY)
00111     {
00112         secs -= SCT_SECS_IN_DAY;
00113         days++;
00114     }
00115 
00116     //Sum days...
00117     days += (aux.days + pDelta->days);
00118 
00119     //Set output values...
00120     pDest->fsecs= fsecs;
00121     pDest->secs = secs;
00122     pDest->days = days;
00123     pDest->status = SCT_STATUS_OK;
00124     pDest->accuracy = pDelta->accuracy;
00125 
00126     //Check accuracy changes...
00127     if(aux.accuracy > pDelta->accuracy)
00128     {
00129         pDest->accuracy = aux.accuracy;
00130         #ifdef SCT_DEBUG
00131             fprintf(stderr, "[scitime:%s] >> Accuracy for epoch element (%d) is"
00132                 " worse than the accuracy of the delta element (%d)! Output "
00133                 "accuracy will be '%d'\n", __FUNCTION__, aux.accuracy,
00134                 pDelta->accuracy, pDest->accuracy);
00135         #endif
00136     }
00137     else if(aux.accuracy < pDelta->accuracy)
00138     {
00139         #ifdef SCT_DEBUG
00140             fprintf(stderr, "[scitime:%s] >> Accuracy for delta element (%d) is"
00141                 " worse than the accuracy of the epoch element (%d)! Output "
00142                 "accuracy will be '%d'\n", __FUNCTION__, pDelta->accuracy,
00143                aux.accuracy, pDest->accuracy);
00144         #endif
00145     }
00146 
00147     //Ok, no problem
00148     return SCT_RET_OK;
00149 }
00150 
00151 SCT_RET sct_diff( \
00152     sct_time_ptr pDeltaDest, \
00153     const sct_time_ptr pEpoch1, \
00154     const sct_time_ptr pEpoch2)
00155 {
00156     int64_t fsecs = 0;
00157     int64_t days = 0;
00158     int32_t secs = 0;
00159 
00160     //Check element
00161     if(pDeltaDest == NULL || pEpoch1 == NULL || pEpoch2 == NULL)
00162     {
00163         //Ko, invalid pointer!
00164         #ifdef SCT_DEBUG
00165             if(pEpoch1 == NULL)
00166             {
00167                 fprintf(stderr, \
00168                     "[scitime:%s] >> Unable to get first epoch to substract!\n",
00169                     __FUNCTION__);
00170             }
00171             if(pEpoch2 == NULL)
00172             {
00173                 fprintf(stderr, "[scitime:%s] >> Unable to get second epoch "
00174                     "to substract!\n", __FUNCTION__);
00175             }
00176             if(pDeltaDest == NULL)
00177             {
00178                 fprintf(stderr, \
00179                     "[scitime:%s] >> Unable to get output element!\n",
00180                     __FUNCTION__);
00181             }
00182         #endif
00183         return SCT_RET_INVALID_PTR;
00184     }
00185     if(pEpoch1->status != SCT_STATUS_OK || pEpoch2->status != SCT_STATUS_OK)
00186     {
00187         #ifdef SCT_DEBUG
00188             if(pEpoch1->status != SCT_STATUS_OK)
00189             {
00190                 fprintf(stderr, "[scitime:%s] >> First element not "
00191                     "initialised as epoch element!\n", __FUNCTION__);
00192             }
00193             if(pEpoch2->status != SCT_STATUS_OK)
00194             {
00195                 fprintf(stderr, "[scitime:%s] >> Second element not "
00196                     "initialised as epoch element!\n", __FUNCTION__);
00197             }
00198         #endif
00199         return SCT_RET_NOT_INITIALISED;
00200     }
00201 
00202     //Substract femtosecs...
00203     fsecs = pEpoch1->fsecs - pEpoch2->fsecs;
00204     if(fsecs < -SCT_FSECS_IN_SEC)
00205     {
00206         fsecs += SCT_FSECS_IN_SEC;
00207         secs--;
00208     }
00209 
00210     //Substract secs...
00211     secs += (pEpoch1->secs - pEpoch2->secs);
00212     if(secs < -SCT_SECS_IN_DAY)
00213     {
00214         secs += SCT_SECS_IN_DAY;
00215         days--;
00216     }
00217 
00218     //Substract days...
00219     days += (pEpoch1->days - pEpoch2->days);
00220 
00221     //Set output values...
00222     pDeltaDest->fsecs = fsecs;
00223     pDeltaDest->secs = secs;
00224     pDeltaDest->days = days;
00225     pDeltaDest->status = SCT_STATUS_DELTA;
00226     pDeltaDest->accuracy = (pEpoch1->accuracy > pEpoch2->accuracy) ? \
00227         pEpoch1->accuracy : pEpoch2->accuracy;
00228     //Check accuracy changes...
00229     #ifdef SCT_DEBUG
00230         if(pEpoch1->accuracy > pEpoch2->accuracy)
00231         {
00232             fprintf(stderr, "[scitime:%s] >> Accuracy for first epoch element"
00233                 " (%d) is worse than the accuracy of the second epoch element"
00234                 " (%d)! Output accuracy will be '%d'\n", __FUNCTION__,
00235                 pEpoch1->accuracy, pEpoch2->accuracy, pDeltaDest->accuracy);
00236         }
00237         else if(pEpoch1->accuracy < pEpoch2->accuracy)
00238         {
00239             fprintf(stderr, "[scitime:%s] >> Accuracy for second epoch element"
00240                 " (%d) is worse than the accuracy of the first epoch element"
00241                 " (%d)! Output accuracy will be '%d'\n", __FUNCTION__,
00242                 pEpoch2->accuracy, pEpoch1->accuracy, pDeltaDest->accuracy);
00243         }
00244     #endif
00245 
00246     //Ok, no problem
00247     return SCT_RET_OK;
00248 }
00249 
00250 int sct_compare ( \
00251     const sct_time_ptr pEpoch1, \
00252     const sct_time_ptr pEpoch2, \
00253     const double epsilon)
00254 {
00255     sct_time delta;
00256     double secs;
00257     sct_diff(&delta, pEpoch1, pEpoch2);
00258     sct_get_delta_secs(&delta, &secs);
00259     if(secs > epsilon)
00260     {
00261         return 1;
00262     }
00263     if(secs < (0-epsilon))
00264     {
00265         return -1;
00266     }
00267     return 0;
00268 }

Generated on Sun May 20 15:32:42 2007 for scitime by  doxygen 1.5.1