[asterisk-commits] tilghman: branch tilghman/strptime-1.4 r83173 - in /team/tilghman/strptime-1....

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Sep 19 13:24:58 CDT 2007


Author: tilghman
Date: Wed Sep 19 13:24:57 2007
New Revision: 83173

URL: http://svn.digium.com/view/asterisk?view=rev&rev=83173
Log:
Written, but untested.  In a branch, for now.

Modified:
    team/tilghman/strptime-1.4/funcs/func_strings.c
    team/tilghman/strptime-1.4/include/asterisk/localtime.h
    team/tilghman/strptime-1.4/main/stdtime/localtime.c

Modified: team/tilghman/strptime-1.4/funcs/func_strings.c
URL: http://svn.digium.com/view/asterisk/team/tilghman/strptime-1.4/funcs/func_strings.c?view=diff&rev=83173&r1=83172&r2=83173
==============================================================================
--- team/tilghman/strptime-1.4/funcs/func_strings.c (original)
+++ team/tilghman/strptime-1.4/funcs/func_strings.c Wed Sep 19 13:24:57 2007
@@ -483,8 +483,8 @@
 		return -1;
 	}
 
-	if (!strptime(args.timestring, args.format, &time)) {
-		ast_log(LOG_WARNING, "C function strptime() output nothing?!!\n");
+	if (!ast_strptime(args.timestring, args.format, &time)) {
+		ast_log(LOG_WARNING, "C function ast_strptime() output nothing?!!\n");
 	} else {
 		snprintf(buf, len, "%d", (int) ast_mktime(&time, args.timezone));
 	}

Modified: team/tilghman/strptime-1.4/include/asterisk/localtime.h
URL: http://svn.digium.com/view/asterisk/team/tilghman/strptime-1.4/include/asterisk/localtime.h?view=diff&rev=83173&r1=83172&r2=83173
==============================================================================
--- team/tilghman/strptime-1.4/include/asterisk/localtime.h (original)
+++ team/tilghman/strptime-1.4/include/asterisk/localtime.h Wed Sep 19 13:24:57 2007
@@ -26,5 +26,6 @@
 
 struct tm *ast_localtime(const time_t *timep, struct tm *p_tm, const char *zone);
 time_t ast_mktime(struct tm * const tmp, const char *zone);
+char *ast_strptime(const char *timestr, const char *format, struct tm *tm);
 
 #endif /* _ASTERISK_LOCALTIME_H */

Modified: team/tilghman/strptime-1.4/main/stdtime/localtime.c
URL: http://svn.digium.com/view/asterisk/team/tilghman/strptime-1.4/main/stdtime/localtime.c?view=diff&rev=83173&r1=83172&r2=83173
==============================================================================
--- team/tilghman/strptime-1.4/main/stdtime/localtime.c (original)
+++ team/tilghman/strptime-1.4/main/stdtime/localtime.c Wed Sep 19 13:24:57 2007
@@ -51,6 +51,7 @@
 #include <stdio.h>
 #endif
 #include <float.h>
+#include <langinfo.h>
 
 
 #include "private.h"
@@ -1645,3 +1646,227 @@
 	return time1(tmp, localsub, 0L, sp);
 }
 
