|
|
1.1 ! root 1: /* dtime.c - routines to do ``ARPA-style'' time structures */ ! 2: ! 3: /* LINTLIBRARY */ ! 4: ! 5: ! 6: #include "tws.h" ! 7: #ifndef INETONLY ! 8: #include "../h/strings.h" ! 9: #else INETONLY ! 10: #include "strings.h" ! 11: #endif INETONLY ! 12: #include <stdio.h> ! 13: #include <sys/types.h> ! 14: #if !defined(SYS5) && !defined(BSD44) ! 15: #include <sys/timeb.h> ! 16: #endif ! 17: #ifndef BSD42 ! 18: #include <time.h> ! 19: #else BSD42 ! 20: #include <sys/time.h> ! 21: #endif BSD42 ! 22: ! 23: #ifdef SYS5 ! 24: extern int daylight; ! 25: extern long timezone; ! 26: extern char *tzname[]; ! 27: #endif SYS5 ! 28: ! 29: /* */ ! 30: ! 31: #define abs(a) (a >= 0 ? a : -a) ! 32: ! 33: #define dysize(y) \ ! 34: (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366))) ! 35: ! 36: /* */ ! 37: ! 38: char *tw_moty[] = { ! 39: "Jan", "Feb", "Mar", "Apr", "May", "Jun", ! 40: "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL ! 41: }; ! 42: ! 43: char *tw_dotw[] = { ! 44: "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL ! 45: }; ! 46: ! 47: char *tw_ldotw[] = { ! 48: "Sunday", "Monday", "Tuesday", "Wednesday", ! 49: "Thursday", "Friday", "Saturday", NULL ! 50: }; ! 51: ! 52: /* */ ! 53: ! 54: #if !defined(ZONEINFO) && !defined(BSD44) ! 55: static struct zone { ! 56: char *std, ! 57: *dst; ! 58: int shift; ! 59: } zones[] = { ! 60: "GMT", "BST", 0, ! 61: "EST", "EDT", -5, ! 62: "CST", "CDT", -6, ! 63: "MST", NULL, -7, ! 64: "PST", "PDT", -8, ! 65: "A", NULL, -1, ! 66: "B", NULL, -2, ! 67: "C", NULL, -3, ! 68: "D", NULL, -4, ! 69: "E", NULL, -5, ! 70: "F", NULL, -6, ! 71: "G", NULL, -7, ! 72: "H", NULL, -8, ! 73: "I", NULL, -9, ! 74: "K", NULL, -10, ! 75: "L", NULL, -11, ! 76: "M", NULL, -12, ! 77: "N", NULL, 1, ! 78: #ifdef notdef ! 79: "O", NULL, 2, ! 80: #else ! 81: "JST", "JDT", 2, ! 82: #endif ! 83: "P", NULL, 3, ! 84: "Q", NULL, 4, ! 85: "R", NULL, 5, ! 86: "S", NULL, 6, ! 87: "T", NULL, 7, ! 88: "U", NULL, 8, ! 89: "V", NULL, 9, ! 90: "W", NULL, 10, ! 91: "X", NULL, 11, ! 92: "Y", NULL, 12, ! 93: ! 94: NULL ! 95: }; ! 96: ! 97: #endif ! 98: #define CENTURY 19 ! 99: ! 100: long time(); ! 101: struct tm *localtime(); ! 102: ! 103: /* */ ! 104: ! 105: char * ! 106: dtimenow() ! 107: { ! 108: long clock; ! 109: ! 110: (void) time (&clock); ! 111: return dtime (&clock); ! 112: } ! 113: ! 114: ! 115: char * ! 116: dctime(tw) ! 117: register struct tws *tw; ! 118: { ! 119: static char buffer[25]; ! 120: ! 121: if (!tw) ! 122: return NULL; ! 123: ! 124: (void) sprintf (buffer, "%.3s %.3s %02d %02d:%02d:%02d %.4d\n", ! 125: tw_dotw[tw -> tw_wday], tw_moty[tw -> tw_mon], tw -> tw_mday, ! 126: tw -> tw_hour, tw -> tw_min, tw -> tw_sec, ! 127: tw -> tw_year >= 100 ? tw -> tw_year : 1900 + tw -> tw_year); ! 128: ! 129: return buffer; ! 130: } ! 131: ! 132: /* */ ! 133: ! 134: struct tws * ! 135: dtwstime() ! 136: { ! 137: long clock; ! 138: ! 139: (void) time (&clock); ! 140: return dlocaltime (&clock); ! 141: } ! 142: ! 143: ! 144: struct tws * ! 145: dlocaltime(clock) ! 146: register long *clock; ! 147: { ! 148: register struct tm *tm; ! 149: #if !defined(SYS5) && !defined(ZONEINFO) & !defined(BSD44) ! 150: struct timeb tb; ! 151: #endif not SYS5 and not ZONEINFO ! 152: static struct tws tw; ! 153: ! 154: if (!clock) ! 155: return NULL; ! 156: tw.tw_flags = TW_NULL; ! 157: ! 158: #if defined(ZONEINFO) || defined(BSD44) ! 159: tzset(); ! 160: #endif ! 161: tm = localtime (clock); ! 162: tw.tw_sec = tm->tm_sec; ! 163: tw.tw_min = tm->tm_min; ! 164: tw.tw_hour = tm->tm_hour; ! 165: tw.tw_mday = tm->tm_mday; ! 166: tw.tw_mon = tm->tm_mon; ! 167: tw.tw_year = tm->tm_year; ! 168: tw.tw_wday = tm->tm_wday; ! 169: tw.tw_yday = tm->tm_yday; ! 170: if (tm->tm_isdst) ! 171: tw.tw_flags |= TW_DST; ! 172: #ifdef SYS5 ! 173: tzset (); ! 174: tw.tw_zone = -(timezone / 60); ! 175: #else ! 176: #if defined(ZONEINFO) || defined(BSD44) ! 177: tw.tw_zone = tm->tm_gmtoff / 60; ! 178: (void) strcpy(tw.tw_zonename, tm->tm_zone); ! 179: #else ! 180: ftime (&tb); ! 181: tw.tw_zone = -tb.timezone; ! 182: #endif ! 183: #endif ! 184: tw.tw_flags &= ~TW_SDAY; ! 185: tw.tw_flags |= TW_SEXP; ! 186: tw.tw_flags &= ~TW_SZONE; ! 187: tw.tw_flags |= TW_SZEXP; ! 188: tw.tw_clock = *clock; ! 189: ! 190: return (&tw); ! 191: } ! 192: ! 193: ! 194: struct tws * ! 195: dgmtime(clock) ! 196: register long *clock; ! 197: { ! 198: register struct tm *tm; ! 199: static struct tws tw; ! 200: ! 201: if (!clock) ! 202: return NULL; ! 203: tw.tw_flags = TW_NULL; ! 204: ! 205: tm = gmtime (clock); ! 206: tw.tw_sec = tm -> tm_sec; ! 207: tw.tw_min = tm -> tm_min; ! 208: tw.tw_hour = tm -> tm_hour; ! 209: tw.tw_mday = tm -> tm_mday; ! 210: tw.tw_mon = tm -> tm_mon; ! 211: tw.tw_year = tm -> tm_year; ! 212: tw.tw_wday = tm -> tm_wday; ! 213: tw.tw_yday = tm -> tm_yday; ! 214: if (tm -> tm_isdst) ! 215: tw.tw_flags |= TW_DST; ! 216: tw.tw_zone = 0; ! 217: tw.tw_flags &= ~TW_SDAY; ! 218: tw.tw_flags |= TW_SEXP; ! 219: tw.tw_flags &= ~TW_SZONE; ! 220: tw.tw_flags |= TW_SZEXP; ! 221: tw.tw_clock = *clock; ! 222: ! 223: return (&tw); ! 224: } ! 225: ! 226: /* */ ! 227: ! 228: char * ! 229: dasctime(tw, flags) ! 230: register struct tws *tw; ! 231: int flags; ! 232: { ! 233: char buffer[80]; ! 234: static char result[80]; ! 235: ! 236: if (!tw) ! 237: return(NULL); ! 238: ! 239: /* Display timezone if known */ ! 240: if ((tw->tw_flags & TW_SZONE) == TW_SZNIL) ! 241: result[0] = '\0'; ! 242: else ! 243: #if defined(ZONEINFO) || defined(BSD44) ! 244: if (*tw->tw_zonename) ! 245: (void) sprintf(result, " %s", tw->tw_zonename); ! 246: else ! 247: result[0] = '\0'; ! 248: #else ! 249: (void) sprintf(result, " %s", ! 250: dtimezone(tw -> tw_zone, tw->tw_flags | flags)); ! 251: #endif ! 252: (void) sprintf(buffer, "%02d %s %02d %02d:%02d:%02d%s", ! 253: tw->tw_mday, tw_moty[tw->tw_mon], tw->tw_year, ! 254: tw->tw_hour, tw->tw_min, tw->tw_sec, result); ! 255: ! 256: /* Display day of week if known (put in parens if implicitly derived) */ ! 257: if ((tw->tw_flags & TW_SDAY) == TW_SEXP) ! 258: (void) sprintf(result, "%s, %s", tw_dotw[tw->tw_wday], buffer); ! 259: else if ((tw -> tw_flags & TW_SDAY) == TW_SNIL) ! 260: (void) strcpy(result, buffer); ! 261: else ! 262: (void) sprintf(result, "%s (%s)", buffer, tw_dotw[tw->tw_wday]); ! 263: ! 264: return(result); ! 265: } ! 266: ! 267: /* */ ! 268: ! 269: #if !defined(ZONEINFO) && !defined(BSD44) ! 270: char * ! 271: dtimezone(offset, flags) ! 272: register int offset, flags; ! 273: { ! 274: register int hours, ! 275: mins; ! 276: register struct zone *z; ! 277: static char buffer[10]; ! 278: ! 279: if (offset < 0) { ! 280: mins = -((-offset) % 60); ! 281: hours = -((-offset) / 60); ! 282: } ! 283: else { ! 284: mins = offset % 60; ! 285: hours = offset / 60; ! 286: } ! 287: ! 288: if (!(flags & TW_ZONE) && mins == 0) ! 289: for (z = zones; z -> std; z++) ! 290: if (z -> shift == hours) ! 291: return (z -> dst && (flags & TW_DST) ? z -> dst : z -> std); ! 292: ! 293: #if defined(DSTXXX) ! 294: if (flags & TW_DST) ! 295: hours += 1; ! 296: #endif DSTXXX ! 297: (void) sprintf (buffer, "%s%02d%02d", ! 298: offset < 0 ? "-" : "+", abs (hours), abs (mins)); ! 299: return buffer; ! 300: } ! 301: #endif ! 302: ! 303: /* */ ! 304: ! 305: void ! 306: twscopy(tb, tw) ! 307: register struct tws *tb, *tw; ! 308: { ! 309: #ifdef notdef ! 310: tb -> tw_sec = tw -> tw_sec; ! 311: tb -> tw_min = tw -> tw_min; ! 312: tb -> tw_hour = tw -> tw_hour; ! 313: tb -> tw_mday = tw -> tw_mday; ! 314: tb -> tw_mon = tw -> tw_mon; ! 315: tb -> tw_year = tw -> tw_year; ! 316: tb -> tw_wday = tw -> tw_wday; ! 317: tb -> tw_yday = tw -> tw_yday; ! 318: tb -> tw_zone = tw -> tw_zone; ! 319: tb -> tw_clock = tw -> tw_clock; ! 320: tb -> tw_flags = tw -> tw_flags; ! 321: #else not notdef ! 322: *tb = *tw; ! 323: #endif not notdef ! 324: } ! 325: ! 326: ! 327: twsort(tw1, tw2) ! 328: register struct tws *tw1, *tw2; ! 329: { ! 330: register long c1, ! 331: c2; ! 332: ! 333: if (tw1 -> tw_clock == 0L) ! 334: (void) twclock (tw1); ! 335: if (tw2 -> tw_clock == 0L) ! 336: (void) twclock (tw2); ! 337: ! 338: return ((c1 = tw1 -> tw_clock) > (c2 = tw2 -> tw_clock) ? 1 ! 339: : c1 == c2 ? 0 : -1); ! 340: } ! 341: ! 342: /* */ ! 343: ! 344: /* This routine is based on the gtime() routine written by Steven Shafer ! 345: (sas) at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS. ! 346: */ ! 347: ! 348: static int dmsize[] = { ! 349: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ! 350: }; ! 351: ! 352: ! 353: long ! 354: twclock(tw) ! 355: register struct tws *tw; ! 356: { ! 357: register int i, ! 358: sec, ! 359: min, ! 360: hour, ! 361: mday, ! 362: mon, ! 363: year; ! 364: register long result; ! 365: ! 366: if (tw -> tw_clock != 0L) ! 367: return tw -> tw_clock; ! 368: ! 369: if ((sec = tw -> tw_sec) < 0 || sec > 59 ! 370: || (min = tw -> tw_min) < 0 || min > 59 ! 371: || (hour = tw -> tw_hour) < 0 || hour > 23 ! 372: || (mday = tw -> tw_mday) < 1 || mday > 31 ! 373: || (mon = tw -> tw_mon + 1) < 1 || mon > 12) ! 374: return (tw -> tw_clock = -1L); ! 375: year = tw -> tw_year; ! 376: ! 377: result = 0L; ! 378: year += 1900; ! 379: for (i = 1970; i < year; i++) ! 380: result += dysize (i); ! 381: if (dysize (year) == 366 && mon >= 3) ! 382: result++; ! 383: while (--mon) ! 384: result += dmsize[mon - 1]; ! 385: result += mday - 1; ! 386: result = 24 * result + hour; ! 387: result = 60 * result + min; ! 388: result = 60 * result + sec; ! 389: result -= 60 * tw -> tw_zone; ! 390: if (tw -> tw_flags & TW_DST) ! 391: result -= 60 * 60; ! 392: ! 393: return (tw -> tw_clock = result); ! 394: } ! 395: ! 396: /* */ ! 397: ! 398: /* ! 399: * Simple calculation of day of the week. Algorithm used is Zeller's ! 400: * congruence. Currently, we assume if tw -> tw_year < 100 ! 401: * then the century is CENTURY. ! 402: */ ! 403: ! 404: set_dotw(tw) ! 405: register struct tws *tw; ! 406: { ! 407: register int month, ! 408: day, ! 409: year, ! 410: century; ! 411: ! 412: month = tw -> tw_mon - 1; ! 413: day = tw -> tw_mday; ! 414: year = tw -> tw_year % 100; ! 415: century = tw -> tw_year >= 100 ? tw -> tw_year / 100 : CENTURY; ! 416: ! 417: if (month <= 0) { ! 418: month += 12; ! 419: if (--year < 0) { ! 420: year += 100; ! 421: century--; ! 422: } ! 423: } ! 424: ! 425: tw -> tw_wday = ! 426: ((26 * month - 2) / 10 + day + year + year / 4 ! 427: - 3 * century / 4 + 1) % 7; ! 428: ! 429: tw->tw_flags &= ~TW_SDAY; ! 430: tw->tw_flags |= TW_SIMP; ! 431: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.