在 vscode-lldb 中调试 chrono::DateTime 时如何查看用户友好的格式?

How do I see a user-friendly format when debugging chrono::DateTime in vscode-lldb?

调试器显示 DateTime 结构:

在查看 lldb 的文档以了解如何格式化后,我发现我可以将整数格式化为十六进制:

type format add -f hex i32

我为 DateTime

尝试了类似的方法
type format add --format hex chrono::datetime::DateTime<chrono::offset::utc::Utc>

它没有选择 chrono 类型,即使 frame variable 将其显示为类型。即使完成此步骤后,也不确定如何将日期解析为字符串。

lldb 对给定类型的打印值进行了三个自定义阶段。最简单的是“类型格式”,它指定用于显示标量实体的格式(十六进制、字符等)。第二个是“type summary”,它允许您打印值的单行字符串摘要。第三个是“类型合成”,它允许您将值呈现为某种结构化对象(例如,使 std::vector 看起来像一个值向量而不是其内部形式。)

您想为此类型添加一个 "type summary" 来解析数据并将其打印为人类可读的字符串。

在另一个答案中使用@Jim Ingham 提示并对此进行修改formula

  1. 通过添加
  2. 修改launch.json文件
"initCommands": [
    "command source '${workspaceFolder}/my_type_formatters'"
]
  1. 将以下名为 my_type_formatters 的文件添加到项目的根目录。
command script import simd.py 

type summary add -F simd.GetSummary chrono::datetime::DateTime<chrono::offset::utc::Utc>
  1. simd.py 文件添加到项目的根目录:
from datetime import datetime,timedelta


def GetSummary(valobj, internal_dict):
    ymdf = valobj.GetChildMemberWithName('datetime').GetChildMemberWithName('date').GetChildMemberWithName('ymdf').GetValue();
    secs = valobj.GetChildMemberWithName('datetime').GetChildMemberWithName('time').GetChildMemberWithName('secs').GetValue();
    year = int(ymdf) >> 13  
    day_of_year = (int(ymdf) & 8191) >> 4;
    date = datetime(year -1, 12, 31) + timedelta(days=day_of_year) + timedelta(seconds=int(secs))
    return date.strftime('%Y-%m-%d %H:%M:%S')

结果: