简体   繁体   English

如何解析相对时间?

[英]How to parse relative time?

This question is the other side of the question asking, " How do I calculate relative time? ".这个问题是问题的另一面,“ 我如何计算相对时间? ”。

Given some human input for a relative time, how can you parse it?鉴于相对时间的一些人工输入,您如何解析它? By default you would offset from DateTime.Now() , but could optionally offset from another DateTime .默认情况下,您将从DateTime.Now()偏移,但可以选择从另一个DateTime偏移。

(Prefer answers in C#) (首选 C# 中的答案)

Example input:示例输入:

  • "in 20 minutes" “在 20 分钟内”
  • "5 hours ago" “5小时前”
  • "3h 2m" “3 小时 2 米”
  • "next week" “下周”

Edit: Let's suppose we can define some limits on the input.编辑:假设我们可以对输入定义一些限制。 This sort of code would be a useful thing to have out on the web.这种代码在网络上会很有用。

A Google search turns up the parsedatetime library (associated with the Chandler project ), which is designed to do exactly this. Google 搜索出现了parsedatetime库(与Chandler 项目相关联),它的设计正是为了做到这一点。 It's open source (Apache License) and written in Python.它是开源的(Apache 许可)并用 Python 编写。 It seems to be quite sophisticated -- from the homepage:它似乎相当复杂——来自主页:

parsedatetime is able to parse, for example, the following: parsedatetime 能够解析,例如,以下内容:

 * Aug 25 5pm * 5pm August 25 * next saturday ... * tomorrow * next thursday at 4pm * at 4pm * eod * in 5 minutes * 5 minutes from now * 5 hours before now * 2 days from tomorrow

Since it's implemented in pure Python and doesn't use anything fancy, there's a good chance it's compatible with IronPython , so you could use it with .net.由于它是用纯 Python 实现的并且没有使用任何花哨的东西,因此它很有可能与IronPython兼容,因此您可以将它与 .net 一起使用。 If you want specifically a C# solution, you could write something based on the algorithms they use...如果你特别想要一个 C# 解决方案,你可以根据他们使用的算法编写一些东西......

It also comes with a whole bunch of unit tests.它还带有一大堆单元测试。

That's building a DSL (Domain specific language) for date handling.那就是为日期处理构建 DSL(域特定语言)。 I don't know if somebody has done one for .NET but the construction of a DSL is fairly straightforward:我不知道是否有人为 .NET 做过一个,但 DSL 的构建相当简单:

  1. Define the language precisely, which input forms you will accept and what will you do with ambiguities准确定义语言,您将接受哪些输入形式以及您将如何处理歧义
  2. Construct the grammar for the language构建语言的语法
  3. Build the finite state machine that parses your language into an actionable AST构建有限状态机,将您的语言解析为可操作的 AST

You can do all that by yourself (with the help of the Dragon Book , for instance) or with the help of tools to the effect, as shown in this link .您可以自己完成所有这些(例如在龙之书的帮助下)或借助工具来实现,如此链接中所示。

Just by thinking hard about the possibilities you have a good chance, with the help of good UI examples, of covering more than half of the actual inputs your application will receive.只需仔细考虑各种可能性,借助优秀的 UI 示例,您就有很好的机会覆盖应用程序将收到的实际输入的一半以上。 If you aim to accept everything a human could possibly type, you can record the input determined as ambiguous and then add them to the grammar, whenever they can be interpreted, as there are things that will be inherently ambiguous.如果您的目标是接受人类可能输入的所有内容,您可以记录确定为不明确的输入,然后将它们添加到语法中,只要它们可以被解释,因为有些东西本质上是不明确的。

The ruby folks have attempted to tackle this with a parser called Chronic . ruby 人员试图用一个叫做Chronic的解析器来解决这个问题。

I watched an informative video presentation recently on how the author went about solving this problem.我最近观看了有关作者如何解决这个问题的内容丰富的视频演示。

这可能不是那么有用,因为你在谈论 c# 但因为没有人提到它,你可以尝试看看 php 优秀且完全疯狂的原生strtotime函数

This: http://www.codeproject.com/KB/edit/dateparser.aspx Is fairly close to what you are trying to accomplish.这: http : //www.codeproject.com/KB/edit/dateparser.aspx与您要完成的任务非常接近。 Not the most elegant solution, but certainly might save you some work.不是最优雅的解决方案,但肯定可以为您节省一些工作。

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

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