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 #pragma ident "@(#) libf/pxf/pxflocaltime.c 92.2 10/29/99 21:41:49"
00038
00039
00040 #include <fortran.h>
00041 #include <errno.h>
00042 #include <liberrno.h>
00043 #include <time.h>
00044 #include <limits.h>
00045 #include <stdlib.h>
00046 #include <string.h>
00047 #include <ctype.h>
00048
00049 #ifdef _SOLARIS
00050 #include <string.h>
00051 #endif
00052
00053 #define POSIX_BASE_YEAR 1900
00054
00055
00056
00057 #define MONTH_OFFSET 1
00058
00059
00060
00061 #define TIME_ZONE "TZ"
00062 #define PLUSMINUS 1
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 #ifdef _UNICOS
00107 void
00108 PXFLOCALTIME(
00109 #else
00110 void
00111 _PXFLOCALTIME(
00112 #endif
00113 _f_int *ISECNDS,
00114 _f_int *IATIME,
00115 _f_int *IERROR
00116 )
00117 {
00118 int i, itemp;
00119 time_t cisecnds;
00120 struct tm *tmstruct;
00121 char *tzstr;
00122
00123 cisecnds = *ISECNDS;
00124 *IERROR = 0;
00125
00126
00127 if ((tzstr = getenv(TIME_ZONE)) != NULL) {
00128
00129 #if defined(_UNICOS) || defined(_MIPSEB)
00130
00131
00132
00133
00134 if (tzstr[0] != ':') {
00135
00136
00137 i = 0;
00138
00139 if (is_stddst(tzstr, &i)) {
00140
00141 if (is_offset(tzstr, &i, PLUSMINUS)) {
00142 if(tzstr[i] != '\0') {
00143
00144 if (is_stddst(tzstr, &i)) {
00145 if (tzstr[i] != '\0') {
00146
00147 itemp = i;
00148 if (is_offset(tzstr, &itemp, PLUSMINUS)) {
00149 i = itemp;
00150 if (tzstr[i] != '\0') {
00151
00152 if (!is_rule(tzstr, &i)) {
00153 *IERROR = EINVAL;
00154 return;
00155 }
00156 }
00157 } else {
00158
00159 if (!is_rule(tzstr, &i)) {
00160 *IERROR = EINVAL;
00161 return;
00162 }
00163 }
00164 }
00165 } else {
00166 *IERROR = EINVAL;
00167 return;
00168 }
00169 }
00170 } else {
00171 *IERROR = EINVAL;
00172 return;
00173 }
00174 } else {
00175 *IERROR = EINVAL;
00176 return;
00177 }
00178 }
00179
00180
00181 #else
00182
00183
00184
00185
00186 if (((int)strlen(tzstr) < 4) || !isalpha(tzstr[0])
00187 || !isalpha(tzstr[1]) || tzstr[2] != '/') {
00188 *IERROR = EINVAL;
00189 return;
00190 }
00191
00192
00193 #endif
00194
00195 }
00196
00197 tmstruct = localtime(&cisecnds);
00198
00199 IATIME[0] = tmstruct->tm_sec;
00200 IATIME[1] = tmstruct->tm_min;
00201 IATIME[2] = tmstruct->tm_hour;
00202 IATIME[3] = tmstruct->tm_mday;
00203 IATIME[4] = tmstruct->tm_mon + MONTH_OFFSET;
00204 IATIME[5] = tmstruct->tm_year + POSIX_BASE_YEAR;
00205 IATIME[6] = tmstruct->tm_wday;
00206 IATIME[7] = tmstruct->tm_yday;
00207 IATIME[8] = tmstruct->tm_isdst;
00208
00209 }
00210
00211
00212 #ifndef _UNICOS
00213 void
00214 pxflocaltime_(
00215 _f_int *ISECNDS,
00216 _f_int *IATIME,
00217 _f_int *IERROR
00218 )
00219 {
00220 _PXFLOCALTIME(ISECNDS,IATIME,IERROR);
00221 }
00222 #endif
00223
00224
00225 #if defined(_UNICOS) || defined(_MIPSEB)
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 static
00250 int
00251 is_stddst(
00252 const char *str,
00253 int *pos
00254 )
00255 {
00256 int i, original;
00257 char c;
00258
00259 i = *pos;
00260
00261 if (str[i] != ':') {
00262 for(original = i, c = str[i];
00263 i - original < TZNAME_MAX && c != '\0' && !isdigit(c) && c != ','
00264 && c != '-' && c != '+';) {
00265 c = str[++i];
00266 }
00267 }
00268
00269 *pos = i;
00270
00271 return (i - original < TZNAME_MAX && i - original >= _POSIX_TZNAME_MAX);
00272 }
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 static
00300 int
00301 is_offset(
00302 const char *str,
00303 int *pos,
00304 int plusminus
00305 )
00306 {
00307 int i, j, original, val;
00308 char c, temp[3];
00309
00310 i = *pos;
00311 c = str[i];
00312 temp[1] = '\0';
00313 temp[2] = '\0';
00314
00315
00316 if (plusminus && (c == '+' || c == '-')) {
00317 c = str[++i];
00318 }
00319
00320
00321 for (j = 0;
00322 (j <= 2) && c != '\0' && isdigit(c);) {
00323 temp[j++] = c;
00324 c = str[++i];
00325 }
00326 val = atoi(temp);
00327 if (j <= 2 && j != 0 && val >= 0 && val <= 23) {
00328 if (c == ':') {
00329
00330 if (str[i+1] != '\0' && isdigit(str[i+1]) &&
00331 str[i+2] != '\0' && isdigit(str[i+2])) {
00332 temp[0] = str[i+1];
00333 temp[1] = str[i+2];
00334 val = atoi(temp);
00335 if (val >= 0 && val <= 60) {
00336 i += 3;
00337 if (str[i] == ':') {
00338
00339 if (str[i+1] != '\0' && isdigit(str[i+1]) &&
00340 str[i+2] != '\0' && isdigit(str[i+2])) {
00341 temp[0] = str[i+1];
00342 temp[1] = str[i+2];
00343 val = atoi(temp);
00344 if (val >= 0 && val <= 60) {
00345 i += 3;
00346 } else {
00347 return 0;
00348 }
00349 } else {
00350 return 0;
00351 }
00352 }
00353 } else {
00354 return 0;
00355 }
00356 } else {
00357 return 0;
00358 }
00359 }
00360 } else {
00361 return 0;
00362 }
00363
00364 *pos = i;
00365 return 1;
00366 }
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387 static
00388 int
00389 is_rule(
00390 const char *str,
00391 int *pos
00392 )
00393 {
00394 int i;
00395
00396 i = *pos;
00397
00398 if (str[i++] == ',') {
00399 if (is_date(str, &i)) {
00400 if (str[i] == '/') {
00401 i++;
00402 if (!is_offset(str, &i, !PLUSMINUS)) {
00403 return 0;
00404 }
00405 }
00406 if (str[i++] == ',') {
00407 if (is_date(str, &i)) {
00408 if (str[i] == '/') {
00409 i++;
00410 if (is_offset(str, &i, !PLUSMINUS)) {
00411 if (str[i] == '\0') {
00412 *pos = i;
00413 return 1;
00414 }
00415 }
00416 } else if (str[i] == '\0') {
00417 *pos = i;
00418 return 1;
00419 }
00420 }
00421 }
00422 }
00423 }
00424
00425 return 0;
00426 }
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453 static
00454 int
00455 is_date(
00456 char *str,
00457 int *pos
00458 )
00459 {
00460 int i, j, day, val, is_julian;
00461 char temp[4];
00462
00463 is_julian = 0;
00464 i = *pos;
00465
00466 switch (str[i]) {
00467 case 'M':
00468 i++;
00469
00470 for (j = 0; str[i+j] != '\0' && isdigit(str[i+j]); j++) {
00471 temp[j] = str[i+j];
00472 }
00473 if (j > 0) {
00474 temp[j] = '\0';
00475 val = atoi(temp);
00476 if (val >= 1 && val <= 12) {
00477 i += j;
00478 if (str[i++] == '.') {
00479
00480 temp[0] = str[i++];
00481 temp[1] = '\0';
00482 val = atoi(temp);
00483 if (val >= 1 && val <= 5 && str[i++] == '.') {
00484
00485 temp[0] = str[i++];
00486 val = atoi(temp);
00487 if (val >= 0 && val <= 6) {
00488 *pos = i;
00489 return 1;
00490 }
00491 }
00492 }
00493 }
00494 }
00495
00496 break;
00497
00498 case 'J':
00499 is_julian = 1;
00500 i++;
00501
00502 default:
00503 for (j = 0; j < 3 && str[i+j] != '\0' && isdigit(str[i+j]); j++) {
00504 temp[j] = str[i+j];
00505 }
00506 if (j > 0) {
00507 temp[j] = '\0';
00508 day = atoi(temp);
00509 if (day <= 365 &&
00510 ((isdigit(str[i]) && day >= 0) || (is_julian && day >= 1))) {
00511 *pos = i + j;
00512 return 1;
00513 }
00514 }
00515 }
00516 return 0;
00517 }
00518
00519 #endif