RationalTime::from_time_string rejects any time string with a leading '-', including the output of to_time_string

未关闭 适合新手
#2,106 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@kolinabir 已经在做这个了。

开始于 2026年11月27日。

  • #2113 来自 @kolinabir —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cpp, python
领域
backend

调研方向

解析循环是 src/opentime/rationalTime.cpp 中的 from_time_string(约第 395-410 行)。对照 rationalTime.h 第 214-218 行记录的约定来阅读它,并检查 to_time_string,以确认它输出的负数字符串的确切格式。使用 CMake 构建 opentime 目标,并运行 issue 中的复现程序。然后在 tests/test_opentime.py(test_time_time_string_negative_rational_time)和 C++ 的 opentime 测试中添加用例。当 -01:00:00、-10 和 -00:00:01.0 被解析为预期的负秒数并能往返转换时,即完成。

由索引模型根据 Issue 内容生成。

描述

Required:


[x] I believe this isn't a duplicate topic
[x] This report is not related to an adapter

Select One:

[ ] Build problem
[x] Incorrect Functionality or bug
[ ] New feature or functionality

Description

RationalTime::from_time_string() returns INVALID_TIME_STRING for every string that starts with a minus sign, for example "-01:00:00", "-10" or "-00:00:01.0".

The header says a leading minus is allowed (rationalTime.h, lines 214-218):

/// @brief Parse a string in the form "hours:minutes:seconds".
///
/// The string may have a leading negative sign.

