00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include <time.h>
00040 #include "mygmtime.h"
00041 static int _lpdays[] = { -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
00042 static int _days[] = { -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 };
00043
00044
00045
00046 #define ChkAdd(dest, src1, src2) ( ((src1 >= 0L) && (src2 >= 0L) \
00047 && (dest < 0L)) || ((src1 < 0L) && (src2 < 0L) && (dest >= 0L)) )
00048
00049
00050
00051
00052 #define ChkMul(dest, src1, src2) ( src1 ? (dest/src1 != src2) : 0 )
00053
00054
00055 long mygmktime(struct tm *tb
00056 ){
00057 time_t tmptm1, tmptm2, tmptm3;
00058 struct tm *tbtemp,Qtbtemp;
00059
00060
00061
00062
00063 if( ((tmptm1 = tb->tm_year) < _BASE_YEAR - 1) || (tmptm1 > _MAX_YEAR+ 1) )
00064 goto err_mktime;
00065
00066
00067
00068
00069
00070 if( (tb->tm_mon < 0) || (tb->tm_mon > 11) ) {
00071
00072
00073 tmptm1 += (tb->tm_mon / 12);
00074 if( (tb->tm_mon %= 12) < 0 ) {
00075 tb->tm_mon += 12;
00076 tmptm1--;
00077 }
00078
00079
00080
00081
00082 if( (tmptm1 < _BASE_YEAR - 1) || (tmptm1 > _MAX_YEAR + 1) )
00083 goto err_mktime;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092 tmptm2 = _days[tb->tm_mon];
00093 if( !(tmptm1 & 3) && (tb->tm_mon > 1) )
00094 tmptm2++;
00095
00096
00097
00098
00099
00100
00101
00102
00103 tmptm3 = (tmptm1 - _BASE_YEAR) * 365L + ((tmptm1 - 1L) >> 2)
00104 - _LEAP_YEAR_ADJUST;
00105
00106
00107 tmptm3 += tmptm2;
00108
00109
00110 tmptm1 = tmptm3 + (tmptm2 = (long)(tb->tm_mday));
00111 if( ChkAdd(tmptm1, tmptm3, tmptm2) ) goto err_mktime;
00112
00113
00114
00115
00116 tmptm2 = tmptm1 * 24L;
00117 if( ChkMul(tmptm2, tmptm1, 24L) )goto err_mktime;
00118
00119 tmptm1 = tmptm2 + (tmptm3 = (long)tb->tm_hour);
00120 if( ChkAdd(tmptm1, tmptm2, tmptm3) )goto err_mktime;
00121
00122
00123
00124
00125
00126 tmptm2 = tmptm1 * 60L;
00127 if( ChkMul(tmptm2, tmptm1, 60L) )goto err_mktime;
00128
00129 tmptm1 = tmptm2 + (tmptm3 = (long)tb->tm_min);
00130 if ( ChkAdd(tmptm1, tmptm2, tmptm3) )goto err_mktime;
00131
00132
00133
00134
00135
00136 tmptm2 = tmptm1 * 60L;
00137 if( ChkMul(tmptm2, tmptm1, 60L) )goto err_mktime;
00138
00139 tmptm1 = tmptm2 + (tmptm3 = (long)tb->tm_sec);
00140 if ( ChkAdd(tmptm1, tmptm2, tmptm3) )goto err_mktime;
00141
00142
00143 if( (tbtemp = mygmtime(&tmptm1,&Qtbtemp)) == NULL )goto err_mktime;
00144
00145 *tb = *tbtemp;
00146 return tmptm1;
00147
00148 err_mktime:
00149
00150 return -1L;
00151 }
00152
00153
00154 struct tm * mygmtime (time_t *timp, struct tm *ptb
00155 ){
00156 long caltim = *timp;
00157 int islpyr = 0;
00158 int tmptim;
00159 int *mdays;
00160
00161 if( caltim < 0L )return NULL;
00162
00163
00164
00165
00166
00167
00168 tmptim = (int)(caltim / _FOUR_YEAR_SEC);
00169 caltim -= ((long)tmptim * _FOUR_YEAR_SEC);
00170
00171
00172
00173
00174 tmptim = (tmptim * 4) + 70;
00175
00176 if( caltim >= _YEAR_SEC ) {
00177 tmptim++;
00178 caltim -= _YEAR_SEC;
00179
00180 if( caltim >= _YEAR_SEC ) {
00181 tmptim++;
00182 caltim -= _YEAR_SEC;
00183
00184
00185
00186
00187
00188 if( caltim >= (_YEAR_SEC + _DAY_SEC) ){
00189 tmptim++;
00190 caltim -= (_YEAR_SEC + _DAY_SEC);
00191 }else {
00192
00193
00194
00195 islpyr++;
00196 }
00197 }
00198 }
00199
00200
00201
00202
00203
00204 ptb->tm_year = tmptim;
00205
00206
00207
00208
00209
00210 ptb->tm_yday = (int)(caltim / _DAY_SEC);
00211 caltim -= (long)(ptb->tm_yday) * _DAY_SEC;
00212
00213
00214
00215
00216 if( islpyr )mdays = _lpdays; else mdays = _days;
00217
00218 for ( tmptim = 1 ; mdays[tmptim] < ptb->tm_yday ; tmptim++ ) ;
00219
00220 ptb->tm_mon = --tmptim;
00221
00222 ptb->tm_mday = ptb->tm_yday - mdays[tmptim];
00223
00224
00225
00226
00227 ptb->tm_wday = ((int)(*timp / _DAY_SEC) + _BASE_DOW) % 7;
00228
00229
00230
00231
00232
00233 ptb->tm_hour = (int)(caltim / 3600);
00234 caltim -= (long)ptb->tm_hour * 3600L;
00235
00236 ptb->tm_min = (int)(caltim / 60);
00237 ptb->tm_sec = (int)(caltim - (ptb->tm_min) * 60);
00238
00239 ptb->tm_isdst = 0;
00240 return( (struct tm *)ptb );
00241 }
00242
00243 static long TimeDifference(void){
00244 time_t lTime;
00245 struct tm GmTime,*pGmTime;
00246
00247
00248
00249 lTime = (time_t)time(NULL);
00250 pGmTime = mygmtime(&lTime,&GmTime);
00251 pGmTime->tm_isdst = -1;
00252 return (long)(lTime - mktime(pGmTime));
00253 }