Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.ComponentModel;
11 : using System.Globalization;
12 :
13 : namespace Cqrs.WebApi.Formatters.FormMultipart.Infrastructure.TypeConverters
14 : {
15 : /// <summary>
16 : /// Convert <see cref="DateTime"/> to ISO 8601 format string
17 : /// </summary>
18 : internal class DateTimeConverterIso8601 : DateTimeConverter
19 : {
20 : /// <summary>
21 : /// Converts the given <paramref name="value"/> object to a <see cref="DateTime"/> using the arguments.
22 : /// </summary>
23 : /// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
24 : /// <param name="culture">A optional <see cref="CultureInfo"/>. If not supplied, the current culture is assumed.</param>
25 : /// <param name="value">The <see cref="object"/> to convert.</param>
26 : /// <param name="destinationType">The <see cref="Type"/> to convert the <paramref name="value"/> to.</param>
27 : /// <returns>An <see cref="object"/> that represents the converted <paramref name="value"/>.</returns>
28 1 : public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
29 : {
30 : if (value != null && value is DateTime && destinationType == typeof (string))
31 : {
32 : return ((DateTime)value).ToString("O"); // ISO 8601
33 : }
34 : return base.ConvertTo(context, culture, value, destinationType);
35 : }
36 : }
37 : }
|