2016-03-19 11 views
1

RFC1123-dateformatをDateTime-Objectに変換します。逆も同様です。 DateTime-ObjectからRFC-date-stringは完全に機能しますが、ドイツに住んでいるので(MEZ- Timezone)間違った結果が出ます。datetime-formatをRFC1123からDateTime-Objectに変換します。

ので、一度、ここに変換するための私のクラスである:

public interface IRFCDate 
{ 
    DateTime ToDateTime(); 
} 

public class RFCDate : IRFCDate 
{ 
    private string dateString { get; set; } = null; 
    private DateTime? dateObject { get; set; } = null; 

    public DateTime ToDateTime() 
    { 
     if (dateObject.HasValue) return dateObject.Value; 

     string regexPattern = @"[a-zA-Z]+, [0-9]+ [a-zA-Z]+ [0-9]+ [0-9]+:[0-9]+:[0-9]+ (?<timezone>[a-zA-Z]+)"; 
     Regex findTimezone = new Regex(regexPattern, RegexOptions.Compiled); 

     string timezone = findTimezone.Match(dateString).Result("${timezone}"); 
     string format = $"ddd, dd MMM yyyy HH:mm:ss {timezone}"; 

     dateObject = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture); 
     return dateObject.Value; 
    } 
    public IRFCDate From(IConvertible value) 
    { 
     if (value is string) 
      dateString = value.ToString(); 
     else if (value is DateTime) 
      dateObject = (DateTime)value; 
     else 
      throw new NotSupportedException($"Parametertype has to be either string or DateTime. '{value.GetType()}' is unsupported."); 
     return this; 
    } 
} 

私のxUnit-テストケースは次のようになります。この場合は

[Fact] 
public void StringToDateTime() 
{ 
    DateTime expectedValue = new DateTime(2001, 1, 1); 
    string RFCDatestring = "Mon, 01 Jan 2001 00:00:00 GMT"; 
    DateTime actualValue = RFCDatestring.To<DateTime>(); 
    Assert.Equal(expectedValue, actualValue); 
} 

にはそう

return new RFCDate().From(@this).ToDateTime(); 

呼び出しますテストケース実行時の結果は次のとおりです。

Assert.Equal期待

()障害:2001-01-01T00:00:00.0000000

実際:2001-01-01T01:00:00.0000000 + 01:00

誰かがこれを修正する方法を知っていますか?実際の値は1時ではなく00時です。

答えて

0

私は間違いを見ました:私はCET(またはGMT + 1)であるドイツにいるのでの代わりにGMTのタイムゾーンを設定する必要があります。 したがって、関数は正しいです。

関連する問題