[1/2] lavu/parseutils: add av_small_strptime()

Message ID 1428255648-27529-1-git-send-email-stebbins@jetheaddev.com
State New
Headers show

Commit Message

John Stebbins April 5, 2015, 5:40 p.m.
From: Stefano Sabatini <stefasab@gmail.com>

Make internal small_strptime() function public, and use it in place of
strptime().
This allows to avoid a dependency on strptime() on systems which do not
support it.

In particular, fix trac ticket #992.

(cherry picked from commit 29e972f67c914d35417bc7368493d2617abdd26e)

Conflicts:
	configure
	doc/APIchanges
	libavformat/utils.c
	libavformat/version.h
	libavutil/parseutils.c
	libavutil/version.h
---
 configure              |  2 --
 doc/APIchanges         |  6 ++++++
 libavformat/utils.c    | 11 ++---------
 libavutil/parseutils.c |  8 ++++----
 libavutil/parseutils.h | 25 +++++++++++++++++++++++++
 libavutil/version.h    |  2 +-
 6 files changed, 38 insertions(+), 16 deletions(-)

Comments

Luca Barbato April 5, 2015, 9:37 p.m. | #1
On 05/04/15 19:40, John Stebbins wrote:
> From: Stefano Sabatini <stefasab@gmail.com>
>
> Make internal small_strptime() function public, and use it in place of
> strptime().
> This allows to avoid a dependency on strptime() on systems which do not
> support it.
>
> In particular, fix trac ticket #992.
>
> (cherry picked from commit 29e972f67c914d35417bc7368493d2617abdd26e)
>

We should not add patches with bugs if we know it before...

I guess that the issue is real. Alternative set coming.

lu
Diego Biurrun April 6, 2015, 3:57 p.m. | #2
On Sun, Apr 05, 2015 at 11:40:47AM -0600, John Stebbins wrote:
> From: Stefano Sabatini <stefasab@gmail.com>
> 
> Make internal small_strptime() function public, and use it in place of
> strptime().
> This allows to avoid a dependency on strptime() on systems which do not
> support it.

Which systems are affected?

Diego
Luca Barbato April 6, 2015, 4:11 p.m. | #3
On 06/04/15 17:57, Diego Biurrun wrote:
> On Sun, Apr 05, 2015 at 11:40:47AM -0600, John Stebbins wrote:
>> From: Stefano Sabatini <stefasab@gmail.com>
>>
>> Make internal small_strptime() function public, and use it in place of
>> strptime().
>> This allows to avoid a dependency on strptime() on systems which do not
>> support it.
>
> Which systems are affected?
>

Windows and that set had been reworked below.

lu
Diego Biurrun April 6, 2015, 4:15 p.m. | #4
On Mon, Apr 06, 2015 at 06:11:59PM +0200, Luca Barbato wrote:
> On 06/04/15 17:57, Diego Biurrun wrote:
> >On Sun, Apr 05, 2015 at 11:40:47AM -0600, John Stebbins wrote:
> >>From: Stefano Sabatini <stefasab@gmail.com>
> >>
> >>Make internal small_strptime() function public, and use it in place of
> >>strptime().
> >>This allows to avoid a dependency on strptime() on systems which do not
> >>support it.
> >
> >Which systems are affected?
> 
> Windows and that set had been reworked below.

Windows what?  MSVC?  MinGW?  Cygwin?  MSYS?

Diego
Luca Barbato April 6, 2015, 4:58 p.m. | #5
On 06/04/15 18:15, Diego Biurrun wrote:
> On Mon, Apr 06, 2015 at 06:11:59PM +0200, Luca Barbato wrote:
>> On 06/04/15 17:57, Diego Biurrun wrote:
>>> On Sun, Apr 05, 2015 at 11:40:47AM -0600, John Stebbins wrote:
>>>> From: Stefano Sabatini <stefasab@gmail.com>
>>>>
>>>> Make internal small_strptime() function public, and use it in place of
>>>> strptime().
>>>> This allows to avoid a dependency on strptime() on systems which do not
>>>> support it.
>>>
>>> Which systems are affected?
>>
>> Windows and that set had been reworked below.
>
> Windows what?  MSVC?  MinGW?  Cygwin?  MSYS?
>

At least the msvc libc and mingw-w64 according to a quick grep.
Given the usage of the function I didn't check any further, possibly 
some other newlib configuration might have it disabled (since it 
requires some locale stuff you might do w/out on really tiny setups).

lu
John Stebbins April 6, 2015, 4:59 p.m. | #6
On 04/06/2015 10:15 AM, Diego Biurrun wrote:
> On Mon, Apr 06, 2015 at 06:11:59PM +0200, Luca Barbato wrote:
>> On 06/04/15 17:57, Diego Biurrun wrote:
>>> On Sun, Apr 05, 2015 at 11:40:47AM -0600, John Stebbins wrote:
>>>> From: Stefano Sabatini <stefasab@gmail.com>
>>>>
>>>> Make internal small_strptime() function public, and use it in place of
>>>> strptime().
>>>> This allows to avoid a dependency on strptime() on systems which do not
>>>> support it.
>>> Which systems are affected?
>> Windows and that set had been reworked below.
> Windows what?  MSVC?  MinGW?  Cygwin?  MSYS?
>
>

Probably all of the above.  But mingw and msvc for sure.

Patch

diff --git a/configure b/configure
index 6ad813b..8821cbc 100755
--- a/configure
+++ b/configure
@@ -1487,7 +1487,6 @@  SYSTEM_FUNCS="
     setrlimit
     Sleep
     strerror_r
-    strptime
     sysconf
     sysctl
     usleep
