Annotation of libelwix/src/time.c, revision 1.1.2.2
1.1.2.1 misho 1: #include "global.h"
2:
3:
1.1.2.2 ! misho 4: struct stridx {
! 5: char *str;
! 6: int id;
! 7: };
! 8:
! 9: static struct stridx months[] = {
! 10: { "jan", 0 }, { "january", 0 },
! 11: { "feb", 1 }, { "february", 1 },
! 12: { "mar", 2 }, { "march", 2 },
! 13: { "apr", 3 }, { "april", 3 },
! 14: { "may", 4 },
! 15: { "jun", 5 }, { "june", 5 },
! 16: { "jul", 6 }, { "july", 6 },
! 17: { "aug", 7 }, { "august", 7 },
! 18: { "sep", 8 }, { "september", 8 },
! 19: { "oct", 9 }, { "october", 9 },
! 20: { "nov", 10 }, { "november", 10 },
! 21: { "dec", 11 }, { "december", 11 },
! 22: };
! 23:
! 24: static struct stridx wdays[] = {
! 25: { "sun", 0 }, { "sunday", 0 },
! 26: { "mon", 1 }, { "monday", 1 },
! 27: { "tue", 2 }, { "tuesday", 2 },
! 28: { "wed", 3 }, { "wednesday", 3 },
! 29: { "thu", 4 }, { "thursday", 4 },
! 30: { "fri", 5 }, { "friday", 5 },
! 31: { "sat", 6 }, { "saturday", 6 },
! 32: };
! 33:
! 34: static int
! 35: stridx_compare(struct stridx * __restrict a, struct stridx * __restrict b)
! 36: {
! 37: return strcmp(a->str, b->str);
! 38: }
! 39:
! 40: static int
! 41: search4month(char * __restrict psMonth, int * __restrict id)
! 42: {
! 43: static int sorted = 0;
! 44: struct stridx *el;
! 45:
! 46: if (!psMonth)
! 47: return -1;
! 48:
! 49: if (!sorted) {
! 50: qsort(months, sizeof(months) / sizeof(struct stridx), sizeof(struct stridx),
! 51: (int (*)(const void*, const void*)) stridx_compare);
! 52: sorted++;
! 53: }
! 54:
! 55: str_Lower(psMonth);
! 56: el = bsearch(psMonth, months, sizeof(months) / sizeof(struct stridx), sizeof(struct stridx),
! 57: (int (*)(const void*, const void*)) stridx_compare);
! 58: if (el && id)
! 59: *id = el->id;
! 60:
! 61: return !!el;
! 62: }
! 63:
! 64: static int
! 65: search4wday(char * __restrict psWDay, int * __restrict id)
! 66: {
! 67: static int sorted = 0;
! 68: struct stridx *el;
! 69:
! 70: if (!psWDay)
! 71: return -1;
! 72:
! 73: if (!sorted) {
! 74: qsort(wdays, sizeof(wdays) / sizeof(struct stridx), sizeof(struct stridx),
! 75: (int (*)(const void*, const void*)) stridx_compare);
! 76: sorted++;
! 77: }
! 78:
! 79: str_Lower(psWDay);
! 80: el = bsearch(psWDay, wdays, sizeof(wdays) / sizeof(struct stridx), sizeof(struct stridx),
! 81: (int (*)(const void*, const void*)) stridx_compare);
! 82: if (el && id)
! 83: *id = el->id;
! 84:
! 85: return !!el;
! 86: }
! 87:
! 88:
! 89: /*
! 90: * time_Parse() - Parse and make unix time from standart time strings ...
! 91: *
! 92: * @csTime = Time string
! 93: * return: =0 error or !=0 converted time
! 94: */
! 95: time_t
! 96: time_Parse(const char *csTime)
! 97: {
! 98: struct tm tm;
! 99: char *s;
! 100: int tm_sec, tm_min, tm_hour, tm_mday, tm_year, tm_mon, tm_wday;
! 101: char str_mon[512], str_wday[512];
! 102:
! 103: memset(&tm, 0, sizeof tm);
! 104: memset(&str_mon, 0, sizeof str_mon);
! 105: memset(&str_wday, 0, sizeof str_wday);
! 106:
! 107: for (s = (char*) csTime; isspace(*s); s++);
! 108:
! 109: /*
! 110: * And do the sscanfs. WARNING: you can add more formats here,
! 111: * but be careful! You can easily screw up the parsing of existing
! 112: * formats when you add new ones. The order is important.
! 113: */
! 114:
! 115: /* DD-mth-YY HH:MM:SS GMT */
! 116: if (sscanf(s, "%d-%400[a-zA-Z]-%d %d:%d:%d GMT",
! 117: &tm_mday, str_mon, &tm_year,
! 118: &tm_hour, &tm_min, &tm_sec) == 6 &&
! 119: search4month(str_mon, &tm_mon)) {
! 120: tm.tm_mday = tm_mday;
! 121: tm.tm_mon = tm_mon;
! 122: tm.tm_year = tm_year;
! 123: tm.tm_hour = tm_hour;
! 124: tm.tm_min = tm_min;
! 125: tm.tm_sec = tm_sec;
! 126: }
! 127: /* DD mth YY HH:MM:SS GMT */
! 128: else if (sscanf(s, "%d %400[a-zA-Z] %d %d:%d:%d GMT",
! 129: &tm_mday, str_mon, &tm_year,
! 130: &tm_hour, &tm_min, &tm_sec) == 6 &&
! 131: search4month(str_mon, &tm_mon)) {
! 132: tm.tm_mday = tm_mday;
! 133: tm.tm_mon = tm_mon;
! 134: tm.tm_year = tm_year;
! 135: tm.tm_hour = tm_hour;
! 136: tm.tm_min = tm_min;
! 137: tm.tm_sec = tm_sec;
! 138: }
! 139: /* HH:MM:SS GMT DD-mth-YY */
! 140: else if (sscanf(s, "%d:%d:%d GMT %d-%400[a-zA-Z]-%d",
! 141: &tm_hour, &tm_min, &tm_sec,
! 142: &tm_mday, str_mon, &tm_year) == 6 &&
! 143: search4month(str_mon, &tm_mon)) {
! 144: tm.tm_hour = tm_hour;
! 145: tm.tm_min = tm_min;
! 146: tm.tm_sec = tm_sec;
! 147: tm.tm_mday = tm_mday;
! 148: tm.tm_mon = tm_mon;
! 149: tm.tm_year = tm_year;
! 150: }
! 151: /* HH:MM:SS GMT DD mth YY */
! 152: else if (sscanf(s, "%d:%d:%d GMT %d %400[a-zA-Z] %d",
! 153: &tm_hour, &tm_min, &tm_sec,
! 154: &tm_mday, str_mon, &tm_year) == 6 &&
! 155: search4month(str_mon, &tm_mon)) {
! 156: tm.tm_hour = tm_hour;
! 157: tm.tm_min = tm_min;
! 158: tm.tm_sec = tm_sec;
! 159: tm.tm_mday = tm_mday;
! 160: tm.tm_mon = tm_mon;
! 161: tm.tm_year = tm_year;
! 162: }
! 163: /* wdy, DD-mth-YY HH:MM:SS GMT */
! 164: else if (sscanf(s, "%400[a-zA-Z], %d-%400[a-zA-Z]-%d %d:%d:%d GMT",
! 165: str_wday, &tm_mday, str_mon,
! 166: &tm_year, &tm_hour, &tm_min, &tm_sec) == 7 &&
! 167: search4wday(str_wday, &tm_wday) &&
! 168: search4month(str_mon, &tm_mon)) {
! 169: tm.tm_wday = tm_wday;
! 170: tm.tm_mday = tm_mday;
! 171: tm.tm_mon = tm_mon;
! 172: tm.tm_year = tm_year;
! 173: tm.tm_hour = tm_hour;
! 174: tm.tm_min = tm_min;
! 175: tm.tm_sec = tm_sec;
! 176: }
! 177: /* wdy, DD mth YY HH:MM:SS GMT */
! 178: else if (sscanf(s, "%400[a-zA-Z], %d %400[a-zA-Z] %d %d:%d:%d GMT",
! 179: str_wday, &tm_mday, str_mon,
! 180: &tm_year, &tm_hour, &tm_min, &tm_sec) == 7 &&
! 181: search4wday(str_wday, &tm_wday) &&
! 182: search4month(str_mon, &tm_mon)) {
! 183: tm.tm_wday = tm_wday;
! 184: tm.tm_mday = tm_mday;
! 185: tm.tm_mon = tm_mon;
! 186: tm.tm_year = tm_year;
! 187: tm.tm_hour = tm_hour;
! 188: tm.tm_min = tm_min;
! 189: tm.tm_sec = tm_sec;
! 190: }
! 191: /* wdy mth DD HH:MM:SS GMT YY */
! 192: else if (sscanf(s, "%400[a-zA-Z] %400[a-zA-Z] %d %d:%d:%d GMT %d",
! 193: str_wday, str_mon, &tm_mday,
! 194: &tm_hour, &tm_min, &tm_sec, &tm_year) == 7 &&
! 195: search4wday(str_wday, &tm_wday) &&
! 196: search4month(str_mon, &tm_mon)) {
! 197: tm.tm_wday = tm_wday;
! 198: tm.tm_mon = tm_mon;
! 199: tm.tm_mday = tm_mday;
! 200: tm.tm_hour = tm_hour;
! 201: tm.tm_min = tm_min;
! 202: tm.tm_sec = tm_sec;
! 203: tm.tm_year = tm_year;
! 204: } else
! 205: return (time_t) -1;
! 206:
! 207: if (tm.tm_year > 1900)
! 208: tm.tm_year -= 1900;
! 209: else if (tm.tm_year < 70)
! 210: tm.tm_year += 100;
! 211:
! 212: return timegm(&tm);
! 213: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>