我正在开发的应用程序中有一个 Google 日历集成。RRULE字符串用于描述其重复行为的重复事件。
(例如 RRULE:FREQ=DAILY;WKST=SU;UNTIL=20211215T215959Z;INTERVAL=3)
我需要验证DateTime应用程序中的一些 s,因此它们不会与这些事件相交。就复杂性而言,最有效的方法是什么?
1 回答
1
如果该 RRULE 的格式是恒定的,只需:
创建一个
Dictionary<string, string>用“;”分割它 获得不同部分的角色
循环到这些部分
在循环中,使用“=”字符拆分部分以获得 rrule 名称和 rrule 值。
实际的代码应该很简单。
// NOTE: This rules parsing code could/should make it into a separate function, this is just some quick coding
string[] rulesParts = myVariable.Substring(myVariable.Indexof(':') + 1).Split(';');
Dictionary<string, string> rrules = new Dictionary<string, string>();
foreach(string rulePart in rulesParts)
{
string[] nameAndValue = rulePart.Split(';');
string ruleName = nameAndValue[0];
string ruleValue = nameAndValue[1];
if (!rulesParts.ContainsKey(ruleName))
{
rrules.Add(ruleName, ruleValue);
}
}
if (rrules.ContainsKey("UNTIL"))
{
// Do a DateTime.TryParseExact of rrules["UNTIL"] and work with your DateTime variable as you like
}
于 2021-11-12T16:48:16.383 回答