Revision f6c7375a libavformat/utils.c

View differences:

libavformat/utils.c
3380 3380
  return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
3381 3381
}
3382 3382

  
3383
int64_t parse_date(const char *datestr, int duration)
3384
{
3385
    const char *p;
3386
    int64_t t;
3387
    struct tm dt;
3388
    int i;
3389
    static const char * const date_fmt[] = {
3390
        "%Y-%m-%d",
3391
        "%Y%m%d",
3392
    };
3393
    static const char * const time_fmt[] = {
3394
        "%H:%M:%S",
3395
        "%H%M%S",
3396
    };
3397
    const char *q;
3398
    int is_utc, len;
3399
    char lastch;
3400
    int negative = 0;
3401

  
3402
#undef time
3403
    time_t now = time(0);
3404

  
3405
    len = strlen(datestr);
3406
    if (len > 0)
3407
        lastch = datestr[len - 1];
3408
    else
3409
        lastch = '\0';
3410
    is_utc = (lastch == 'z' || lastch == 'Z');
3411

  
3412
    memset(&dt, 0, sizeof(dt));
3413

  
3414
    p = datestr;
3415
    q = NULL;
3416
    if (!duration) {
3417
        if (!strncasecmp(datestr, "now", len))
3418
            return (int64_t) now * 1000000;
3419

  
3420
        /* parse the year-month-day part */
3421
        for (i = 0; i < FF_ARRAY_ELEMS(date_fmt); i++) {
3422
            q = small_strptime(p, date_fmt[i], &dt);
3423
            if (q) {
3424
                break;
3425
            }
3426
        }
3427

  
3428
        /* if the year-month-day part is missing, then take the
3429
         * current year-month-day time */
3430
        if (!q) {
3431
            if (is_utc) {
3432
                dt = *gmtime(&now);
3433
            } else {
3434
                dt = *localtime(&now);
3435
            }
3436
            dt.tm_hour = dt.tm_min = dt.tm_sec = 0;
3437
        } else {
3438
            p = q;
3439
        }
3440

  
3441
        if (*p == 'T' || *p == 't' || *p == ' ')
3442
            p++;
3443

  
3444
        /* parse the hour-minute-second part */
3445
        for (i = 0; i < FF_ARRAY_ELEMS(time_fmt); i++) {
3446
            q = small_strptime(p, time_fmt[i], &dt);
3447
            if (q) {
3448
                break;
3449
            }
3450
        }
3451
    } else {
3452
        /* parse datestr as a duration */
3453
        if (p[0] == '-') {
3454
            negative = 1;
3455
            ++p;
3456
        }
3457
        /* parse datestr as HH:MM:SS */
3458
        q = small_strptime(p, time_fmt[0], &dt);
3459
        if (!q) {
3460
            /* parse datestr as S+ */
3461
            dt.tm_sec = strtol(p, (char **)&q, 10);
3462
            if (q == p)
3463
                /* the parsing didn't succeed */
3464
                return INT64_MIN;
3465
            dt.tm_min = 0;
3466
            dt.tm_hour = 0;
3467
        }
3468
    }
3469

  
3470
    /* Now we have all the fields that we can get */
3471
    if (!q) {
3472
        return INT64_MIN;
3473
    }
3474

  
3475
    if (duration) {
3476
        t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec;
3477
    } else {
3478
        dt.tm_isdst = -1;       /* unknown */
3479
        if (is_utc) {
3480
            t = mktimegm(&dt);
3481
        } else {
3482
            t = mktime(&dt);
3483
        }
3484
    }
3485

  
3486
    t *= 1000000;
3383
#if FF_API_PARSE_DATE
3384
#include "libavutil/parseutils.h"
3487 3385

  
3488
    /* parse the .m... part */
3489
    if (*q == '.') {
3490
        int val, n;
3491
        q++;
3492
        for (val = 0, n = 100000; n >= 1; n /= 10, q++) {
3493
            if (!isdigit(*q))
3494
                break;
3495
            val += n * (*q - '0');
3496
        }
3497
        t += val;
3498
    }
3499
    return negative ? -t : t;
3386
int64_t parse_date(const char *timestr, int duration)
3387
{
3388
    int64_t timeval;
3389
    av_parse_time(&timeval, timestr, duration);
3390
    return timeval;
3500 3391
}
3392
#endif
3501 3393

  
3502 3394
int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
3503 3395
{

Also available in: Unified diff