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
00040
00041
00042
00043
00044 #include <linux/time.h>
00045 #include <linux/string.h>
00046
00047 #include <sys_routines.h>
00048 #include <ssdef.h>
00049 #include <descrip.h>
00050 #include <lib_routines.h>
00051
00052 #define TIMEBASE 100000
00053 #define TIMESIZE 8640000
00054
00055 #define QUAD_CENTURY_DAYS 146097
00056
00057 #define CENTURY_DAYS 36524
00058
00059 #define QUAD_YEAR_DAYS 1461
00060
00061 #define YEAR_DAYS 365
00062
00063 #define OFFSET_DAYS 94187
00064
00065
00066
00067 #define BASE_YEAR 1601
00068
00069
00070
00071
00072 struct TIME {
00073 unsigned char time[8];
00074 };
00075
00076 #include <exe_routines.h>
00077
00078 const char month_names[] = "-JAN-FEB-MAR-APR-MAY-JUN-JUL-AUG-SEP-OCT-NOV-DEC-";
00079
00080 static const unsigned short month_end[] =
00081 {0,31,59,90,120,151,181,212,243,273,304,334,365};
00082
00083 unsigned long sys___combine_date_time(int days,struct TIME *timadr,int day_time)
00084 {
00085 if (day_time >= TIMESIZE) {
00086 return SS__IVTIME;
00087 } else {
00088
00089
00090
00091 unsigned long count,time;
00092 unsigned char *ptr;
00093
00094 count = 8;
00095 ptr = timadr->time;
00096 time = days;
00097 do {
00098 *ptr++ = time;
00099 time = (time >> 8);
00100 } while (--count > 0);
00101
00102
00103
00104 count = 8;
00105 ptr = timadr->time;
00106 time = day_time;
00107 do {
00108 time += *ptr * TIMESIZE;
00109 *ptr++ = time;
00110 time = (time >> 8);
00111 } while (--count > 0);
00112
00113
00114
00115 count = 8;
00116 ptr = timadr->time;
00117 time = 0;
00118 do {
00119 time += *ptr * TIMEBASE;
00120 *ptr++ = time;
00121 time = (time >> 8);
00122 } while (--count > 0);
00123
00124 return SS__NORMAL;
00125 }
00126 }
00127
00128
00129
00130
00131
00132 unsigned long lib_day(long *days, const void *timadra, int *day_time)
00133 {
00134 const struct TIME *timadr = (const struct TIME *)timadra;
00135
00136 register unsigned date,time,count;
00137 register const unsigned char *srcptr;
00138 register unsigned char *dstptr;
00139 struct TIME wrktim;
00140 int delta;
00141
00142
00143
00144 if (timadr == NULL) {
00145 register unsigned sts;
00146 sts = exe_gettim(&wrktim);
00147 if ((sts & 1) == 0) {
00148 return sts;
00149 }
00150 delta = 0;
00151 srcptr = wrktim.time + 7;
00152 } else {
00153
00154
00155
00156 srcptr = timadr->time + 7;
00157 if ((delta = (*srcptr & 0x80))) {
00158
00159
00160
00161 count = 8;
00162 srcptr = timadr->time;
00163 dstptr = wrktim.time;
00164 time = 1;
00165 do {
00166 time = time + ((~*srcptr++) & 0xFF);
00167 *dstptr++ = time;
00168 time = (time >> 8);
00169 } while (--count > 0);
00170 srcptr = wrktim.time + 7;
00171 }
00172 }
00173
00174
00175
00176
00177 count = 8;
00178 dstptr = wrktim.time + 7;
00179 time = 0;
00180 do {
00181 time = (time << 8) | *srcptr--;
00182 *dstptr-- = time / TIMEBASE;
00183 time %= TIMEBASE;
00184 } while (--count > 0);
00185
00186
00187
00188
00189 date = time = 0;
00190 srcptr = wrktim.time + 7;
00191 count = 8;
00192 do {
00193 time = (time << 8) | *srcptr--;
00194 date = (date << 8) | (time / TIMESIZE);
00195 time %= TIMESIZE;
00196 } while (--count > 0);
00197
00198
00199
00200 if (delta) {
00201 *days = -(int) date;
00202 if (day_time != NULL) *day_time = -(int) time;
00203 } else {
00204 *days = date;
00205 if (day_time != NULL) *day_time = time;
00206 }
00207
00208 return SS__NORMAL;
00209 }
00210
00211
00212
00213 unsigned long lib_cvt_vectim(const void* timbufa, void *timadra)
00214 {
00215 const unsigned short * timbuf = (const unsigned short *)timbufa;
00216 struct TIME *timadr = (struct TIME *)timadra;
00217
00218 int delta = 0;
00219 register unsigned sts,days,day_time;
00220 sts = SS__NORMAL;
00221
00222
00223
00224 if (timbuf[0] == 0 && timbuf[1] == 0) {
00225 delta = 1;
00226 days = timbuf[2];
00227 } else {
00228 register leap = 0,year = timbuf[0],month = timbuf[1];
00229 if (month >= 2) {
00230 if ((year % 4) == 0) {
00231 if ((year % 100) == 0) {
00232 if ((year % 400) == 0) {
00233 leap = 1;
00234 }
00235 } else {
00236 leap = 1;
00237 }
00238 }
00239 }
00240 days = timbuf[2];
00241 if (year >= 1858 && year <= 9999 && month >= 1 &&
00242 month <= 12 && days >= 1) {
00243 days += month_end[month - 1];
00244 if (month > 2) days += leap;
00245 if (days <= month_end[month] + leap) {
00246 year -= BASE_YEAR;
00247 days += year * 365 + year / 4 - year / 100 + year / 400
00248 - OFFSET_DAYS - 1;
00249 } else {
00250 sts = SS__IVTIME;
00251 }
00252 } else {
00253 sts = SS__IVTIME;
00254 }
00255 }
00256 if (timbuf[3] > 23 || timbuf[4] > 59 ||
00257 timbuf[5] > 59 || timbuf[6] > 99) {
00258 sts = SS__IVTIME;
00259 }
00260 if (sts & 1) {
00261 day_time = timbuf[3] * 360000 + timbuf[4] * 6000 +
00262 timbuf[5] * 100 + timbuf[6];
00263 sts = sys___combine_date_time(days,timadr,day_time);
00264 if (delta) {
00265
00266
00267
00268 register unsigned count,time;
00269 register unsigned char *ptr;
00270 count = 8;
00271 ptr = timadr->time;
00272 time = 1;
00273 do {
00274 time = time + ((~*ptr) & 0xFF);
00275 *ptr++ = time;
00276 time = (time >> 8);
00277 } while (--count > 0);
00278 }
00279 }
00280 return sts;
00281 }
00282
00283
00284
00285
00286 int exe_asctim(unsigned short *timlen,
00287 struct dsc_descriptor *timbuf,
00288 const void *timadra, unsigned long cvtflg)
00289 {
00290 const struct TIME *timadr = (const struct TIME *)timadra;
00291 long count,timval;
00292 unsigned short wrktim[7];
00293 long length = timbuf->dsc_w_length;
00294 char *chrptr = timbuf->dsc_a_pointer;
00295
00296 #if 0
00297 printk("flag %x\n",cvtflg);
00298 #endif
00299
00300
00301 {
00302 register unsigned sts;
00303 sts = exe_numtim(wrktim, timadr);
00304 if ((sts & 1) == 0) {
00305 return sts;
00306 }
00307 }
00308
00309
00310
00311 #if 0
00312 {
00313 int i;
00314 for(i=0;i<7;i++)printk("%x : ",wrktim[i]);
00315 printk("\n");
00316 }
00317 #endif
00318
00319 if (cvtflg == 0) {
00320
00321
00322
00323 if (*wrktim) {
00324
00325
00326
00327 if (length > 0) {
00328 if ((timval = wrktim[2]) / 10 == 0) {
00329 *chrptr++ = ' ';
00330 } else {
00331 *chrptr++ = '0' + timval / 10;
00332 }
00333 length--;
00334 }
00335 if (length > 0) {
00336 *chrptr++ = '0' + (timval % 10);
00337 length--;
00338 }
00339 if ((count = length) > 5) count = 5;
00340 memcpy(chrptr,month_names + (wrktim[1] * 4 - 4),count);
00341 length -= count;
00342 chrptr += count;
00343 timval = *wrktim;
00344 } else {
00345
00346
00347
00348 timval = wrktim[2];
00349 }
00350
00351
00352
00353 count = 10000;
00354 if (timval < count) {
00355 count = 1000;
00356 while (length > 0 && timval < count && count > 1) {
00357 length--;
00358 *chrptr++ = ' ';
00359 count /= 10;
00360 }
00361 }
00362 while (length > 0 && count > 0) {
00363 length--;
00364 *chrptr++ = '0' + (timval / count);
00365 timval = timval % count;
00366 count /= 10;
00367 }
00368
00369
00370
00371 if (length > 0) {
00372 *chrptr++ = ' ';
00373 length--;
00374 }
00375 }
00376
00377
00378 count = 3;
00379 do {
00380 timval = wrktim[count];
00381 if (length >= 1) *chrptr++ = '0' + (timval / 10);
00382 if (length >= 2) {
00383 *chrptr++ = '0' + (timval % 10);
00384 length -= 2;
00385 } else {
00386 length = 0;
00387 }
00388 if (count < 6 && length > 0) {
00389 length--;
00390 if (count == 5) {
00391 *chrptr++ = '.';
00392 } else {
00393 *chrptr++ = ':';
00394 }
00395 }
00396 } while (++count < 7);
00397
00398
00399
00400 if (timlen != NULL) *timlen = timbuf->dsc_w_length - length;
00401 return SS__NORMAL;
00402 }
00403
00404
00405
00406 static const char time_sep[] = "::.";
00407
00408 int exe_bintim(struct dsc$descriptor *timbuf, struct TIME *timadra)
00409 {
00410 struct TIME *timadr = (struct TIME *)timadra;
00411
00412 register length = timbuf->dsc_w_length;
00413 register char *chrptr = timbuf->dsc_a_pointer;
00414 unsigned short wrktim[7];
00415 int num,tf;
00416
00417
00418
00419
00420 while (length > 0 && *chrptr == ' ') {
00421 length--;
00422 chrptr++;
00423 }
00424
00425
00426
00427 num = -1;
00428 if (length > 0 && *chrptr >= '0' && *chrptr <= '9') {
00429 num = 0;
00430 do {
00431 num = num * 10 + (*chrptr++ - '0');
00432 } while (--length > 0 && *chrptr >= '0' && *chrptr <= '9');
00433 }
00434
00435
00436 if (length > 0 && *chrptr == '-') {
00437 chrptr++;
00438
00439
00440
00441 exe_numtim(wrktim,NULL);
00442 if (num >= 0) wrktim[2] = num;
00443 num = 0;
00444 if (--length >= 3 && *chrptr != '-') {
00445 char *mn = month_names + 1;
00446 num = 1;
00447 while (num <= 12) {
00448 if (memcmp(chrptr,mn,3) == 0) break;
00449 mn += 4;
00450 num++;
00451 }
00452 chrptr += 3;
00453 length -= 3;
00454 wrktim[1] = num;
00455 }
00456
00457
00458 if (length > 0 && *chrptr == '-') {
00459 length--;
00460 chrptr++;
00461 if (length > 0 && *chrptr >= '0' && *chrptr <= '9') {
00462 num = 0;
00463 do {
00464 num = num * 10 + (*chrptr++ - '0');
00465 } while (--length > 0 && *chrptr >= '0' && *chrptr <= '9');
00466 wrktim[0] = num;
00467 }
00468 }
00469 } else {
00470
00471
00472
00473 wrktim[0] = wrktim[1] = 0;
00474 wrktim[2] = num;
00475 wrktim[3] = wrktim[4] = wrktim[5] = wrktim[6] = 0;
00476 }
00477
00478
00479
00480 while (length > 0 && *chrptr == ' ') {
00481 length--;
00482 chrptr++;
00483 }
00484
00485
00486
00487 for (tf = 0; tf < 3; tf++) {
00488 if (length > 0 && *chrptr >= '0' && *chrptr <= '9') {
00489 num = 0;
00490 do {
00491 num = num * 10 + (*chrptr++ - '0');
00492 } while (--length > 0 && *chrptr >= '0' && *chrptr <= '9');
00493 wrktim[3 + tf] = num;
00494 if (num > 59) wrktim[1] = 13;
00495 }
00496 if (length > 0 && *chrptr == time_sep[tf]) {
00497 length--;
00498 chrptr++;
00499 } else {
00500 break;
00501 }
00502 }
00503
00504
00505
00506 if (length > 0 && *chrptr >= '0' && *chrptr <= '9') {
00507 tf = 10;
00508 num = 0;
00509 do {
00510 num = num + tf * (*chrptr++ - '0');
00511 tf = tf / 10;
00512 } while (--length > 0 && *chrptr >= '0' && *chrptr <= '9');
00513 wrktim[6] = num;
00514 }
00515
00516
00517 while (length > 0 && *chrptr == ' ') {
00518 length--;
00519 chrptr++;
00520 }
00521
00522
00523
00524 if (length == 0) {
00525 return lib_cvt_vectim(wrktim,timadr);
00526 } else {
00527 return SS__IVTIME;
00528 }
00529 }
00530
00531
00532
00533
00534
00535 static const unsigned char month_days[] = {31,29,31,30,31,30,31,31,30,31,30,31};
00536
00537 int exe_numtim(unsigned short timbuf[7], struct TIME *timadra)
00538 {
00539 struct TIME *timadr = (struct TIME *)timadra;
00540 register date,time;
00541
00542
00543
00544 {
00545 int days,day_time;
00546 register unsigned sts;
00547 sts = lib_day(&days, timadr, &day_time);
00548 if ((sts & 1) == 0) {
00549 return sts;
00550 }
00551 date = days;
00552 time = day_time;
00553 }
00554
00555
00556
00557 if (date < 0 || time < 0) {
00558 timbuf[2] = -date;
00559 timbuf[1] = 0;
00560 timbuf[0] = 0;
00561 time = -time;
00562
00563 } else {
00564
00565
00566
00567 register year,month;
00568 date += OFFSET_DAYS;
00569 year = BASE_YEAR + (date / QUAD_CENTURY_DAYS) * 400;
00570 date %= QUAD_CENTURY_DAYS;
00571
00572
00573
00574 if ((month = date / CENTURY_DAYS) == 4) month = 3;
00575 date -= month * CENTURY_DAYS;
00576 year += month * 100;
00577
00578
00579
00580
00581 year += (date / QUAD_YEAR_DAYS) * 4;
00582 date %= QUAD_YEAR_DAYS;
00583
00584 if ((month = date / YEAR_DAYS) == 4) month = 3;
00585 date -= month * YEAR_DAYS;
00586 year += month;
00587
00588
00589
00590 if (date++ > 58) {
00591 if (month != 3) {
00592 date++;
00593 } else {
00594 if ((year % 100) == 0 && (year % 400) != 0) date++;
00595 }
00596 }
00597
00598
00599 {
00600 unsigned char *mthptr = month_days;
00601 month = 1;
00602 while (date > *mthptr) {
00603 date -= *mthptr++;
00604 month++;
00605 }
00606 }
00607
00608
00609
00610 timbuf[2] = date;
00611 timbuf[1] = month;
00612 timbuf[0] = year;
00613 }
00614
00615
00616
00617 timbuf[6] = time % 100;
00618 time /= 100;
00619 timbuf[5] = time % 60;
00620 time /= 60;
00621 timbuf[4] = time % 60;
00622 timbuf[3] = time / 60;
00623
00624 return SS__NORMAL;
00625 }
00626
00627