在 C# 中解析相对日期的库(如谷歌日历可以)

发布于 2024-07-29 06:15:49 字数 1542 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧 2024-08-05 06:15:49
using System;
using System.Text.RegularExpressions;

class RelativeDateParser
    {
        
        public static DateTime Parse(string input)
        {
            DateTime dt = DateTime.Now;

            // parse "x days x hours x minutes x seconds" or "x days x hours x minutes x seconds ago"
            Regex r = new Regex("(day)|(hour)|(minues)|(second)");
            if (r.Match(input).Success == true)
            {
                dt = ParseDHMS(input, dt);
            }

            // parse "yesterday" or "today" or "tomorrow" or "eow" or "eod"
            r = new Regex("(today)|(tomorrow)|(eow)|(eod)");
            if (r.Match(input).Success == true)
            {
                dt = ParseGenericRelative(input);
            }

            Console.WriteLine("Now DateTime: " + DateTime.Now.ToString());
            Console.WriteLine("New DateTime: " + dt.ToString());
            return dt;
        }

        private static DateTime ParseGenericRelative(string input)
        {
            throw new NotImplementedException("Not implemented");
        }
        
        private static DateTime ParseDHMS(string input, DateTime seedDtm)
        {
            TimeSpan timeSpan = new TimeSpan(0);
            
            // search for days
            timeSpan += TimeSpan.FromDays(getMetricValue(input, "day"));
            // search for hours
            timeSpan += TimeSpan.FromHours(getMetricValue(input, "hour"));
            // search for minutes
            timeSpan += TimeSpan.FromMinutes(getMetricValue(input, "minutes"));
            // search for seconds
            timeSpan += TimeSpan.FromSeconds(getMetricValue(input, "second"));
            
            return seedDtm.AddTicks(timeSpan.Ticks * (int)(input.EndsWith("ago") ? -1 : 1));
        }
        
        private static double getMetricValue(string input, string metric)
        {
            Regex r = new Regex(@"(\d+)\s*" + metric);
            Match m = r.Match(input);
            if (m.Success)
            {
                string match = m.Groups[1].Value;
                return Double.Parse(match);
            }
            
            return 0;
        }
    }

原始解决方案的 Wayback Machine 副本
注意:此实现尚未经过测试

using System;
using System.Text.RegularExpressions;

class RelativeDateParser
    {
        
        public static DateTime Parse(string input)
        {
            DateTime dt = DateTime.Now;

            // parse "x days x hours x minutes x seconds" or "x days x hours x minutes x seconds ago"
            Regex r = new Regex("(day)|(hour)|(minues)|(second)");
            if (r.Match(input).Success == true)
            {
                dt = ParseDHMS(input, dt);
            }

            // parse "yesterday" or "today" or "tomorrow" or "eow" or "eod"
            r = new Regex("(today)|(tomorrow)|(eow)|(eod)");
            if (r.Match(input).Success == true)
            {
                dt = ParseGenericRelative(input);
            }

            Console.WriteLine("Now DateTime: " + DateTime.Now.ToString());
            Console.WriteLine("New DateTime: " + dt.ToString());
            return dt;
        }

        private static DateTime ParseGenericRelative(string input)
        {
            throw new NotImplementedException("Not implemented");
        }
        
        private static DateTime ParseDHMS(string input, DateTime seedDtm)
        {
            TimeSpan timeSpan = new TimeSpan(0);
            
            // search for days
            timeSpan += TimeSpan.FromDays(getMetricValue(input, "day"));
            // search for hours
            timeSpan += TimeSpan.FromHours(getMetricValue(input, "hour"));
            // search for minutes
            timeSpan += TimeSpan.FromMinutes(getMetricValue(input, "minutes"));
            // search for seconds
            timeSpan += TimeSpan.FromSeconds(getMetricValue(input, "second"));
            
            return seedDtm.AddTicks(timeSpan.Ticks * (int)(input.EndsWith("ago") ? -1 : 1));
        }
        
        private static double getMetricValue(string input, string metric)
        {
            Regex r = new Regex(@"(\d+)\s*" + metric);
            Match m = r.Match(input);
            if (m.Success)
            {
                string match = m.Groups[1].Value;
                return Double.Parse(match);
            }
            
            return 0;
        }
    }

Wayback Machine copy of original solution
Note: this implementation hasn't been tested

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文