1: /*
   2:  * Copyright (c) 1980 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #ifndef lint
   8: static char sccsid[] = "@(#)unctime.c	5.1 (Berkeley) 6/5/85";
   9: #endif not lint
  10: 
  11: #include <sys/types.h>
  12: #include <sys/time.h>
  13: #include <stdio.h>
  14: /*
  15:  * Convert a ctime(3) format string into a system format date.
  16:  * Return the date thus calculated.
  17:  *
  18:  * Return -1 if the string is not in ctime format.
  19:  */
  20: 
  21: /*
  22:  * Offsets into the ctime string to various parts.
  23:  */
  24: 
  25: #define E_MONTH     4
  26: #define E_DAY       8
  27: #define E_HOUR      11
  28: #define E_MINUTE    14
  29: #define E_SECOND    17
  30: #define E_YEAR      20
  31: 
  32: time_t unctime(str)
  33:     char *str;
  34: {
  35:     struct tm then;
  36:     char dbuf[30];
  37:     time_t emitl();
  38: 
  39:     if (strlen(str) != 25)
  40:         str[25] = 0;
  41:     strcpy(dbuf, str);
  42:     dbuf[E_MONTH+3] = 0;
  43:     if ( (then.tm_mon = lookup(&dbuf[E_MONTH])) < 0) {
  44:         return(-1);;
  45:     }
  46:     then.tm_mday = atoi(&dbuf[E_DAY]);
  47:     then.tm_hour = atoi(&dbuf[E_HOUR]);
  48:     then.tm_min = atoi(&dbuf[E_MINUTE]);
  49:     then.tm_sec = atoi(&dbuf[E_SECOND]);
  50:     then.tm_year = atoi(&dbuf[E_YEAR]) - 1900;
  51:     return(emitl(&then));
  52: }
  53: 
  54: static char months[] =
  55:     "JanFebMarAprMayJunJulAugSepOctNovDec";
  56: 
  57: static
  58: lookup(str)
  59:     char *str;
  60: {
  61:     register char *cp, *cp2;
  62: 
  63:     for (cp = months, cp2 = str; *cp != 0; cp += 3)
  64:         if (strncmp(cp, cp2, 3) == 0)
  65:             return((cp-months) / 3);
  66:     return(-1);
  67: }
  68: /*
  69:  * Routine to convert a localtime(3) format date back into
  70:  * a system format date.
  71:  *
  72:  *	Use a binary search.
  73:  */
  74: 
  75: struct tm *localtime();
  76: 
  77: time_t emitl(dp)
  78:     struct tm *dp;
  79: {
  80:     time_t conv;
  81:     register int i, bit;
  82:     struct tm dcopy;
  83: 
  84:     dcopy = *dp;
  85:     dp = &dcopy;
  86:     conv = 0;
  87:     for (i = 30; i >= 0; i--) {
  88:         bit = 1 << i;
  89:         conv |= bit;
  90:         if (dcmp(localtime(&conv), dp) > 0)
  91:             conv &= ~bit;
  92:     }
  93:     return(conv);
  94: }
  95: 
  96: /*
  97:  * Compare two localtime dates, return result.
  98:  */
  99: 
 100: #define DECIDE(a) \
 101:     if (dp->a > dp2->a) \
 102:         return(1); \
 103:     if (dp->a < dp2->a) \
 104:         return(-1)
 105: 
 106: static
 107: dcmp(dp, dp2)
 108:     register struct tm *dp, *dp2;
 109: {
 110: 
 111:     DECIDE(tm_year);
 112:     DECIDE(tm_mon);
 113:     DECIDE(tm_mday);
 114:     DECIDE(tm_hour);
 115:     DECIDE(tm_min);
 116:     DECIDE(tm_sec);
 117:     return(0);
 118: }

Defined functions

dcmp defined in line 106; used 1 times
  • in line 90
emitl defined in line 77; used 2 times
lookup defined in line 57; used 1 times
  • in line 43
unctime defined in line 32; used 2 times

Defined variables

months defined in line 54; used 2 times
sccsid defined in line 8; never used

Defined macros

DECIDE defined in line 100; used 6 times
E_DAY defined in line 26; used 1 times
  • in line 46
E_HOUR defined in line 27; used 1 times
  • in line 47
E_MINUTE defined in line 28; used 1 times
  • in line 48
E_MONTH defined in line 25; used 2 times
E_SECOND defined in line 29; used 1 times
  • in line 49
E_YEAR defined in line 30; used 1 times
  • in line 50
Last modified: 1985-06-05
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1120
Valid CSS Valid XHTML 1.0 Strict