+static const char *scan_integer(const char *str, int *value, int min, int max)
+{
+	const char *remainder = str;
+
+	*value = 0;
+	while (strchr(" \n\r\t", *remainder))
+		remainder++;
+	while (strchr("0123456789", *remainder)) {
+		if (*value * 10 + (*remainder - '0') > max)
+			break;
+		*value *= 10;
+		*value += *remainder - '0';
+		remainder++;
+	}
+
+	if (*value < min || *value > max)
+		return NULL;
+	else
+		return remainder;
+}
+
+char *ast_strptime(const char *timestr, const char *format, struct tm *tm)
+{
+	int century = -1;
+
+	for (; *format && *timestr; format++) {
+		if (*format == '%') {
+			format++;
+			if (*format == '%') {
+				if (*timestr == '%')
+					timestr++;
+				else
+					break;
+			} else if (*format == 'a' || *format == 'A') {
+				/* Weekday name - otherwise ignored */
+				int i, ok = 0;
+				char *name;
+				size_t namelen;
+
+				for (i = 1; i <= 12; i++) {
+					if ((name = nl_langinfo(DAY_1 - 1 + i)) && (namelen = strlen(name)) && strncasecmp(name, timestr, namelen) == 0) {
+						timestr += namelen;
+						ok = 1;
+						break;
+					}
+					if ((name = nl_langinfo(ABDAY_1 - 1 + i)) && (namelen = strlen(name)) && strncasecmp(name, timestr, namelen) == 0) {
+						timestr += namelen;
+						ok = 1;
+						break;
+					}
+				}
+				if (!ok)
+					break;
+			} else if (*format == 'b' || *format == 'B' || *format == 'h') {
+				/* Month name */
+				int i, ok = 0;
+				char *name;
+				size_t namelen;
+				for (i = 1; i <= 12; i++) {
+					if ((name = nl_langinfo(MON_1 - 1 + i)) && (namelen = strlen(name)) && strncasecmp(name, timestr, namelen) == 0) {
+						timestr += namelen;
+						ok = 1;
+						break;
+					}
+					if ((name = nl_langinfo(ABMON_1 - 1 + i)) && (namelen = strlen(name)) && strncasecmp(name, timestr, namelen) == 0) {
+						timestr += namelen;
+						ok = 1;
+						break;
+					}
+				}
+				if (!ok)
+					break;
+			} else if (*format == 'C') {
+				/* Century */
+				const char *tmp;
+				if ((tmp = scan_integer(timestr, &century, 0, 99)))
+					timestr = tmp;
+				else
+					break;
+			} else if (*format == 'd' || *format == 'e') {
+				/* Day of month */
+				const char *tmp;
+				if ((tmp = scan_integer(timestr, &tm->tm_hour, 0, 31)))
+					timestr = tmp;
+				else
+					break;
+			} else if (*format == 'D') {
+				/* %m/%d/%y */
+				const char *tmp = ast_strptime(timestr, "%m/%d/%y", tm);
+				if (tmp != NULL)
+					timestr = tmp;
+				else
+					break;
+			} else if (*format == 'F') {
+				/* %Y-%m-%d */
+				const char *tmp = ast_strptime(timestr, "%Y-%m-%d", tm);
+				if (tmp != NULL)
+					timestr = tmp;
+				else
+					break;
+			} else if (*format == 'H') {
+				/* 24 hour */
+				const char *tmp;
+				if ((tmp = scan_integer(timestr, &tm->tm_hour, 0, 23)))
+					timestr = tmp;
+				else
+					break;
+			} else if (*format == 'I') {
+				/* 12 hour */
+				const char *tmp;
+				if ((tmp = scan_integer(timestr, &tm->tm_hour, 1, 12))) {
+					if (tm->tm_hour == 12)
+						tm->tm_hour = 0;
+					timestr = tmp;
+				} else
+					break;
+			} else if (*format == 'j') {
+				/* tm_yday */
+				const char *tmp;
+				if ((tmp = scan_integer(timestr, &tm->tm_yday, 1, 366)))
+					timestr = tmp;
+				else
+					break;
+			} else if (*format == 'm') {
+				/* tm_mon */
+				const char *tmp;
+				if ((tmp = scan_integer(timestr, &tm->tm_mon, 1, 12))) {
+					tm->tm_mon--;
+					timestr = tmp;
+				} else
+					break;
+			} else if (*format == 'M') {
+				/* tm_min */
+				const char *tmp;
+				if ((tmp = scan_integer(timestr, &tm->tm_min, 0, 60)))
+					timestr = tmp;
+				else
+					break;
+			} else if (*format == 'n' || *format == 't') {
+				/* arbitrary whitespace */
+				while (strchr(" \n\r\t", *timestr))
+					timestr++;
+			} else if (*format == 'p') {
+				/* am/pm */
+				char *name;
+				size_t namelen;
+				if ((name = nl_langinfo(AM_STR)) && (namelen = strlen(name)) && strncasecmp(timestr, name, namelen) == 0)
+					timestr += namelen;
+				else if ((name = nl_langinfo(PM_STR)) && (namelen = strlen(name)) && strncasecmp(timestr, name, namelen) == 0)
+					timestr += namelen;
+				else
+					break;
+			} else if (*format == 'r') {
+				/* %I:%M:%S %p */
+				const char *tmp = ast_strptime(timestr, "%I:%M:%S %p", tm);
+				if (tmp != NULL)
+					timestr = tmp;
+				else
+					break;
+			} else if (*format == 'R') {
+				/* %H:%M */
+				const char *tmp = ast_strptime(timestr, "%H:%M", tm);
+				if (tmp != NULL)
+					timestr = tmp;
+				else
+					break;
+			} else if (*format == 'S') {
+				/* tm_sec */
+				const char *tmp;
+				if ((tmp = scan_integer(timestr, &tm->tm_min, 0, 60)))
+					timestr = tmp;
+				else
+					break;
+			} else if (*format == 'T') {
+				/* %H:%M:%S */
+				const char *tmp = ast_strptime(timestr, "%H:%M:%S", tm);
+				if (tmp != NULL)
+					timestr = tmp;
+				else
+					break;
+			} else if (*format == 'U') {
+				/* XXX Week number */
+
+				break;
+			} else if (*format == 'w') {
+				/* XXX Weekday number (sunday-based) */
+
+				break;
+			} else if (*format == 'W') {
+				/* XXX Weekday number (monday-based) */
+
+				break;
+			} else if (*format == 'y') {
+				/* 2-digit year */
+				const char *tmp;
+				if ((tmp = scan_integer(timestr, &tm->tm_year, 0, 99))) {
+					if (century != -1)
+						tm->tm_year += century * 100 - 1900;
+					else if (tm->tm_year < 69)
+						tm->tm_year += 100;
+					timestr = tmp;
+				} else
+					break;
+			} else if (*format == 'Y') {
+				/* 4-digit year */
+				const char *tmp;
+				if ((tmp = scan_integer(timestr, &tm->tm_year, 1000, 9999))) {
+					tm->tm_year -= 1900;
+					timestr = tmp;
+				} else
+					break;
+			} else {
+				/* Not supported */
+				break;
+			}
+		} else if (*format == *timestr)
+			timestr++;
+		else
+			break;
+	}
+
+	return *format != '\0' ? NULL : (char *)timestr;
+}
+




More information about the asterisk-commits mailing list