diff mbox

[FFmpeg-devel] add locale month names to av_small_strptime

Message ID 20170220012941.26070-2-micahgalizia@gmail.com
State Superseded
Headers show

Commit Message

Micah Galizia Feb. 20, 2017, 1:29 a.m. UTC
---
 libavutil/parseutils.c       | 31 +++++++++++++++++++++++++++++++
 libavutil/tests/parseutils.c |  5 +++++
 2 files changed, 36 insertions(+)

Comments

Nicolas George Feb. 20, 2017, 10:47 a.m. UTC | #1
Le primidi 1er ventôse, an CCXXV, Micah Galizia a écrit :
> +static const char *mo_abr[] = { "jan", "feb", "mar", "apr", "may", "jun",
> +                               "jul", "aug", "sep", "oct", "nov", "dec" };
> +
> +static const char *mo_full[] = { "uary", "ruary", "ch", "il", NULL, "e", "y",
> +                                "ust", "tember", "ober", "ember", "ember" };
> +

I do not have the time for a full review, but this was directly obvious
to me.

First, the indentation is strange.

Second, since you are using av_strncasecmp(3) anyway, no need to split
the words like that: month_name[i] will do fine for av_strncasecmp(3)
and month_name[i]+3 will do fine for your mo_full, which is a misnomer.

Regards,
diff mbox

Patch

diff --git a/libavutil/parseutils.c b/libavutil/parseutils.c
index 86d3dac..a273216 100644
--- a/libavutil/parseutils.c
+++ b/libavutil/parseutils.c
@@ -140,6 +140,12 @@  static const VideoRateAbbr video_rate_abbrs[]= {
     { "ntsc-film", { 24000, 1001 } },
 };
 
+static const char *mo_abr[] = { "jan", "feb", "mar", "apr", "may", "jun",
+                               "jul", "aug", "sep", "oct", "nov", "dec" };
+
+static const char *mo_full[] = { "uary", "ruary", "ch", "il", NULL, "e", "y",
+                                "ust", "tember", "ober", "ember", "ember" };
+
 int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
 {
     int i;
@@ -466,6 +472,23 @@  static int date_get_num(const char **pp,
     return val;
 }
 
+static int date_get_month(const char **pp) {
+    for (int i = 0; i < 12; i++) {
+        if (!av_strncasecmp(*pp, mo_abr[i], 3)) {
+            *pp += 3;
+            if (mo_full[i] != NULL) {
+                int len = strlen(mo_full[i]);
+                if (!av_strncasecmp(*pp, mo_full[i], len)) {
+                    *pp += len;
+                    return i;
+                }
+            }
+            return i;
+        }
+    }
+    return -1;
+}
+
 char *av_small_strptime(const char *p, const char *fmt, struct tm *dt)
 {
     int c, val;
@@ -525,6 +548,14 @@  char *av_small_strptime(const char *p, const char *fmt, struct tm *dt)
             if (!p)
                 return NULL;
             break;
+        case 'b':
+        case 'B':
+        case 'h':
+            val = date_get_month(&p);
+            if (val == -1)
+                return NULL;
+            dt->tm_mon = val;
+            break;
         case '%':
             if (*p++ != '%')
                 return NULL;
diff --git a/libavutil/tests/parseutils.c b/libavutil/tests/parseutils.c
index 682b390..e9b9599 100644
--- a/libavutil/tests/parseutils.c
+++ b/libavutil/tests/parseutils.c
@@ -138,6 +138,11 @@  static void test_av_small_strptime(void)
         { "%Y - %m - %d",                "2012-12-21" },
         { "%Y-%m-%d %H:%M:%S",           "2012-12-21 20:12:21" },
         { "  %Y - %m - %d %H : %M : %S", "   2012 - 12 -  21   20 : 12 : 21" },
+        { "  %Y - %b - %d %H : %M : %S", "   2012 - nOV -  21   20 : 12 : 21" },
+        { "  %Y - %B - %d %H : %M : %S", "   2012 - nOVemBeR -  21   20 : 12 : 21" },
+        { "  %Y - %B - %d %H : %M : %S", "   2012 - may -  21   20 : 12 : 21" },
+        { "  %Y - %B - %d %H : %M : %S", "   2012 - JunE -  21   20 : 12 : 21" },
+        { "  %Y - %B - %d %H : %M : %S", "   2012 - January -  21   20 : 12 : 21" },
     };
 
     av_log_set_level(AV_LOG_DEBUG);