Annotation of researchv9/libc/gen/timec.c, revision 1.1

1.1     ! root        1: #include <time.h>
        !             2: #include <libc.h>
        !             3: #include <sys/types.h>
        !             4: #include <sys/timeb.h>
        !             5: #include <ctype.h>
        !             6: 
        !             7: static
        !             8: reduce(fp,n)
        !             9: int *fp;
        !            10: {
        !            11:        int val = 0;
        !            12:        for( ; *fp<0;  *fp+=n)
        !            13:                val--;
        !            14:        val += *fp/n;
        !            15:        *fp %= n;
        !            16:        return val;
        !            17: }
        !            18: 
        !            19: static char Month[][3] = {"jan","feb","mar","apr","may","jun",
        !            20:                "jul","aug","sep","oct","nov","dec","\0"};
        !            21: static char Zone[][3] = {"gmt","est","edt","cst","cdt",
        !            22:                "mst","mdt","pst","pdt","\0"};
        !            23: static int Diff[] = {0,5*60,4*60,6*60,5*60,7*60,6*60,8*60,7*60};
        !            24: static int mday[] = {0,31,59,90,120,151,181,212,243,273,304,334};
        !            25: 
        !            26: long timegm(tp)
        !            27: struct tm *tp;
        !            28: {
        !            29:        int n;
        !            30:        struct tm tm;
        !            31:        tm = *tp;
        !            32:        tm.tm_year += reduce(&tm.tm_mon, 12);
        !            33:        tm.tm_yday += mday[tm.tm_mon] + tm.tm_mday
        !            34:                - ((tm.tm_year%4!=0)|(tm.tm_mon<3));
        !            35:        tm.tm_min += reduce(&tm.tm_sec, 60);
        !            36:        tm.tm_hour += reduce(&tm.tm_min, 60);
        !            37:        tm.tm_yday += reduce(&tm.tm_hour, 24);
        !            38:        tm.tm_year += 4*reduce(&tm.tm_yday, 4*365+1);
        !            39:        while(tm.tm_yday >= (n = 365+((tm.tm_year%4)==0)))
        !            40:                tm.tm_yday -= n, tm.tm_year++;
        !            41:        return  tm.tm_sec + 60*(tm.tm_min +
        !            42:                60*(tm.tm_hour + 24L*(tm.tm_yday +
        !            43:                365*(tm.tm_year-70) + (tm.tm_year-69)/4)));
        !            44: }
        !            45: 
        !            46: long timelocal(tp,zone)
        !            47: struct tm *tp;
        !            48: char *zone;
        !            49: {
        !            50:        struct timeb systime;
        !            51:        long date;
        !            52:        date = timegm(tp);
        !            53:        if(zone==0) {
        !            54:                ftime(&systime);
        !            55:                date += systime.timezone*60L;
        !            56:                if(systime.dstflag &&
        !            57:                   localtime(&date)->tm_isdst)
        !            58:                        date -= 3600;
        !            59:        } else 
        !            60:                date += Diff[serialno(zone,Zone)];
        !            61:        return date;
        !            62: }
        !            63: 
        !            64: static char *breaks = "\t ,:\n";
        !            65: 
        !            66: static char *
        !            67: skipfld(s)
        !            68: char *s;
        !            69: {
        !            70:        char *t = strpbrk(s,breaks);
        !            71:        return t!=0? t: s+strlen(s);
        !            72: }
        !            73: 
        !            74: static char*
        !            75: skipbrk(s)
        !            76: char *s;
        !            77: {
        !            78:        return s + strspn(s,breaks);
        !            79: }
        !            80: 
        !            81: long
        !            82: timec(s)
        !            83: char *s;
        !            84: {
        !            85:        char *t;
        !            86:        struct tm tm;
        !            87:        char *zone = 0;
        !            88:        tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
        !            89:        tm.tm_mon = tm.tm_mday = tm.tm_yday = 0;
        !            90:        for(s=skipbrk(s); isalpha(*s); s=skipbrk(skipfld(s)))
        !            91:                tm.tm_mon = serialno(s,Month);
        !            92:        tm.tm_mday = atoi(s);
        !            93:        s = skipbrk(skipfld(s));
        !            94:        t = skipfld(s);
        !            95:        if(*t==':') {
        !            96:                tm.tm_hour = atoi(s);
        !            97:                tm.tm_min = atoi(++t);
        !            98:                t = skipfld(t);
        !            99:                if(*t==':') {
        !           100:                        tm.tm_sec = atoi(++t);
        !           101:                        t = skipfld(t);
        !           102:                }
        !           103:                s = skipbrk(t);
        !           104:        }
        !           105:        tm.tm_year = atoi(s);
        !           106:        if(tm.tm_year!=0)
        !           107:                s = skipbrk(skipfld(s));
        !           108:        if(isalpha(*s)) {
        !           109:                zone = s;
        !           110:                s = skipbrk(skipfld(s));
        !           111:        }
        !           112:        if(tm.tm_year==0)
        !           113:                tm.tm_year = atoi(s);
        !           114:        tm.tm_year -= 1900;
        !           115:        if(tm.tm_year < 0) {
        !           116:                long clock;
        !           117:                time(&clock);
        !           118:                tm.tm_year = localtime(&clock)->tm_year;
        !           119:                if(timelocal(&tm,zone) > clock)
        !           120:                        tm.tm_year--;
        !           121:        }
        !           122:        return timelocal(&tm,zone);
        !           123: }
        !           124: 
        !           125: static
        !           126: serialno(item,list)
        !           127: char *item, list[][3];
        !           128: {
        !           129:        register i,j;
        !           130:        for(i=0; list[i][0]; i++)
        !           131:                for(j=0; j<3; j++)
        !           132:                        if(tolower(item[j])!=list[i][j])
        !           133:                                break;
        !           134:                        else if(j==2)
        !           135:                                return i;
        !           136:        return 0;
        !           137: }

unix.superglobalmegacorp.com

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