Multiple Date in FileHelpers how?
Asked Answered
J

2

5

I am wondering how do I do multiple dates in filehelpers? It seems that you must specify every single format you are going to support (what kinda sucks...wish it could handle more of the basic ones).

   [FieldOrder(1), FieldConverter(ConverterKind.DateMultiFormat, "MM/dd/yyyy", "MM/d/yyyy", "M/d/yyyy","M/dd/yyyy")]

This gives me though

Error   35  Argument 1: cannot convert from 'FileHelpers.ConverterKind' to 'System.Type'    

So it seems I have to make some custom convert or something? Is this correct?

Edit

I am using version 2.9.9.0

Options

// Summary:
//     Indicates the FileHelpers.ConverterKind used for read/write operations.
//
// Remarks:
//     See the Complete attributes list for more information and examples of each
//     one.
[AttributeUsage(AttributeTargets.Field)]
public sealed class FieldConverterAttribute : Attribute
{
    // Summary:
    //     Indicates the FileHelpers.ConverterKind used for read/write operations.
    //
    // Parameters:
    //   converter:
    //     The FileHelpers.ConverterKind used for the transformations.
    public FieldConverterAttribute(ConverterKind converter);
    //
    // Summary:
    //     Indicates a custom FileHelpers.ConverterBase implementation.
    //
    // Parameters:
    //   customConverter:
    //     The Type of your custom converter.
    public FieldConverterAttribute(Type customConverter);
    //
    // Summary:
    //     Indicates the FileHelpers.ConverterKind used for read/write operations.
    //
    // Parameters:
    //   converter:
    //     The FileHelpers.ConverterKind used for the transformations.
    //
    //   arg1:
    //     The first param passed directly to the Converter Constructor.
    public FieldConverterAttribute(ConverterKind converter, string arg1);
    //
    // Summary:
    //     Indicates a custom FileHelpers.ConverterBase implementation.
    //
    // Parameters:
    //   customConverter:
    //     The Type of your custom converter.
    //
    //   args:
    //     A list of params passed directly to your converter constructor.
    public FieldConverterAttribute(Type customConverter, params object[] args);
    //
    // Summary:
    //     Indicates a custom FileHelpers.ConverterBase implementation.
    //
    // Parameters:
    //   customConverter:
    //     The Type of your custom converter.
    //
    //   arg1:
    //     The first param passed directly to the Converter Constructor.
    public FieldConverterAttribute(Type customConverter, string arg1);
    //
    // Summary:
    //     Indicates the FileHelpers.ConverterKind used for read/write operations.
    //
    // Parameters:
    //   converter:
    //     The FileHelpers.ConverterKind used for the transformations.
    //
    //   arg1:
    //     The first param passed directly to the Converter Constructor.
    //
    //   arg2:
    //     The second param passed directly to the Converter Constructor.
    public FieldConverterAttribute(ConverterKind converter, string arg1, string arg2);
    //
    // Summary:
    //     Indicates a custom FileHelpers.ConverterBase implementation.
    //
    // Parameters:
    //   customConverter:
    //     The Type of your custom converter.
    //
    //   arg1:
    //     The first param passed directly to the Converter Constructor.
    //
    //   arg2:
    //     The second param passed directly to the Converter Constructor.
    public FieldConverterAttribute(Type customConverter, string arg1, string arg2);
    //
    // Summary:
    //     Indicates the FileHelpers.ConverterKind used for read/write operations.
    //
    // Parameters:
    //   converter:
    //     The FileHelpers.ConverterKind used for the transformations.
    //
    //   arg1:
    //     The first param passed directly to the Converter Constructor.
    //
    //   arg2:
    //     The second param passed directly to the Converter Constructor.
    //
    //   arg3:
    //     The third param passed directly to the Converter Constructor.
    public FieldConverterAttribute(ConverterKind converter, string arg1, string arg2, string arg3);
    //
    // Summary:
    //     Indicates a custom FileHelpers.ConverterBase implementation.
    //
    // Parameters:
    //   customConverter:
    //     The Type of your custom converter.
    //
    //   arg1:
    //     The first param passed directly to the Converter Constructor.
    //
    //   arg2:
    //     The second param passed directly to the Converter Constructor.
    //
    //   arg3:
    //     The third param passed directly to the Converter Constructor.
    public FieldConverterAttribute(Type customConverter, string arg1, string arg2, string arg3);
}
Julissa answered 3/2, 2012 at 17:42 Comment(3)
I'm not familiar with filehelpers, but is there a reason you don't want to use the built-in datetime parsing functionality of the System.DateTime class of .Net?Chutzpah
Are you sure that you have a version with a FieldConverterAttribute(ConverterKind, params string[]) constructor overload? Since older versions don't allow more than 3 parameters for builtin conversions (some may support it but require manual wrapping in a string array – Not sure about that though, because I didn't research that far). At least the compiler error looks like that's the reason...Pusillanimity
Thanks for your question - it solved my issue (having 2 different date formats for 2 files) but also alerted me to the fact that FileHelpers exists as 2 NuGet packages - one for 2.0.0 and another for 2.9.9 - of course that isn't mentioned on the web site. Grrr...Harryharsh
F
13

There is no overload with parameters FieldConverterAttribute(ConverterKind, params string[]).

There is one with FieldConverterAttribute(ConverterKind, string, string, string) so you can supply up to 3 formats.

If you need more than that, then you can create your own converter:

public class CustomDateTimeConverter : ConverterBase
{
    public CustomDateTimeConverter(string format1, string format2, string format3, string format4)
    {
        _FormatStrings = new string[] { format1, format2, format3, format4} ;
    }

    private string[] _FormatStrings;

    public override object StringToField(string from)
    {
        foreach (string formatString in _FormatStrings)
        {
            DateTime dt;
            if (DateTime.TryParseExact(from, formatString, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
                return dt;
        }
        throw new ConvertException(from, typeof(DateTime));
    }
}

And then your field would look like

[FieldConverter(typeof(CustomDateTimeConverter), "MM/dd/yyyy", "MM/d/yyyy", "M/d/yyyy", "M/dd/yyyy")]
public DateTime Field1;
Franni answered 3/2, 2012 at 18:47 Comment(0)
C
1

As to your first comment, I don't know about Filehelpers, but every library I have worked with requires you to specify the format(s) you wish to recognize/parse. The DateTime facilities of .Net are are no different. However, .Net does provide a useful function that can return all of the built-in formats, which can then be iterated with a TryParse() (documentation).

For example, the following code uses the built-in DateTimeFormatInfo.GetAllDateTimeFormats() (documentation here) to loop through 128 custom datetime formats. (This code demonstrates a "round-trip": converting the date to strings and then parsing the strings):

using System;
using System.Globalization;
public class Example
{
      public static void Main()
   {
      DateTimeFormatInfo myDTFI = new CultureInfo("en-US", false).DateTimeFormat;

      char[] formats = { 'd', 'D', 'f', 'F', 'g', 'G', 'm', 'o', 
                           'r', 's', 't', 'T', 'u', 'U', 'y' };
      DateTime date1 = new DateTime(2011, 02, 01, 7, 30, 45, 0);
      DateTime date2;

      foreach (var fmt in formats) 
      {
         foreach (var pattern in myDTFI.GetAllDateTimePatterns(fmt))
         {
            // "round-trip" = convert the date to string, then parse it
            if (DateTime.TryParse(date1.ToString(pattern), out date2))
            {
                Console.WriteLine("{0:" + pattern + "} (format '{1}')",
                                  date1, pattern);
            }
         }
      }
   }
}
Chutzpah answered 3/2, 2012 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.