FileHelpers を使用して CSV ファイルに書き出した記録があります。構造体の DateTime フィールドを UTC 日付として書き出す必要があります。現在、次のフォーマッタがあります。
[FieldConverter(ConverterKind.Date, "yyyy-MM-dd")]
UTC日付を出力するにはどうすればよいですか?
FileHelpers を使用して CSV ファイルに書き出した記録があります。構造体の DateTime フィールドを UTC 日付として書き出す必要があります。現在、次のフォーマッタがあります。
[FieldConverter(ConverterKind.Date, "yyyy-MM-dd")]
UTC日付を出力するにはどうすればよいですか?
ドキュメントは言う:
サポートされているすべてのフォーマット文字列を確認できます。DateTime.ParseExact の MSDN ドキュメントを確認してください。
によると:http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
[FieldConverter(ConverterKind.Date, "u")]
"u" => "yyyy'-'MM'-'dd HH':'mm':'ss'Z'" 日付を utc に変換せず、フォーマットするだけです
変換するには DateTime.ToUniversalTime が必要です。
編集 次のようなものがある場合:
[FieldConverter(ConverterKind.Date, "ddMMyyyy" )]
public DateTime ShippedDate;
次に、一時 ShippedDateUTC を追加します。
public DateTime ShippedDate;
[FieldConverter(ConverterKind.Date, "ddMMyyyy" )]
public DateTime ShippedDateUTC {
get{ return ShippedDate.ToUniversalTime();}
}
プライベート フィールドに適切な値を割り当てるパブリック セッターを使用して変換をラップできます。例えば:
public class OutputRecord
{
[FieldConverter(ConverterKind.Date, "ddMMyyyy" )]
private DateTime dateInUtc:
public void SetDate(DateTime date)
{
dateInUtc = date.ToUniversalTime();
}
}
カスタム コンバーターhttp://www.filehelpers.com/example_customconv.htmlを使用することもできます。
DateTime クラスのToUniversalTime()メソッドを使用する
したがって、ConverterKind.Date
が DateTime 型の場合、コードは次のようになります。
[FieldConverter(ConverterKind.Date.ToUniversalTime(), "yyyy-MM-dd")]