RationalTime::to_time_string gives a malformed or wrong string when the fractional second is below 0.0001

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

维护者通常 1 天内回复

@SoulmatelynchVFX 已经在做这个了。

开始于 2026年11月25日。

  • #2112 来自 @SoulmatelynchVFX —— 未关闭

评估

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

调研方向

错误的格式来自 src/opentime/rationalTime.cpp 中(大约第 634-647 行)的小数秒代码,它使用了 %.7g 以及 substr(1) 和 resize(7)。编译 issue 中的复现程序,然后检查小数部分的格式以及 from_time_string 中的解析路径。当复现程序不打印任何 FAIL 行、往返转换保持在一微秒以内,并且 Python 测试 test_long_running_time_string_24 仍然通过时,即完成。

由索引模型根据 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

When the fractional part of the seconds is between one microsecond and 0.0001 s, RationalTime::to_time_string() returns either a string with no decimal point, which from_time_string() rejects, or a well-formed string with the wrong value:

  • 50 microseconds (RationalTime(0.00005, 1.0), or RationalTime(1.0, 20000.0)) gives "00:00:00e-05";
  • 3.00005 s gives "00:00:03e-05";
  • one sample at 24000 Hz (RationalTime(1.0, 24000.0), about 0.0000417 s) gives "00:00:00.166667", which reads back as 0.166667 s, 4000 times too large;
  • one sample at 96000 Hz gives "00:00:00.041667" instead of about 0.0000104 s.

The header promises microsecond precision in the "hours:minutes:seconds" form (rationalTime.h, lines 278-283):

/// @brief Return a string in the form "hours:minutes:seconds".
///
/// Seconds may have up to microsecond precision.

and the project's tests expect from_time_string(to_time_string(rt)) to give back rt (test_long_running_time_string_24). 0.0001 s and 0.001 s are formatted correctly ("00:00:00.0001", "00:00:00.001"); only fractions below 0.0001 s go wrong.

Expected: a decimal seconds field, for example "00:00:00.00005" for 50 microseconds and "00:00:00.000042" (or .000041) for 1/24000 s, which from_time_string() accepts and reads back as about the same time.

Suggestion for the cause: rationalTime.cpp, lines 634-647 formats the fraction with string_printf("%.7g", fractpart), which switches to exponent notation below 1e-4 ("5e-05", "4.166667e-05"), then substr(1) drops the first character assuming it is the 0 of "0.", and resize(7) cuts the rest. A fixed-point format for the fraction would avoid this.

Reproduction Steps

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

#include <opentime/rationalTime.h>

#include <cmath>
#include <cstdio>
#include <string>

using namespace opentime::OPENTIME_VERSION_NS;

static int failures = 0;

static void
check(RationalTime rt, double parseRate, const char* label)
{
    std::string ts = rt.to_time_string();

    bool hasDot = ts.find('.') != std::string::npos;

    ErrorStatus  err;
    RationalTime back     = RationalTime::from_time_string(ts, parseRate, &err);
    bool         rejected = is_error(err);

    std::string parseMsg =
        rejected ? ("NO(" + err.details + ")") : std::string("yes");

    printf(
        "%-20s secs=%.12g -> \"%s\" (len=%zu, dot=%s) parse=%s\n",
        label,
        rt.to_seconds(),
        ts.c_str(),
        ts.size(),
        hasDot ? "yes" : "NO",
        parseMsg.c_str());

    if (!hasDot)
    {
        printf("  FAIL: malformed time string, no '.' fraction field\n");
        ++failures;
    }
    if (rejected)
    {
        printf("  FAIL: from_time_string rejects the string to_time_string produced\n");
        ++failures;
    }
    else if (std::fabs(back.to_seconds() - rt.to_seconds()) > 1e-6)
    {
        // more than one microsecond away
        printf(
            "  FAIL: round trip changes the value: got %.12g s, want %.12g s\n",
            back.to_seconds(),
            rt.to_seconds());
        ++failures;
    }
}

int
main()
{
    printf("=== fraction small enough for %%.7g exponential form ===\n");
    check(RationalTime(0.00005, 1.0), 24.0, "5e-05 s");
    check(RationalTime(3.00005, 1.0), 24.0, "3.00005 s");
    check(RationalTime(1.0, 20000.0), 25.0, "1/20000 s");

    printf("=== well-formed but wrong value (exponential, truncated) ===\n");
    check(RationalTime(1.0, 24000.0), 24.0, "1/24000 s");
    check(RationalTime(1.0, 96000.0), 24.0, "1/96000 s");

    printf("=== ordinary cases (control) ===\n");
    check(RationalTime(0.0, 24.0), 24.0, "zero");
    check(RationalTime(0.0001, 1.0), 24.0, "1e-04 s");
    check(RationalTime(0.001, 1.0), 24.0, "1e-03 s");

    return failures ? 1 : 0;
}

Log output if appropriate

Observed:

=== fraction small enough for %.7g exponential form ===
5e-05 s              secs=5e-05 -> "00:00:00e-05" (len=12, dot=NO) parse=NO(Error: '00:00:00e-05' - invalid time string)
  FAIL: malformed time string, no '.' fraction field
  FAIL: from_time_string rejects the string to_time_string produced
3.00005 s            secs=3.00005 -> "00:00:03e-05" (len=12, dot=NO) parse=NO(Error: '00:00:03e-05' - invalid time string)
  FAIL: malformed time string, no '.' fraction field
  FAIL: from_time_string rejects the string to_time_string produced
1/20000 s            secs=5e-05 -> "00:00:00e-05" (len=12, dot=NO) parse=NO(Error: '00:00:00e-05' - invalid time string)
  FAIL: malformed time string, no '.' fraction field
  FAIL: from_time_string rejects the string to_time_string produced
=== well-formed but wrong value (exponential, truncated) ===
1/24000 s            secs=4.16666666667e-05 -> "00:00:00.166667" (len=15, dot=yes) parse=yes
  FAIL: round trip changes the value: got 0.166667 s, want 4.16666666667e-05 s
1/96000 s            secs=1.04166666667e-05 -> "00:00:00.041667" (len=15, dot=yes) parse=yes
  FAIL: round trip changes the value: got 0.041667 s, want 1.04166666667e-05 s
=== ordinary cases (control) ===
zero                 secs=0 -> "00:00:00.0" (len=10, dot=yes) parse=yes
1e-04 s              secs=0.0001 -> "00:00:00.0001" (len=13, dot=yes) parse=yes
1e-03 s              secs=0.001 -> "00:00:00.001" (len=12, dot=yes) parse=yes

Expected: no FAIL line. The first five times are written with a decimal point and microsecond digits (for example "00:00:00.00005" and "00:00:03.00005"), from_time_string() accepts them, and the value read back is within one microsecond of the original time (not 0.166667 s for 1/24000 s).

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.
Python version if appropriate: n/a (C++ API; the same function is exposed as otio.opentime.to_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 摘要。