将DateTime字符串以宽恕的方式解析为Noda Time LocalDateTime? [英] Parse DateTime string into Noda Time LocalDateTime in a forgiving way?

查看:176
本文介绍了将DateTime字符串以宽恕的方式解析为Noda Time LocalDateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有GetDateTimeOffset(字符串纬度,字符串经度,字符串dateTime)Web服务,它确定时间偏移给定Lat / Long和本地DateTime。



我们当前的客户端网页使用DateTimePicker插件 http://trentrichardson.com/examples/timepicker/ 。我们使用默认日期格式和格式时间部分为h:mm:ss TT Z,所以我们传递给服务器的字符串看起来像'01 / 22/2014 12:09:00 AM -05:00'。但是我正在考虑使我们的Web服务更加通用,所以应该原谅一个传递的dateTime字符串格式。



现在我正在解析DateTime字符串(用户输入)使用BCL http://goo.gl/s9Kypx 以次优的方式。

  var tmpDateTime = new DateTimeOffset(DateTime.Now).DateTime; 
if(!String.IsNullOrEmpty(dateTime))
{
try
{
//注意:看起来很愚蠢?我需要丢弃在dateTime字符串(如果有的话)中指定的TimeZone Offset。
//有趣的是,调用DateTime.Parse(dateTime)会在系统时区中自动修改DateTime。
tmpDateTime = DateTimeOffset.Parse(dateTime).DateTime;
}

catch(异常){}
}

问题:




  • a)上述代码是否以灵活的宽恕方式解析为DateTime的用户输入的正确BCL方法? b / b> b)b)将dateTime字符串解析成LocalDateTime(Noda Time class)的好方法和宽恕方式是什么?



我想我应该使用




  • http://goo.gl/a2wYco 获取系统的LocalDateTime在Noda Time

  • LocalDateTimePattern.Parse(String),可能是由Jon Skeet这里 http://goo.gl/F8k51c 可以解析各种格式的功能。但是要使用哪种模式使其变得非常灵活?


解决方案

如果需要解析该特定模式,那么可以使用 DateTimeOffset.TryParseExact 或使用NodaTime代码从该输入中解析:

  var defaultValue = new OffsetDateTime(new LocalDateTime(2000,1,0,0),Offset.Zero); 
var pattern = OffsetDateTimePattern.Create(MM / dd / yyyy hh:mm:ss tt o< m>,CultureInfo.InvariantCulture,defaultValue);
var result = pattern.Parse(01/22/2014 12:09:00 AM -05:00);
if(!result.Success)
{
//处理你的错误
}

OffsetDateTime value = result.Value;
LocalDateTime local = value.LocalDateTime;

但是回到你的陈述中:


但是我正在考虑使我们的Web服务更加通用,所以应该原谅一种传入的dateTime字符串的格式。


这不是个好主意。如果您正在创建一个 Web服务,那么您应该非常清楚您所使用的格式。最好不应该是您在这里显示的格式,而应该是ISO-8601扩展格式,如 2014-01-22T12:09:00-05:00


I have GetDateTimeOffset(string latitude, string longitude, string dateTime) web service which determines Time Offset given Lat/Long and local DateTime.

Our current client web page uses DateTimePicker plugin http://trentrichardson.com/examples/timepicker/. We use the default date formatting and format time part as 'h:mm:ss TT Z' so a string we pass to a server looks like '01/22/2014 12:09:00 AM -05:00'. But I'm thinking about making our web service more generic, so it should be forgiving for a format of dateTime string passed in.

Right now I'm parsing DateTime string (user input) in an sub-optimal way using BCL http://goo.gl/s9Kypx.

var tmpDateTime = new DateTimeOffset(DateTime.Now).DateTime;
if (!String.IsNullOrEmpty(dateTime))
{
    try
    {
        // Note: Looks stupid? I need to throw away TimeZone Offset specified in dateTime string (if any).
        // Funny thing is that calling DateTime.Parse(dateTime) would automatically modify DateTime for its value in a system timezone.
        tmpDateTime = DateTimeOffset.Parse(dateTime).DateTime;
    }

    catch (Exception) { }
}

Questions:

  • a) Is the above code a proper BCL method of parsing user input into DateTime in flexible "forgiving" way?
  • b) What would be a good and "forgiving" way of parsing dateTime string into LocalDateTime (Noda Time class)?

I suppose I should use

  • http://goo.gl/a2wYco for getting the system's LocalDateTime in Noda Time
  • LocalDateTimePattern.Parse(String) and probably the approach described by Jon Skeet here http://goo.gl/F8k51c for ability to parse various formats. But which patterns to use to make it really flexible?

解决方案

If you need to parse that specific pattern, then you can do that DateTimeOffset.TryParseExact, or with NodaTime code to parse from that input:

var defaultValue = new OffsetDateTime(new LocalDateTime(2000, 1, 1, 0, 0), Offset.Zero);
var pattern = OffsetDateTimePattern.Create("MM/dd/yyyy hh:mm:ss tt o<m>", CultureInfo.InvariantCulture, defaultValue);
var result = pattern.Parse("01/22/2014 12:09:00 AM -05:00");
if (!result.Success)
{
    // handle your error
}

OffsetDateTime value = result.Value;
LocalDateTime local = value.LocalDateTime;

But going back to your statement:

But I'm thinking about making our web service more generic, so it should be forgiving for a format of dateTime string passed in.

That is not a good idea. If you are creating a web service, you should be very explicit about the format that you are using. Preferably, it should not be this format that you showed here, but instead should be the ISO-8601 extended format such as 2014-01-22T12:09:00-05:00

这篇关于将DateTime字符串以宽恕的方式解析为Noda Time LocalDateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