Annotation of 43BSDReno/contrib/isode-beta/psap/prim2time.c, revision 1.1

1.1     ! root        1: /* prim2time.c - presentation element to time string */
        !             2: 
        !             3: #ifndef        lint
        !             4: static char *rcsid = "$Header: /f/osi/psap/RCS/prim2time.c,v 7.0 89/11/23 22:13:17 mrose Rel $";
        !             5: #endif
        !             6: 
        !             7: /* 
        !             8:  * $Header: /f/osi/psap/RCS/prim2time.c,v 7.0 89/11/23 22:13:17 mrose Rel $
        !             9:  *
        !            10:  *
        !            11:  * $Log:       prim2time.c,v $
        !            12:  * Revision 7.0  89/11/23  22:13:17  mrose
        !            13:  * Release 6.0
        !            14:  * 
        !            15:  */
        !            16: 
        !            17: /*
        !            18:  *                               NOTICE
        !            19:  *
        !            20:  *    Acquisition, use, and distribution of this module and related
        !            21:  *    materials are subject to the restrictions of a license agreement.
        !            22:  *    Consult the Preface in the User's Manual for the full terms of
        !            23:  *    this agreement.
        !            24:  *
        !            25:  */
        !            26: 
        !            27: 
        !            28: /* LINTLIBRARY */
        !            29: 
        !            30: #include <ctype.h>
        !            31: #include <stdio.h>
        !            32: #include "psap.h"
        !            33: 
        !            34: /*    DATA */
        !            35: 
        !            36: #define        YEAR(y)         ((y) >= 100 ? (y) : (y) + 1900)
        !            37: 
        !            38: long   get_usec ();
        !            39: 
        !            40: /*  */
        !            41: 
        !            42: UTC    prim2time (pe, generalized)
        !            43: register PE    pe;
        !            44: int    generalized;
        !            45: {
        !            46:     int     len;
        !            47:     register char  *cp;
        !            48:     register UTC    u;
        !            49:     UTC           (*aux) ();
        !            50: 
        !            51:     aux = generalized ? str2gent : str2utct;
        !            52:     switch (pe -> pe_form) {
        !            53:        case PE_FORM_PRIM: 
        !            54:            if (pe -> pe_prim == NULLPED)
        !            55:                return pe_seterr (pe, PE_ERR_PRIM, NULLUTC);
        !            56:            u = (*aux) ((char *) pe -> pe_prim, (int) pe -> pe_len);
        !            57:            break;
        !            58: 
        !            59:        case PE_FORM_CONS: 
        !            60:            if ((cp = prim2str (pe, &len)) == NULLCP)
        !            61:                return NULLUTC;
        !            62:            u = len ? (*aux) (cp, len) : NULLUTC;
        !            63:            free (cp);
        !            64:            break;
        !            65:     }
        !            66: 
        !            67:     return (u ? u : pe_seterr (pe, generalized ? PE_ERR_GENT : PE_ERR_UTCT,
        !            68:                        NULLUTC));
        !            69: }
        !            70: 
        !            71: /*  */
        !            72: 
        !            73: UTC    str2utct (cp, len)
        !            74: register char  *cp;
        !            75: register int   len;
        !            76: {
        !            77:     int     year,
        !            78:            hours,
        !            79:             mins;
        !            80:     register int    zone;
        !            81:     static UTCtime  ut;
        !            82:     register UTC    u = &ut;
        !            83: 
        !            84:     bzero ((char *) u, sizeof *u);
        !            85: 
        !            86:     if (sscanf (cp, "%2d%2d%2d%2d%2d", &year, &u -> ut_mon,
        !            87:                &u -> ut_mday, &u -> ut_hour, &u -> ut_min) != 5)
        !            88:        return NULLUTC;
        !            89:     cp += 10, len -= 10;
        !            90:     u -> ut_year = YEAR (year);
        !            91: 
        !            92:     if (len > 0 && isdigit (*cp)) {
        !            93:        if (sscanf (cp, "%2d", &u -> ut_sec) != 1)
        !            94:            return NULLUTC;
        !            95:        u -> ut_flags |= UT_SEC;
        !            96:        cp += 2, len -= 2;
        !            97:     }
        !            98: 
        !            99:     if (len > 0) {
        !           100:        switch (*cp) {
        !           101:            case 'Z': 
        !           102:                cp++, len--;
        !           103:                break;
        !           104: 
        !           105:            case '+': 
        !           106:            case '-': 
        !           107:                if (sscanf (cp + 1, "%2d%2d", &hours, &mins) != 2)
        !           108:                    return NULLUTC;
        !           109:                zone = hours * 60 + mins;
        !           110:                u -> ut_zone = *cp == '+' ? zone : -zone;
        !           111:                cp += 5, len -= 5;
        !           112:                break;
        !           113: 
        !           114:            default: 
        !           115:                return NULLUTC;
        !           116:        }
        !           117:        u -> ut_flags |= UT_ZONE;
        !           118:     }
        !           119:     if (len != 0)
        !           120:        return NULLUTC;
        !           121: 
        !           122:     return u;
        !           123: }
        !           124: 
        !           125: /*  */
        !           126: 
        !           127: UTC    str2gent (cp, len)
        !           128: char   *cp;
        !           129: int    len;
        !           130: {
        !           131:     int     hours,
        !           132:             mins;
        !           133:     long    usec;
        !           134:     register int    zone;
        !           135:     static UTCtime    ut;
        !           136:     register UTC      u = &ut;
        !           137: 
        !           138:     bzero ((char *) u, sizeof *u);
        !           139: 
        !           140:     if (sscanf (cp, "%4d%2d%2d%2d", &u -> ut_year, &u -> ut_mon,
        !           141:                &u -> ut_mday, &u -> ut_hour) != 4)
        !           142:        return NULLUTC;
        !           143:     cp += 10, len -= 10;
        !           144: 
        !           145:     if (len > 0)
        !           146:        switch (*cp) {
        !           147:            case '.':
        !           148:            case ',':
        !           149:                cp++, len--;
        !           150:                if ((usec = get_usec (&cp, &len)) < 0)
        !           151:                    return NULLUTC;
        !           152:                u -> ut_min = (usec * 60) / 1000000;
        !           153:                usec -= (u -> ut_min * 1000000) / 60;
        !           154:                u -> ut_sec = (usec * 60) / 1000000;
        !           155:                usec -= (u -> ut_sec * 1000000) / 60;
        !           156:                u -> ut_usec = usec;
        !           157:                u -> ut_flags |= UT_SEC | UT_USEC;
        !           158:                goto get_zone;
        !           159: 
        !           160:            default:
        !           161:                if (isdigit (*cp)) {
        !           162:                    if (sscanf (cp, "%2d", &u -> ut_min) != 1)
        !           163:                        return NULLUTC;
        !           164:                    cp += 2, len -= 2;
        !           165:                }
        !           166:                break;
        !           167:        }
        !           168: 
        !           169:     if (len > 0)
        !           170:        switch (*cp) {
        !           171:            case '.':
        !           172:            case ',':
        !           173:                cp++, len--;
        !           174:                if ((usec = get_usec (&cp, &len)) < 0)
        !           175:                    return NULLUTC;
        !           176:                u -> ut_sec = (usec * 60) / 1000000;
        !           177:                usec -= (u -> ut_sec * 1000000) / 60;
        !           178:                u -> ut_usec = usec;
        !           179:                u -> ut_flags |= UT_SEC | UT_USEC;
        !           180:                goto get_zone;
        !           181: 
        !           182:            default:
        !           183:                if (isdigit (*cp)) {
        !           184:                    if (sscanf (cp, "%2d", &u -> ut_sec) != 1)
        !           185:                        return NULLUTC;
        !           186:                    u -> ut_flags |= UT_SEC;
        !           187:                    cp += 2, len -= 2;
        !           188:                }
        !           189:                break;
        !           190:        }
        !           191:            
        !           192:     if (len > 0)
        !           193:        switch (*cp) {
        !           194:            case '.':
        !           195:            case ',':
        !           196:                cp++, len--;
        !           197:                if ((usec = get_usec (&cp, &len)) < 0)
        !           198:                    return NULLUTC;
        !           199:                u -> ut_usec = usec;
        !           200:                u -> ut_flags |= UT_USEC;
        !           201:                goto get_zone;
        !           202: 
        !           203:            default:
        !           204:                break;
        !           205:        }
        !           206:            
        !           207: get_zone: ;
        !           208:     if (len > 0) {
        !           209:        switch (*cp) {
        !           210:            case 'Z': 
        !           211:                cp++, len--;
        !           212:                break;
        !           213: 
        !           214:            case '+': 
        !           215:            case '-': 
        !           216:                if (sscanf (cp + 1, "%2d%2d", &hours, &mins) != 2)
        !           217:                    return NULLUTC;
        !           218:                zone = hours * 60 + mins;
        !           219:                u -> ut_zone = *cp == '+' ? zone : -zone;
        !           220:                cp += 5, len -= 5;
        !           221:                break;
        !           222: 
        !           223:            default: 
        !           224:                return NULLUTC;
        !           225:        }
        !           226:        u -> ut_flags |= UT_ZONE;
        !           227:     }
        !           228:     if (len != 0)
        !           229:        return NULLUTC;
        !           230: 
        !           231:     return u;
        !           232: }
        !           233: 
        !           234: /*  */
        !           235: 
        !           236: /* not perfect, but what is? */
        !           237: 
        !           238: static long  get_usec (cp, len)
        !           239: char  **cp;
        !           240: int    *len;
        !           241: {
        !           242:     register int    j;
        !           243:     register long   i;
        !           244:     register char  *dp;
        !           245: 
        !           246:     i = 0L;
        !           247:     for (dp = *cp, j = 0; isdigit (*dp); dp++, j++)
        !           248:        if (j < 6)
        !           249:            i = i * 10L + (long) (*dp - '0');
        !           250: 
        !           251:     *cp = dp, *len -= j;
        !           252: 
        !           253:     while (j++ < 6)
        !           254:        i *= 10L;
        !           255: 
        !           256:     return i;
        !           257: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.