00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "scitime.h"
00022
00023 static int _sct_get_exponent_from_double(const double number, int *pExponent);
00024
00025 SCT_RET sct_set_accuracy (sct_time_ptr pElem, SCT_ACCURACY accuracy)
00026 {
00027 pElem->accuracy = accuracy;
00028 return SCT_RET_OK;
00029 }
00030
00031
00032 SCT_ACCURACY sct_get_accuracy (sct_time_ptr pElem)
00033 {
00034 return pElem->accuracy;
00035 }
00036
00037
00038
00039
00040
00041
00042 SCT_ACCURACY sct_get_accuracy_from_jd(const double jd)
00043 {
00044 int accuracyValue = 100;
00045 int exponent = 0;
00046
00047 if( _sct_get_exponent_from_double(jd, &exponent) != SCT_RET_OK)
00048 {
00049 #ifdef SCT_DEBUG
00050 fprintf(stderr, "[scitime:%s] >> Could not get exponent...\n",
00051 __FUNCTION__);
00052 #endif
00053 return SCT_ACCURACY_DAYS;
00054 }
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 accuracyValue = exponent -10;
00080
00081 if(accuracyValue > SCT_ACCURACY_DAYS)
00082 {
00083 #ifdef SCT_DEBUG
00084 fprintf(stderr, "[scitime:%s] >> Accuracy is WORSE than days "
00085 "(1E%d secs)!\n", __FUNCTION__, accuracyValue);
00086 #endif
00087 return SCT_ACCURACY_DAYS;
00088 }
00089 if(accuracyValue < SCT_ACCURACY_FEMTOSECS)
00090 {
00091 #ifdef SCT_DEBUG
00092 fprintf(stderr, "[scitime:%s] >> Accuracy is BETTER than femtosecs "
00093 "(1E%d secs)! (Resetting to 1E-15 secs)\n",
00094 __FUNCTION__, accuracyValue);
00095 #endif
00096 return SCT_ACCURACY_FEMTOSECS;
00097 }
00098
00099 return (SCT_ACCURACY)(accuracyValue);
00100 }
00101
00102 SCT_ACCURACY sct_get_accuracy_from_secs(const double secs)
00103 {
00104 int accuracyValue = 100;
00105 int exponent = 0;
00106
00107 if( _sct_get_exponent_from_double(secs, &exponent) != SCT_RET_OK)
00108 {
00109 #ifdef SCT_DEBUG
00110 fprintf(stderr, "[scitime:%s] >> Could not get exponent...\n",
00111 __FUNCTION__);
00112 #endif
00113 return SCT_ACCURACY_DAYS;
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 accuracyValue = exponent -15;
00136
00137 if(accuracyValue > SCT_ACCURACY_DAYS)
00138 {
00139 #ifdef SCT_DEBUG
00140 fprintf(stderr, "[scitime:%s] >> Accuracy is WORSE than days "
00141 "(1E%d secs)!\n", __FUNCTION__, accuracyValue);
00142 #endif
00143 return SCT_ACCURACY_DAYS;
00144 }
00145 if(accuracyValue < SCT_ACCURACY_FEMTOSECS)
00146 {
00147 #ifdef SCT_DEBUG
00148 fprintf(stderr, "[scitime:%s] >> Accuracy is BETTER than femtosecs "
00149 "(1E%d secs)! (Resetting to 1E-15 secs)\n",
00150 __FUNCTION__, accuracyValue);
00151 #endif
00152 return SCT_ACCURACY_FEMTOSECS;
00153 }
00154
00155 return (SCT_ACCURACY)(accuracyValue);
00156 }
00157
00158
00159 static int _sct_get_exponent_from_double(const double number, int *pExponent)
00160 {
00161 char aux[50];
00162 char *expS = NULL;
00163
00164
00165 memset(&aux[0],0,sizeof(char)*50);
00166 sprintf(aux,"%E",number);
00167 expS = strstr(aux,"E");
00168 if(expS == NULL)
00169 {
00170 #ifdef SCT_DEBUG
00171 fprintf(stderr, "[scitime:%s] >> Invalid string (%s) to get the "
00172 "exponent!\n", __FUNCTION__, aux);
00173 #endif
00174 return SCT_RET_INVALID_NUMBER;
00175 }
00176 else
00177 {
00178 *pExponent = atoi(&expS[1]);
00179 }
00180 return SCT_RET_OK;
00181 }
00182