简体   繁体   English

用于解析人类可读时间跨度的 C# 库

[英]C# library to parse human readable time spans

Is there a library that exists that will parse human readable timespans into a .NET TimeSpan?是否存在将人类可读的时间跨度解析为 .NET 时间跨度的库?

I need something that will parse strings like:我需要一些可以解析字符串的东西:

30 days
1 week
5 hours

Does such a thing exist?这样的事情存在吗? Its probably not too hard to write myself, but if something is out there, it would be so much easier!自己写可能不太难,但如果有什么东西,那就容易多了!

I currently don't need functionality like "30 days, 3 hours, 5 minutes", just the one value, but it couldn't hurt!我目前不需要像“30 天、3 小时、5 分钟”这样的功能,只需要一个值,但它不会受到伤害!

I took a crack at this sometime back, I was playing with parsers as I knew nothing about them so don't expect quality.前段时间我对此进行了破解,我正在玩解析器,因为我对它们一无所知,所以不要指望质量。 But you can get the code here: http://timestamper.codeplex.com/但您可以在此处获取代码: http : //timestamper.codeplex.com/

I havent looked at it for a while, but im pretty sure it can do all the things that you mentioned above.我有一段时间没看它了,但我很确定它可以做你上面提到的所有事情。

Edit:编辑:

Just noted that you wanted a timespan object returned and not a date time.刚刚注意到您想要返回一个时间跨度对象而不是日期时间。 The code I provided will return a DateTime reletive to today ie我提供的代码将返回一个与今天相关的日期时间,即

"2 days" will be: DateTime.Now.AddDays(2); “2 天”将是:DateTime.Now.AddDays(2);

At least I think this is what is did, can quite be sure.至少我认为这是干的,可以肯定的。 If this is the case, you should just be able to get the timespan between the two DateTimes.如果是这种情况,您应该能够获得两个 DateTime 之间的时间跨度。

This might be long overdue, but Microsoft has recently released nuget packages and sources for its extractors and parsers that underpin LUIS (the language services in Azure).这可能早就应该了,但微软最近发布了支持 LUIS(Azure 中的语言服务)的提取器和解析器的 nuget 包和源代码。

You can find them here: GitHub Repo你可以在这里找到它们: GitHub Repo

My guess is that for most projects it would be like swatting a fly with a jackhammer, but luckily separate packages (such as one for DateTime) can be downloaded independently.我的猜测是,对于大多数项目来说,这就像用手提钻拍苍蝇一样,但幸运的是,可以独立下载单独的包(例如 DateTime 的包)。

The NuGet package that answers the question 回答问题的 NuGet 包

I am working on a library on the Dotnet framework (and dotnet core) primarily based on timestamper.我正在研究一个主要基于时间戳的 Dotnet 框架(和 dotnet 核心)库。

Install from Nuget: HumanDateParser从 Nuget 安装: HumanDateParser

Usage: (returns System.DateTime )用法:(返回System.DateTime

DateParser.Parse("1 month ago")
DateParser.Parse("after 15 days")
DateParser.Parse("15th feb 2010 at 5:30pm")

Well, I know that datejs the javascript code works really well for dates like that. 好吧,我知道datejs的javascript代码对于这样的日期确实非常有效。 You could look at it for inspiration for C# code. 您可以查看它以获取C#代码的灵感。 Not sure if that helps that much though; 不确定是否有太大帮助; but might be interesting. 但可能很有趣。

See www.datejs.com 见www.datejs.com

I know that you were looking for a library to do this but it seems the people that like this syntax are ruby developers and well they stick to Ruby. 我知道您正在寻找要执行此操作的库,但是喜欢这种语法的人似乎是ruby开发人员,他们很愿意使用Ruby。 If you really want to do this ,its pretty simple to do something similiar with .Net Extensions that is a bit more readable. 如果您确实想执行此操作,则与.Net Extensions进行类似的操作非常容易阅读,这很简单。 Have a look at this article http://haacked.com/archive/2007/05/24/ruby-like-syntax-in-c-3.0.aspx 看看这篇文章http://haacked.com/archive/2007/05/24/ruby-like-syntax-in-c-3.0.aspx

Also if you dont like the syntax in the article then all you need to do is create a parser that converts down to ticks , which can be used by the timespan constructor class. 另外,如果您不喜欢本文中的语法,那么您所要做的就是创建一个解析器,该解析器会转换为ticks,可以由timepan构造函数类使用。

It's not directly applicable, but my company has developed a .NET library that does this for dates. 它不是直接适用的,但是我公司已经开发了一个.NET 来执行此操作。 A counterpart that works for spans (more or less exactly what you are asking for) is planed for "when we get around to needing it". 针对跨度(或多或少完全符合您的要求)的工作人员计划在“我们需要时”进行计划。 If there is enough interest, that could become sooner. 如果有足够的兴趣,那可能会更快。

this is the available parsing ways for date and time: 这是日期和时间的可用解析方式:

Parsing Date and Time Strings 解析日期和时间字符串

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

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