Revision f6c7375a libavutil/parseutils.c

View differences:

libavutil/parseutils.c
22 22
 */
23 23

  
24 24
#include <strings.h>
25
#include <sys/time.h>
26
#include <time.h>
25 27
#include "parseutils.h"
26 28
#include "libavutil/avutil.h"
27 29
#include "libavutil/eval.h"
......
371 373
    return 0;
372 374
}
373 375

  
376
/* get a positive number between n_min and n_max, for a maximum length
377
   of len_max. Return -1 if error. */
378
static int date_get_num(const char **pp,
379
                        int n_min, int n_max, int len_max)
380
{
381
    int i, val, c;
382
    const char *p;
383

  
384
    p = *pp;
385
    val = 0;
386
    for(i = 0; i < len_max; i++) {
387
        c = *p;
388
        if (!isdigit(c))
389
            break;
390
        val = (val * 10) + c - '0';
391
        p++;
392
    }
393
    /* no number read ? */
394
    if (p == *pp)
395
        return -1;
396
    if (val < n_min || val > n_max)
397
        return -1;
398
    *pp = p;
399
    return val;
400
}
401

  
402
/* small strptime for ffmpeg */
403
static
404
const char *small_strptime(const char *p, const char *fmt,
405
                           struct tm *dt)
406
{
407
    int c, val;
408

  
409
    for(;;) {
410
        c = *fmt++;
411
        if (c == '\0') {
412
            return p;
413
        } else if (c == '%') {
414
            c = *fmt++;
415
            switch(c) {
416
            case 'H':
417
                val = date_get_num(&p, 0, 23, 2);
418
                if (val == -1)
419
                    return NULL;
420
                dt->tm_hour = val;
421
                break;
422
            case 'M':
423
                val = date_get_num(&p, 0, 59, 2);
424
                if (val == -1)
425
                    return NULL;
426
                dt->tm_min = val;
427
                break;
428
            case 'S':
429
                val = date_get_num(&p, 0, 59, 2);
430
                if (val == -1)
431
                    return NULL;
432
                dt->tm_sec = val;
433
                break;
434
            case 'Y':
435
                val = date_get_num(&p, 0, 9999, 4);
436
                if (val == -1)
437
                    return NULL;
438
                dt->tm_year = val - 1900;
439
                break;
440
            case 'm':
441
                val = date_get_num(&p, 1, 12, 2);
442
                if (val == -1)
443
                    return NULL;
444
                dt->tm_mon = val - 1;
445
                break;
446
            case 'd':
447
                val = date_get_num(&p, 1, 31, 2);
448
                if (val == -1)
449
                    return NULL;
450
                dt->tm_mday = val;
451
                break;
452
            case '%':
453
                goto match;
454
            default:
455
                return NULL;
456
            }
457
        } else {
458
        match:
459
            if (c != *p)
460
                return NULL;
461
            p++;
462
        }
463
    }
464
    return p;
465
}
466

  
467
static time_t mktimegm(struct tm *tm)
468
{
469
    time_t t;
470

  
471
    int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
472

  
473
    if (m < 3) {
474
        m += 12;
475
        y--;
476
    }
477

  
478
    t = 86400 *
479
        (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
480

  
481
    t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
482

  
483
    return t;
484
}
485

  
486
int av_parse_time(int64_t *timeval, const char *datestr, int duration)
487
{
488
    const char *p;
489
    int64_t t;
490
    struct tm dt;
491
    int i;
492
    static const char * const date_fmt[] = {
493
        "%Y-%m-%d",
494
        "%Y%m%d",
495
    };
496
    static const char * const time_fmt[] = {
497
        "%H:%M:%S",
498
        "%H%M%S",
499
    };
500
    const char *q;
501
    int is_utc, len;
502
    char lastch;
503
    int negative = 0;
504

  
505
#undef time
506
    time_t now = time(0);
507

  
508
    len = strlen(datestr);
509
    if (len > 0)
510
        lastch = datestr[len - 1];
511
    else
512
        lastch = '\0';
513
    is_utc = (lastch == 'z' || lastch == 'Z');
514

  
515
    memset(&dt, 0, sizeof(dt));
516

  
517
    p = datestr;
518
    q = NULL;
519
    if (!duration) {
520
        if (!strncasecmp(datestr, "now", len)) {
521
            *timeval = (int64_t) now * 1000000;
522
            return 0;
523
        }
524

  
525
        /* parse the year-month-day part */
526
        for (i = 0; i < FF_ARRAY_ELEMS(date_fmt); i++) {
527
            q = small_strptime(p, date_fmt[i], &dt);
528
            if (q) {
529
                break;
530
            }
531
        }
532

  
533
        /* if the year-month-day part is missing, then take the
534
         * current year-month-day time */
535
        if (!q) {
536
            if (is_utc) {
537
                dt = *gmtime(&now);
538
            } else {
539
                dt = *localtime(&now);
540
            }
541
            dt.tm_hour = dt.tm_min = dt.tm_sec = 0;
542
        } else {
543
            p = q;
544
        }
545

  
546
        if (*p == 'T' || *p == 't' || *p == ' ')
547
            p++;
548

  
549
        /* parse the hour-minute-second part */
550
        for (i = 0; i < FF_ARRAY_ELEMS(time_fmt); i++) {
551
            q = small_strptime(p, time_fmt[i], &dt);
552
            if (q) {
553
                break;
554
            }
555
        }
556
    } else {
557
        /* parse datestr as a duration */
558
        if (p[0] == '-') {
559
            negative = 1;
560
            ++p;
561
        }
562
        /* parse datestr as HH:MM:SS */
563
        q = small_strptime(p, time_fmt[0], &dt);
564
        if (!q) {
565
            /* parse datestr as S+ */
566
            dt.tm_sec = strtol(p, (char **)&q, 10);
567
            if (q == p) {
568
                /* the parsing didn't succeed */
569
                *timeval = INT64_MIN;
570
                return AVERROR(EINVAL);
571
            }
572
            dt.tm_min = 0;
573
            dt.tm_hour = 0;
574
        }
575
    }
576

  
577
    /* Now we have all the fields that we can get */
578
    if (!q) {
579
        *timeval = INT64_MIN;
580
        return AVERROR(EINVAL);
581
    }
582

  
583
    if (duration) {
584
        t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec;
585
    } else {
586
        dt.tm_isdst = -1;       /* unknown */
587
        if (is_utc) {
588
            t = mktimegm(&dt);
589
        } else {
590
            t = mktime(&dt);
591
        }
592
    }
593

  
594
    t *= 1000000;
595

  
596
    /* parse the .m... part */
597
    if (*q == '.') {
598
        int val, n;
599
        q++;
600
        for (val = 0, n = 100000; n >= 1; n /= 10, q++) {
601
            if (!isdigit(*q))
602
                break;
603
            val += n * (*q - '0');
604
        }
605
        t += val;
606
    }
607
    *timeval = negative ? -t : t;
608
    return 0;
609
}
610

  
374 611
#ifdef TEST
375 612

  
376 613
#undef printf

Also available in: Unified diff