简体   繁体   English

如何在C#中解析Kusto时间跨度字符串?

[英]How can I parse Kusto timespan strings in C#?

I tried TimeSpan.Parse("2d") , for example, but that doesn't work. 例如,我尝试了TimeSpan.Parse("2d") ,但这不起作用。

The format of Kusto timespan doesn't seem to be supported by any of the TimeSpan.Parse() flavors. 任何TimeSpan.Parse() 样式似乎都不支持Kusto时间跨度的格式。

The Kusto Data Client SDK for .NET (in the Microsoft.Azure.Kusto.Data nuget package) has a CslTimeSpanLiteral class that understands this format. 用于.NET的Kusto数据客户端SDK(位于Microsoft.Azure.Kusto.Data nuget程序包中)具有CslTimeSpanLiteral类,该类可以理解此格式。 It contains several static methods for parsing strings to .NET's TimeSpan structure. 它包含几种用于将字符串解析为.NET的TimeSpan结构的静态方法。

For example: 例如:

using Kusto.Data.Common;

...

TimeSpan? ts = CslTimeSpanLiteral.Parse("2d");

In addition to Parse , there is also ParseNoNull , TryParse , and TryParseNoNull . Parse ,还有ParseNoNullTryParseTryParseNoNull

TimeSpan.Parse in c# does not recognize the Kusto timespan strings like 2d,2h etc. C#中的TimeSpan.Parse无法识别Kusto时间跨度字符串,例如2d,2h等。

We don't know your purpose on parsing it, but you can use some code like below: 我们不知道您解析它的目的,但是您可以使用如下代码:

        string mytime = "2d";

        if (mytime.EndsWith("d"))
        {
            mytime = mytime.Remove(mytime.IndexOf('d'));
            var dt = DateTime.Now.AddDays(Convert.ToDouble(mytime));
            Console.WriteLine(dt.ToString());
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM