【问题标题】:YouTube Reporting API .Net Client Library CreateTime MillisecondsYouTube Reporting API .Net 客户端库 CreateTime 毫秒
【发布时间】:2017-04-03 17:33:13
【问题描述】:

当返回为特定作业生成的报告列表时,API 会返回一个 createTime 属性。然后使用此属性值将未来的请求限制为仅包括在已收集的最大创建时间之后创建的报告。

问题是客户端库没有返回毫秒精度。相反,我得到一个仅包含秒数的字符串,例如“4/3/2017 11:25:25 AM”。

当我使用 API 资源管理器时,我在响应 json 中确实得到了毫秒。

问题是,客户端库可以返回毫秒吗?如果是这样,我如何获得这个值?

我所指的 Report 属性称为“CreateTime”。

var reportList = ytReportingService.Jobs.Reports.List(JobId);
reportList.CreatedAfter = "1900-01-01T12:00:00.000000Z";

foreach (Report report in reportListResponse.Reports)
{
//Some irrelevant code is removed
Console.WriteLine("================== Job Report ==================");
Console.WriteLine("Event: " + Event);
Console.WriteLine("ID: " + report.Id);
Console.WriteLine("DownloadUrl: " + report.DownloadUrl);
Console.WriteLine("CreateTime: " + report.CreateTime.ToString());
Console.WriteLine("StartTime: " + report.StartTime);
Console.WriteLine("EndTime: " + report.EndTime);
Console.WriteLine("JobID: " + report.JobId);
Console.WriteLine("JobExpireTime: " + report.JobExpireTime);
}

更新 20170406

CreateTime 作为通用对象从客户端库返回。令人惊讶的是,它允许获取和设置。

下面我将转换为日期时间数据,然后返回字符串并显式格式化。

((DateTime)job.CreateTime).ToString("yyyy-MM-dd HH:mm:ss.fff"))

结果是一样的。不返回小数秒。

((DateTime)job.CreateTime).ToString("yyyy-MM-dd HH:mm:ss.fff"))

更新 20170406

更正 - 上面的示例使用了 Job 对象的 CreateTime 属性。虽然作业不返回小数秒,但 report.CreateTime 确实返回小数秒!

事实证明,report.CreateTime (object) 在直接转换为字符串时不包括小数秒。解决方案是首先将其转换为数据时间数据类型。然后,可以根据需要对数据进行格式化。

((DateTime)report.CreateTime).ToString("yyyy-MM-dd HH:mm:ss.fff")

【问题讨论】:

  • 与你所说的相反,Youtube Reports properties 说 createTime 使用 RFC3339 UTC“Zulu”格式的时间戳,精确到微秒(例如:“2015-10-02T15:01:23.045678Z” .)。如果您能给我们一个原始的答复,那就太好了
  • 我同意。规范说我应该得到毫秒。我还验证了我在使用 API 资源管理器时确实得到了毫秒。问题是 .Net 客户端库没有返回毫秒。代码非常简单。我在原始帖子中添加了一个示例。
  • 您确定问题出在数据上,而不是您如何将其转换为字符串?例如,尝试Console.WriteLine("EndTime: " + report.EndTime.ToString("yyyy-MM-dd HH:mm:ss.fff"))。 (不过,我会调查的。)
  • 我很确定问题出在返回的数据上。 createtime 属性作为通用对象返回。要格式化它,我需要将它转换为日期时间。我在原始帖子中添加了注释,以演示格式化日期时间以包含秒数的结果。
  • 查看我的最新笔记,乔恩。你是对的。 report.CreateTime 正在返回小数秒!非常感谢您为我指明了正确的方向。如果您想发布答案,我很乐意标记它。

标签: youtube-api google-api-dotnet-client


【解决方案1】:

Jon Skeet 发表了一条评论,让答案。请参阅上述帖子中的最后一个示例。

【讨论】:

    猜你喜欢
    • 2016-08-19
    • 1970-01-01
    • 2022-10-25
    • 2013-10-15
    • 2017-11-04
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多