[FFmpeg-devel] [misc-parse-date PATCH 4/5] Deprecate parse_date() in favor of av_parse_time().

Stefano Sabatini stefano.sabatini-lala
Sun Jun 20 18:51:08 CEST 2010


The new function is av_ prefixed and has a slightly more
convenient/expressive interface.
---
 cmdutils.c             |    4 +-
 libavformat/avformat.h |   67 ++++++++++++++++++++++++++++++++---------------
 libavformat/utils.c    |   27 ++++++++++++++-----
 3 files changed, 67 insertions(+), 31 deletions(-)

diff --git a/cmdutils.c b/cmdutils.c
index fcdebb2..9724362 100644
--- a/cmdutils.c
+++ b/cmdutils.c
@@ -74,8 +74,8 @@ double parse_number_or_die(const char *context, const char *numstr, int type, do
 
 int64_t parse_time_or_die(const char *context, const char *timestr, int is_duration)
 {
-    int64_t us = parse_date(timestr, is_duration);
-    if (us == INT64_MIN) {
+    int64_t us;
+    if (av_parse_time(&us, timestr, is_duration) < 0) {
         fprintf(stderr, "Invalid %s specification for %s: %s\n",
                 is_duration ? "duration" : "date", context, timestr);
         exit(1);
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 49a9a18..a858d1d 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -1272,36 +1272,59 @@ attribute_deprecated int parse_image_size(int *width_ptr, int *height_ptr,
  */
 attribute_deprecated int parse_frame_rate(int *frame_rate, int *frame_rate_base,
                                           const char *arg);
-#endif
 
 /**
  * Parses datestr and returns a corresponding number of microseconds.
+ *
  * @param datestr String representing a date or a duration.
- * - If a date the syntax is:
- * @code
- *  now|{[{YYYY-MM-DD|YYYYMMDD}[T|t| ]]{{HH[:MM[:SS[.m...]]]}|{HH[MM[SS[.m...]]]}}[Z|z]}
- * @endcode
- * If the value is "now" it takes the current time.
- * Time is local time unless Z is appended, in which case it is
- * interpreted as UTC.
- * If the year-month-day part is not specified it takes the current
- * year-month-day.
- * Returns the number of microseconds since 1st of January, 1970 up to
- * the time of the parsed date or INT64_MIN if datestr cannot be
- * successfully parsed.
- * - If a duration the syntax is:
- * @code
- *  [-]HH[:MM[:SS[.m...]]]
- *  [-]S+[.m...]
- * @endcode
- * Returns the number of microseconds contained in a time interval
- * with the specified duration or INT64_MIN if datestr cannot be
- * successfully parsed.
+ * See av_parse_time() for the syntax of the provided string.
  * @param duration Flag which tells how to interpret datestr, if
  * not zero datestr is interpreted as a duration, otherwise as a
  * date.
- */
-int64_t parse_date(const char *datestr, int duration);
+ * @return the number of microseconds corresponding to the string in
+ *     datestr. If the string represents a duration, it is the number
+ *     of microseconds contained in the time interval.  If the string
+ *     is a date, is the number of microseconds since 1st of January,
+ *     1970 up to the time of the parsed date.  If datestr cannot be
+ *     successfully parsed, returns INT64_MIN.
+ */
+attribute_deprecated int64_t parse_date(const char *datestr, int duration);
+#endif
+
+/**
+ * Parses timestr and returns in *time a corresponding number of
+ * microseconds.
+ *
+ * @param timeval puts here the number of microseconds corresponding to
+ *     the string in timestr. If the string represents a duration, it
+ *     is the number of microseconds contained in the time interval.
+ *     If the string is a date, is the number of microseconds since
+ *     1st of January, 1970 up to the time of the parsed date.
+ *     If timestr cannot be successfully parsed, set *time to
+ *     INT64_MIN.
+ * @param datestr a string representing a date or a duration.
+ *     - If a date the syntax is:
+ *     @code
+ *     [{YYYY-MM-DD|YYYYMMDD}[T|t| ]]{{HH[:MM[:SS[.m...]]]}|{HH[MM[SS[.m...]]]}}[Z|z]
+ *     now
+ *     @endcode
+ *     If the value is "now" it takes the current time.
+ *     Time is local time unless Z is appended, in which case it is
+ *     interpreted as UTC.
+ *     If the year-month-day part is not specified it takes the current
+ *     year-month-day.
+ *     - If a duration the syntax is:
+ *     @code
+ *     [-]HH[:MM[:SS[.m...]]]
+ *     [-]S+[.m...]
+ *     @endcode
+ * @param duration flag which tells how to interpret timestr, if
+ *     not zero timestr is interpreted as a duration, otherwise as a
+ *     date
+ * @return 0 in case of success, a negative value corresponding to an
+ *     AVERROR code otherwise
+ */
+int av_parse_time(int64_t *timeval, const char *timestr, int duration);
 
 /** Gets the current time in microseconds. */
 int64_t av_gettime(void);
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 9faa566..20dadf7 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -3204,7 +3204,7 @@ uint64_t ff_ntp_time(void)
   return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
 }
 
-int64_t parse_date(const char *datestr, int duration)
+int av_parse_time(int64_t *timeval, const char *datestr, int duration)
 {
     const char *p;
     int64_t t;
@@ -3238,8 +3238,10 @@ int64_t parse_date(const char *datestr, int duration)
     p = datestr;
     q = NULL;
     if (!duration) {
-        if (!strncasecmp(datestr, "now", len))
-            return (int64_t) now * 1000000;
+        if (!strncasecmp(datestr, "now", len)) {
+            *timeval = (int64_t) now * 1000000;
+            return 0;
+        }
 
         /* parse the year-month-day part */
         for (i = 0; i < FF_ARRAY_ELEMS(date_fmt); i++) {
@@ -3283,9 +3285,11 @@ int64_t parse_date(const char *datestr, int duration)
         if (!q) {
             /* parse datestr as S+ */
             dt.tm_sec = strtol(p, (char **)&q, 10);
-            if (q == p)
+            if (q == p) {
                 /* the parsing didn't succeed */
-                return INT64_MIN;
+                *timeval = INT64_MIN;
+                return AVERROR(EINVAL);
+            }
             dt.tm_min = 0;
             dt.tm_hour = 0;
         }
@@ -3293,7 +3297,8 @@ int64_t parse_date(const char *datestr, int duration)
 
     /* Now we have all the fields that we can get */
     if (!q) {
-        return INT64_MIN;
+        *timeval = INT64_MIN;
+        return AVERROR(EINVAL);
     }
 
     if (duration) {
@@ -3320,7 +3325,15 @@ int64_t parse_date(const char *datestr, int duration)
         }
         t += val;
     }
-    return negative ? -t : t;
+    *timeval = negative ? -t : t;
+    return 0;
+}
+
+int64_t parse_date(const char *timestr, int duration)
+{
+    int64_t timeval;
+    av_parse_time(&timeval, timestr, duration);
+    return timeval;
 }
 
 int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
-- 
1.7.1




More information about the ffmpeg-devel mailing list