@@ -4133,7 +4132,6 @@  check_func_headers time.h nanosleep || { check_func_headers time.h nanosleep -lr
 check_func  sched_getaffinity
 check_func  setrlimit
 check_func  strerror_r
-check_func  strptime
 check_func  sysconf
 check_func  sysctl
 check_func  usleep
diff --git a/doc/APIchanges b/doc/APIchanges
index 75eb66b..2d57368 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -16,6 +16,12 @@  API changes, most recent first:
 2015-xx-xx - xxxxxxx - lavc 56.22.0
   Add FF_PROFILE_DTS_EXPRESS.
 
+2015-xx-xx - xxxxxxx - lavu 54.11.0 - parseutils.h
+  Add av_small_strptime() time parsing function.
+
+  Can be used as a stripped-down replacement for strptime(), on
+  systems which do not support it.
+
 2015-xx-xx - xxxxxxx - lavu 54.10.0
   Add AV_PIX_FMT_MMAL for MMAL hardware acceleration.
 
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 6248c1e..8fa9871 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -2902,21 +2902,14 @@  int ff_find_stream_index(AVFormatContext *s, int id)
 
 int64_t ff_iso8601_to_unix_time(const char *datestr)
 {
-#if HAVE_STRPTIME
     struct tm time1 = { 0 }, time2 = { 0 };
     char *ret1, *ret2;
-    ret1 = strptime(datestr, "%Y - %m - %d %T", &time1);
-    ret2 = strptime(datestr, "%Y - %m - %dT%T", &time2);
+    ret1 = av_small_strptime(datestr, "%Y - %m - %d %H:%M:%S", &time1);
+    ret2 = av_small_strptime(datestr, "%Y - %m - %dT%H:%M:%S", &time2);
     if (ret2 && !ret1)
         return av_timegm(&time2);
     else
         return av_timegm(&time1);
-#else
-    av_log(NULL, AV_LOG_WARNING,
-           "strptime() unavailable on this system, cannot convert "
-           "the date string.\n");
-    return 0;
-#endif
 }
 
 int avformat_query_codec(const AVOutputFormat *ofmt, enum AVCodecID codec_id,
diff --git a/libavutil/parseutils.c b/libavutil/parseutils.c
index 4ae47ee..c03f1a2 100644
--- a/libavutil/parseutils.c
+++ b/libavutil/parseutils.c
@@ -400,7 +400,7 @@  static int date_get_num(const char **pp,
     return val;
 }
 
-static const char *small_strptime(const char *p, const char *fmt, struct tm *dt)
+const char *av_small_strptime(const char *p, const char *fmt, struct tm *dt)
 {
     int c, val;
 
@@ -518,7 +518,7 @@  int av_parse_time(int64_t *timeval, const char *timestr, int duration)
 
         /* parse the year-month-day part */
         for (i = 0; i < FF_ARRAY_ELEMS(date_fmt); i++) {
-            q = small_strptime(p, date_fmt[i], &dt);
+            q = av_small_strptime(p, date_fmt[i], &dt);
             if (q) {
                 break;
             }
@@ -542,7 +542,7 @@  int av_parse_time(int64_t *timeval, const char *timestr, int duration)
 
         /* parse the hour-minute-second part */
         for (i = 0; i < FF_ARRAY_ELEMS(time_fmt); i++) {
-            q = small_strptime(p, time_fmt[i], &dt);
+            q = av_small_strptime(p, time_fmt[i], &dt);
             if (q) {
                 break;
             }
@@ -554,7 +554,7 @@  int av_parse_time(int64_t *timeval, const char *timestr, int duration)
             ++p;
         }
         /* parse timestr as HH:MM:SS */
-        q = small_strptime(p, time_fmt[0], &dt);
+        q = av_small_strptime(p, time_fmt[0], &dt);
         if (!q) {
             char *o;
             /* parse timestr as S+ */
diff --git a/libavutil/parseutils.h b/libavutil/parseutils.h
index 0844abb..94751d1 100644
--- a/libavutil/parseutils.h
+++ b/libavutil/parseutils.h
@@ -109,6 +109,31 @@  int av_parse_color(uint8_t *rgba_color, const char *color_string, int slen,
 int av_parse_time(int64_t *timeval, const char *timestr, int duration);
 
 /**
+ * Parse the input string p according to the format string fmt and
+ * store its results in the structure dt.
+ * This implementation supports only a subset of the formats supported
+ * by the standard strptime().
+ *
+ * In particular it actually supports the parameters:
+ * - %H: the hour as a decimal number, using a 24-hour clock, in the
+ * range '00' through '23'
+ * - %M: the minute as a decimal number, using a 24-hour clock, in the
+ * range '00' through '59'
+ * - %S: the second as a decimal number, using a 24-hour clock, in the
+ * range '00' through '59'
+ * - %Y: the year as a decimal number, using the Gregorian calendar
+ * - %m: the month as a decimal number, in the range '1' through '12'
+ * - %d: the day of the month as a decimal number, in the range '1'
+ * through '31'
+ * - %%: a literal '%'
+ *
+ * @return a pointer to the first character not processed in this
+ * function call, or NULL in case the function fails to match all of
+ * the fmt string and therefore an error occurred
+ */
+const char *av_small_strptime(const char *p, const char *fmt, struct tm *dt);
+
+/**
  * Attempt to find a specific tag in a URL.
  *
  * syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done.
diff --git a/libavutil/version.h b/libavutil/version.h
index b94db74..14a406e 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -54,7 +54,7 @@ 
  */
 
 #define LIBAVUTIL_VERSION_MAJOR 54
-#define LIBAVUTIL_VERSION_MINOR 10
+#define LIBAVUTIL_VERSION_MINOR 11
 #define LIBAVUTIL_VERSION_MICRO  0
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \