简体   繁体   English

用于解析人类可读算术计算的日期库

[英]Date library for parsing human readable arithmetic calculations

I am writing a QA test automation program where people are going to input human readable duration strings with arithmetic operations with values like now - 5d , yesterday , now + 8 days , and I need parse them into java 8 LocalDateTime instances.我正在编写一个 QA 测试自动化程序,其中人们将使用算术运算输入人类可读的持续时间字符串,其值如now - 5dyesterdaynow + 8 days ,我需要将它们解析为 java 8 LocalDateTime实例。

So simply stated, I need a utility like this:简单地说,我需要一个这样的实用程序:

LocalDateTime nowMinus5Days = DurationUtil.parseHumanReadableDuration("now - 5d")

How can I code this in Java?我如何在 Java 中编写此代码? Or is there some library that already exists that I should use instead of trying to reinvent the wheel?或者是否有一些已经存在的库我应该使用而不是试图重新发明轮子?

Here's a starting point.这是一个起点。 I am guessing that you will want to elaborate it further.我猜你会想进一步阐述它。

private static Pattern relativeTimePattern
        = Pattern.compile("(\\w+)\\s*(?:([+-])\\s*(\\w+))?");
private static Map<String, Supplier<LocalDateTime>> bases
        = Map.of("now", () -> LocalDateTime.now(),
                "yesterday", () -> LocalDate.now().minusDays(1).atStartOfDay()); 

public static LocalDateTime parseRelativeTime(String timeString) {
    Matcher m = relativeTimePattern.matcher(timeString);
    if (m.matches()) {
        String baseString = m.group(1);
        LocalDateTime result = bases.get(baseString).get();
        String signString = m.group(2);
        if (signString != null) {
            boolean subtract = signString.equals("-");
            String diffString = m.group(3);
            TemporalAmount diff;
            try {
                diff = Period.parse("P" + diffString);
            } catch (DateTimeParseException dtpe) {
                // try a Duration instead
                diff = Duration.parse("PT" + diffString);
            }
            if (subtract) {
                result = result.minus(diff);
            } else {
                result = result.plus(diff);
            }
        }
        return result;
    } else {
        throw new IllegalArgumentException();
    }
}

Let's try it out:让我们试试看:

    System.out.println(parseRelativeTime("now - 5d"));
    System.out.println(parseRelativeTime("yesterday"));
    System.out.println(parseRelativeTime("now + 8d"));

Output when I ran just now:刚才跑的时候输出:

 2020-03-30T09:49:18.300731 2020-04-03T00:00 2020-04-12T09:49:18.307784

As my method stands, it accepts either of now and yesterday in lower case optionally followed by a sign ( + or - ) and either a period of years-months-weeks-days or a duration of hours-minutes-seconds.由于我的方法表示,它接受的要么nowyesterday小写后面可以跟一个符号( +-的年,月,周,日和句点小时-分-秒的持续时间。 Each of the latter must use one letter abbreviations for the time units (y, m, w, d, h, m, s; when the ambiguous m comes alone, it is taken as months).后面的每一个都必须使用一个字母缩写作为时间单位(y, m, w, d, h, m, s;当有歧义的 m 单独出现时,以月为单位)。 No spaces are allowed inside the period or duration.期间或持续时间内不允许有空格。

Thinkable further developments include:可以想到的进一步发展包括:

  • Add more words: today , tomorrow ;添加更多词: todaytomorrow consider allowing upper case and mixed case.考虑允许大写和混合大小写。
  • Allow units in full and with space, for example 8 days .允许单位完整且有空间,例如8 days A further regex will convert this to 8d before the final parsing into a Period .进一步的正则表达式将在最终解析为Period之前将其转换为8d
  • Disallow the ambiguous 2m forcing the user to specify for example 2m0d (2 months 0 days) or 2m0s (2 minutes 0 seconds).禁止模棱两可的2m强制用户指定例如2m0d (2 个月 0 天)或2m0s (2 分 0 秒)。 Or force uppercase M for months and lowercase m for minutes.或者强制大写M几个月,小写m分钟。
  • The complicated part: provide useful error messages for strings that cannot be parsed.复杂的部分:为无法解析的字符串提供有用的错误消息。

I think it is pretty much impossible (or at least hard) to cover conversion of every possible String input to a valid date.我认为几乎不可能(或至少很难)将每个可能的 String 输入转换为有效日期。 It could even need some intelligent Natural Language Processing.它甚至可能需要一些智能的自然语言处理。

I would suggest using a set of fields exposed for input and convert them to a desired object.我建议使用一组公开用于输入的字段并将它们转换为所需的对象。 If there is an end user, you can have a user select whether they want to add or subtract date and then present a Text Input where they can enter the number of days.如果有最终用户,您可以让用户选择他们是要添加还是减去日期,然后显示一个文本输入,他们可以在其中输入天数。 You can handle inputs like "Yesterday" and "Tomorrow" as special inputs.您可以将“昨天”和“明天”等输入作为特殊输入处理。

Java's Date Package is pretty useful and has methods for adding and subtracting a given number of days. Java的日期套餐是非常有用的,有方法的加和减天的给定数。

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

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