to_time_string() produces such strings for negative times (since #605), and the Python test test_time_time_string_negative_rational_time checks that RationalTime(-24, 24) gives "-00:00:01.0". Passing that exact string back to from_time_string() fails, so a negative time cannot make a round trip through a time string.

Expected: from_time_string("-01:00:00", 24) returns -3600 seconds (value -86400 at rate 24) with no error, and from_time_string("-00:00:01.0", 24) returns -1 second, like RationalTime(-24, 24).

Suggestion for the cause: in the backward scan of from_time_string (rationalTime.cpp, lines 395-404), the branch that rejects non-digit characters also runs when current reaches the leading '-', before the start == current branch (line 406) can call parseFloat(..., true, ...) with negatives allowed. Even past that point, the sign would only apply to the hours field ("-00:00:01.0" would give +1 s), so it may be simpler to strip a leading '-' first and negate the total at the end.

Reproduction Steps

Build against the C++ library (opentime) and run:

#include <opentime/errorStatus.h>
#include <opentime/rationalTime.h>

#include <cstdio>
#include <string>

using namespace opentime::OPENTIME_VERSION_NS;

static int failures = 0;

static void check(
    const char*        label,
    const std::string& input,
    double             rate,
    double             expected_seconds)
{
    ErrorStatus  es;
    RationalTime rt = RationalTime::from_time_string(input, rate, &es);

    const bool err = is_error(es);
    printf(
        "%s: input=\"%s\" rate=%g -> error=%s code=%d valid=%d value=%g "
        "rate=%g to_seconds=%.6f\n",
        label,
        input.c_str(),
        rate,
        err ? "yes" : "no",
        (int) es.outcome,
        (int) rt.is_valid_time(),
        rt.value(),
        rt.rate(),
        rt.to_seconds());

    if (err)
    {
        printf(
            "FAIL %s: \"%s\" rejected (outcome=%d details=\"%s\"); header "
            "says \"The string may have a leading negative sign\", expected "
            "to_seconds=%.6f\n",
            label,
            input.c_str(),
            (int) es.outcome,
            es.details.c_str(),
            expected_seconds);
        ++failures;
        return;
    }
    if (rt.to_seconds() != expected_seconds)
    {
        printf(
            "FAIL %s: parsed to_seconds=%.6f, expected %.6f\n",
            label,
            rt.to_seconds(),
            expected_seconds);
        ++failures;
    }
}

int main()
{
    // positive control
    {
        ErrorStatus  es;
        RationalTime rt = RationalTime::from_time_string("01:00:00", 24, &es);
        printf(
            "control positive: error=%s to_seconds=%.6f\n",
            is_error(es) ? "yes" : "no",
            rt.to_seconds());
        if (is_error(es) || rt.to_seconds() != 3600.0)
        {
            printf("FAIL control: positive string did not parse\n");
            ++failures;
        }
    }

    // leading negative sign
    check("minus-hhmmss", "-01:00:00", 24, -3600.0);
    check("minus-hhmmss-frac", "-01:00:00.0", 24, -3600.0);
    check("minus-min", "-1:00:00", 24, -3600.0);
    check("minus-seconds-only", "-10", 24, -10.0);

    // round trip through to_time_string() / from_time_string()
    {
        RationalTime rt(-24, 24);
        std::string  ts = rt.to_time_string();
        printf("to_time_string(RationalTime(-24,24)) = \"%s\"\n", ts.c_str());
        check("roundtrip-neg-1s", ts, 24, -1.0);
    }
    {
        RationalTime rt(-3600, 1);
        std::string  ts = rt.to_time_string();
        printf("to_time_string(RationalTime(-3600,1)) = \"%s\"\n", ts.c_str());
        check("roundtrip-neg-1h", ts, 24, -3600.0);
    }

    if (failures)
    {
        return 1;
    }
    printf("all negative time strings parsed as documented\n");
    return 0;
}

Log output if appropriate

Observed:

control positive: error=no to_seconds=3600.000000
minus-hhmmss: input="-01:00:00" rate=24 -> error=yes code=3 valid=0 value=0 rate=-1 to_seconds=0.000000
FAIL minus-hhmmss: "-01:00:00" rejected (outcome=3 details="Error: '-01:00:00' - invalid time string"); header says "The string may have a leading negative sign", expected to_seconds=-3600.000000
minus-hhmmss-frac: input="-01:00:00.0" rate=24 -> error=yes code=3 valid=0 value=0 rate=-1 to_seconds=0.000000
FAIL minus-hhmmss-frac: "-01:00:00.0" rejected (outcome=3 details="Error: '-01:00:00.0' - invalid time string"); header says "The string may have a leading negative sign", expected to_seconds=-3600.000000
minus-min: input="-1:00:00" rate=24 -> error=yes code=3 valid=0 value=0 rate=-1 to_seconds=0.000000
FAIL minus-min: "-1:00:00" rejected (outcome=3 details="Error: '-1:00:00' - invalid time string"); header says "The string may have a leading negative sign", expected to_seconds=-3600.000000
minus-seconds-only: input="-10" rate=24 -> error=yes code=3 valid=0 value=0 rate=-1 to_seconds=0.000000
FAIL minus-seconds-only: "-10" rejected (outcome=3 details="Error: '-10' - invalid time string"); header says "The string may have a leading negative sign", expected to_seconds=-10.000000
to_time_string(RationalTime(-24,24)) = "-00:00:01.0"
roundtrip-neg-1s: input="-00:00:01.0" rate=24 -> error=yes code=3 valid=0 value=0 rate=-1 to_seconds=0.000000
FAIL roundtrip-neg-1s: "-00:00:01.0" rejected (outcome=3 details="Error: '-00:00:01.0' - invalid time string"); header says "The string may have a leading negative sign", expected to_seconds=-1.000000
to_time_string(RationalTime(-3600,1)) = "-01:00:00.0"
roundtrip-neg-1h: input="-01:00:00.0" rate=24 -> error=yes code=3 valid=0 value=0 rate=-1 to_seconds=0.000000
FAIL roundtrip-neg-1h: "-01:00:00.0" rejected (outcome=3 details="Error: '-01:00:00.0' - invalid time string"); header says "The string may have a leading negative sign", expected to_seconds=-3600.000000

Expected: no FAIL line. Every negative string is accepted with no error and gives -3600 s, -3600 s, -3600 s, -10 s, -1 s and -3600 s respectively, and the program prints all negative time strings parsed as documented.

Optional


Environment

OpenTimelineIO main at bad7ab5a40ac1a95d9d78a87c95a688d8b57cf28, and run again at f6c34bc6c3337cdd27db77c983c6074130c1c91c (20 Nov 2026) with the same result, C++ library opentime.
Operating System: Windows, MSVC (Visual Studio 2022 Build Tools), the project's CMake build in Debug configuration with AddressSanitizer (/fsanitize=address).
Python version if appropriate: n/a (C++ API; the same function is exposed as otio.opentime.from_time_string).

Found by VaultLens, an analysis tool that uses AI to catch bugs. If anything is wrong, please let us know.

主要语言
C++
星标
2.1k
派生
367
平均合并
3 天 22 小时
30 天内合并 PR
14

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

AcademySoftwareFoundation/OpenTimelineIO 的其他 Issue

查看 AcademySoftwareFoundation/OpenTimelineIO 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。