.net NET中的STROTIME等价物
是否有一个PHP的.net NET中的STROTIME等价物,.net,string,parsing,datetime,strtotime,.net,String,Parsing,Datetime,Strtotime,是否有一个PHP的strotime()函数在.NET Framework上工作的等价物。我说的是它处理字符串的能力,比如: STROTIME(“现在””) 标准时间(“2000年9月10日”) STROTIME(“+1天”) STROTIME(“+1周”) STROTIME(“+1周2天4小时2秒”) STROTIME(下周四) STROTIME(上周一) 显然,DateTime.Parse()和Convert.ToDateTime()不会这样做 我发现的最接近的类是一个小类,它只处理以下几
strotime()
函数在.NET Framework上工作的等价物。我说的是它处理字符串的能力,比如:
- STROTIME(“现在””)
- 标准时间(“2000年9月10日”)
- STROTIME(“+1天”)
- STROTIME(“+1周”)
- STROTIME(“+1周2天4小时2秒”)
- STROTIME(下周四)
- STROTIME(上周一)
DateTime.Parse()
和Convert.ToDateTime()
不会这样做
我发现的最接近的类是一个小类,它只处理以下几个类:
编辑:我对C#编译时功能不感兴趣。问题是在运行时将人类相对日期/时间字符串转换为DateTime(即“now”-->DateTime.now等)。DateTime结构有几种方法和属性来获取所需内容:
DateTime.Now;
DateTime.Parse("10 September 2000");
DateTime.Now.AddDays(1);
DateTime.Now.AddDays(7);
DateTime.Now.AddDays(9).AddHours(4).AddSeconds(2);
// And so on
如果DateTime结构提供的功能还不够,我建议(由Jon Skeet)签出。我认为您必须编写自己的方法 “现在”只是
DateTime.now
例如,“+1天”将是DateTime.Now.AddDays(1)
因此,您需要解析字符串以查找这种类型的输入,然后调用相应的DateTime
方法。失败的情况是使用不同的DateTimeStyle传递字符串
DateTime date = new DateTime(10,10,2010)
Response.Write(date.ToShortDateTimeString());
Response.Write(date.Year);
date = DateTime.Now;
etc etc我认为最好的方法是将扩展方法写入DateTime,以满足您的需求
也许,它可以是OSS,因此社区可以帮助您实现它。由于目前还没有答案,我是根据给出的示例制作的。它支持除“最后一个星期四”(或一周中的其他几天)之外的大多数情况
//
///解析日期/时间字符串。
///
///可以处理相关的英文书写日期时间,如:
///-“-1天”:昨天
///-“+12周”:12周后的今天
///-“1秒”:从现在起1秒后。
///-“5天1小时前”
///-“1年2个月3周4天5小时6分7秒”
///-“今天”:今天午夜。
///-“现在”:现在(日期和时间)。
///-“下周”
///-“上个月”
/// - "2010-12-31"
///-“2010年1月1日下午1:59”
///-“23:59:58”:今天在给定的时间。
///
///如果相对时间包括小时、分钟或秒,则它是相对于现在的,
///否则它是相对于今天的。
///
内部类相对论
{
private const string ValidUnits=“年|月|周|日|小时|分钟|秒”;
///
///例:“去年”
///
private readonly Regex_basicRelativeRegex=new Regex(@“^(last | next)+”(“+ValidUnits+”)$”;
///
///例:“+1周”
///例:“1周”
///
private readonly Regex_simpleRelativeRegex=new Regex(@“^([+-]?\d+)*(“+ValidUnits+”)s?$”);
///
///例:“2分钟”
///例:“3个月5天1小时前”
///
private readonly Regex_completeRelativeRegex=new Regex(@“^(?**(\d)*)(“+ValidUnits+”)s?+(+ago)?$”;
公共日期时间分析(字符串输入)
{
//拆下外壳并修剪空间。
input=input.Trim().ToLower();
//试试常见的简单单词,比如“昨天”。
var结果=TryParseCommonDateTime(输入);
if(result.HasValue)
返回结果值;
//试试常见的简单单词,比如“上周”。
结果=TryParseLastOrNextCommonDateTime(输入);
if(result.HasValue)
返回结果值;
//尝试简单的格式,如“+1周”。
结果=TryParseSimpleRelativeDateTime(输入);
if(result.HasValue)
返回结果值;
//首先尝试完整的格式,如“1天2小时10分钟前”。
结果=TryParseCompleteRelativeDateTime(输入);
if(result.HasValue)
返回结果值;
//尝试解析固定日期,如“01/01/2000”。
返回DateTime.Parse(输入);
}
私有静态日期时间?TryParseCommonDateTime(字符串输入)
{
开关(输入)
{
案例“现在”:
返回日期时间。现在;
“今天”一案:
返回日期时间。今天;
案例“明天”:
返回日期时间。今天。添加天数(1);
“昨天”案:
返回日期时间。今天。添加天数(-1);
违约:
返回null;
}
}
private DateTime?TryParseLastOrNextCommonDateTime(字符串输入)
{
var match=_basicRelativeRegex.match(输入);
如果(!match.Success)
返回null;
var unit=match.Groups[2]。值;
var sign=string.Compare(match.Groups[1]。值“next”,true)==0?1:-1;
返回AddOffset(单位、符号);
}
private DateTime?TryParseSimpleRelativeDateTime(字符串输入)
{
var match=_simpleRelativeRegex.match(输入);
如果(!match.Success)
返回null;
var delta=Convert.ToInt32(match.Groups[1].Value);
var unit=match.Groups[2]。值;
返回AddOffset(单位,增量);
}
private DateTime?TryParseCompleteRelativeDateTime(字符串输入)
{
var match=_completeRelativeRegex.match(输入);
如果(!match.Success)
返回null;
var values=match.Groups[1]。捕获;
var units=match.Groups[2]。捕获;
变量符号=匹配。组[3]。成功?-1:1;
Assert(values.Count==units.Count);
var dateTime=UnitIncludeTime(单位)?dateTime.Now:dateTime.Today;
对于(int i=0;i/// <summary>
/// Parse a date/time string.
///
/// Can handle relative English-written date times like:
/// - "-1 day": Yesterday
/// - "+12 weeks": Today twelve weeks later
/// - "1 seconds": One second later from now.
/// - "5 days 1 hour ago"
/// - "1 year 2 months 3 weeks 4 days 5 hours 6 minutes 7 seconds"
/// - "today": This day at midnight.
/// - "now": Right now (date and time).
/// - "next week"
/// - "last month"
/// - "2010-12-31"
/// - "01/01/2010 1:59 PM"
/// - "23:59:58": Today at the given time.
///
/// If the relative time includes hours, minutes or seconds, it's relative to now,
/// else it's relative to today.
/// </summary>
internal class RelativeDateParser
{
private const string ValidUnits = "year|month|week|day|hour|minute|second";
/// <summary>
/// Ex: "last year"
/// </summary>
private readonly Regex _basicRelativeRegex = new Regex(@"^(last|next) +(" + ValidUnits + ")$");
/// <summary>
/// Ex: "+1 week"
/// Ex: " 1week"
/// </summary>
private readonly Regex _simpleRelativeRegex = new Regex(@"^([+-]?\d+) *(" + ValidUnits + ")s?$");
/// <summary>
/// Ex: "2 minutes"
/// Ex: "3 months 5 days 1 hour ago"
/// </summary>
private readonly Regex _completeRelativeRegex = new Regex(@"^(?: *(\d) *(" + ValidUnits + ")s?)+( +ago)?$");
public DateTime Parse(string input)
{
// Remove the case and trim spaces.
input = input.Trim().ToLower();
// Try common simple words like "yesterday".
var result = TryParseCommonDateTime(input);
if (result.HasValue)
return result.Value;
// Try common simple words like "last week".
result = TryParseLastOrNextCommonDateTime(input);
if (result.HasValue)
return result.Value;
// Try simple format like "+1 week".
result = TryParseSimpleRelativeDateTime(input);
if (result.HasValue)
return result.Value;
// Try first the full format like "1 day 2 hours 10 minutes ago".
result = TryParseCompleteRelativeDateTime(input);
if (result.HasValue)
return result.Value;
// Try parse fixed dates like "01/01/2000".
return DateTime.Parse(input);
}
private static DateTime? TryParseCommonDateTime(string input)
{
switch (input)
{
case "now":
return DateTime.Now;
case "today":
return DateTime.Today;
case "tomorrow":
return DateTime.Today.AddDays(1);
case "yesterday":
return DateTime.Today.AddDays(-1);
default:
return null;
}
}
private DateTime? TryParseLastOrNextCommonDateTime(string input)
{
var match = _basicRelativeRegex.Match(input);
if (!match.Success)
return null;
var unit = match.Groups[2].Value;
var sign = string.Compare(match.Groups[1].Value, "next", true) == 0 ? 1 : -1;
return AddOffset(unit, sign);
}
private DateTime? TryParseSimpleRelativeDateTime(string input)
{
var match = _simpleRelativeRegex.Match(input);
if (!match.Success)
return null;
var delta = Convert.ToInt32(match.Groups[1].Value);
var unit = match.Groups[2].Value;
return AddOffset(unit, delta);
}
private DateTime? TryParseCompleteRelativeDateTime(string input)
{
var match = _completeRelativeRegex.Match(input);
if (!match.Success)
return null;
var values = match.Groups[1].Captures;
var units = match.Groups[2].Captures;
var sign = match.Groups[3].Success ? -1 : 1;
Debug.Assert(values.Count == units.Count);
var dateTime = UnitIncludeTime(units) ? DateTime.Now : DateTime.Today;
for (int i = 0; i < values.Count; ++i)
{
var value = sign*Convert.ToInt32(values[i].Value);
var unit = units[i].Value;
dateTime = AddOffset(unit, value, dateTime);
}
return dateTime;
}
/// <summary>
/// Add/Remove years/days/hours... to a datetime.
/// </summary>
/// <param name="unit">Must be one of ValidUnits</param>
/// <param name="value">Value in given unit to add to the datetime</param>
/// <param name="dateTime">Relative datetime</param>
/// <returns>Relative datetime</returns>
private static DateTime AddOffset(string unit, int value, DateTime dateTime)
{
switch (unit)
{
case "year":
return dateTime.AddYears(value);
case "month":
return dateTime.AddMonths(value);
case "week":
return dateTime.AddDays(value * 7);
case "day":
return dateTime.AddDays(value);
case "hour":
return dateTime.AddHours(value);
case "minute":
return dateTime.AddMinutes(value);
case "second":
return dateTime.AddSeconds(value);
default:
throw new Exception("Internal error: Unhandled relative date/time case.");
}
}
/// <summary>
/// Add/Remove years/days/hours... relative to today or now.
/// </summary>
/// <param name="unit">Must be one of ValidUnits</param>
/// <param name="value">Value in given unit to add to the datetime</param>
/// <returns>Relative datetime</returns>
private static DateTime AddOffset(string unit, int value)
{
var now = UnitIncludesTime(unit) ? DateTime.Now : DateTime.Today;
return AddOffset(unit, value, now);
}
private static bool UnitIncludeTime(CaptureCollection units)
{
foreach (Capture unit in units)
if (UnitIncludesTime(unit.Value))
return true;
return false;
}
private static bool UnitIncludesTime(string unit)
{
switch (unit)
{
case "hour":
case "minute":
case "second":
return true;
default:
return false;
}
}
}