FlatFile icon indicating copy to clipboard operation
FlatFile copied to clipboard

converting datetime with layout

Open pelife opened this issue 7 years ago • 4 comments

Idid not find any property to instruct how to convert dates in my flat file to a property of time DateTime and when I tried to read the file, an exception was thrown converting 20170712 to type DateTime.

public class BDIHeader
    {
        public int Tipo { get; set; }
        public string NomeArquivo { get; set; }
        public string Origem { get; set; }
        public int Destino { get; set; }
        public DateTime DataGeracao { get; set; }
        public DateTime DataPregao { get; set; }
        public string HoraMinuto { get; set; }
    }
public sealed class BDIHeaderLayout : FixedLayout<BDIHeader>
    {
        public BDIHeaderLayout()
        {
            this.WithMember(x => x.Tipo, c => c.WithLength(2))
                .WithMember(x => x.NomeArquivo, c => c.WithLength(8))
                .WithMember(x => x.Origem, c => c.WithLength(8))
                .WithMember(x => x.Destino, c => c.WithLength(4))
                .WithMember(x => x.DataGeracao, c => c.WithLength(8))
                .WithMember(x => x.DataPregao, c => c.WithLength(8))
                .WithMember(x => x.HoraMinuto, c => c.WithLength(4));
        }
    }
public void Read()
        {
            //
            var factory = new FixedLengthFileEngineFactory();
            using (var stream = new FileInfo(enderecoArquivoBDI).Open(FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                // If using attribute mapping, pass an array of record types
                // rather than layout instances
                var layouts = new ILayoutDescriptor<IFixedFieldSettingsContainer>[]
                {
                    new BDIHeaderLayout(),new BDIIndiceLayout()

                };
                
                var flatFile = factory.GetEngine(layouts,
                    line =>
                    {
                        // For each line, return the proper record type.
                        // The mapping for this line will be loaded based on that type.
                        // In this simple example, the first character determines the
                        // record type.
                        if (String.IsNullOrEmpty(line) || line.Length < 1) return null;
                        switch (line.Substring(0, 2))
                        {
                            case "00":
                                return typeof(BDIHeader);
                            //case "01":
                            //    return typeof(BDIIndice);
                                //case "02":
                                //    return typeof(BDINegociosPapelLayout);
                                //case "99":
                                //    return typeof(BDITrailerLayout);
                        }
                        return null;
                    });

                flatFile.Read(stream);

                var header = flatFile.GetRecords<BDIHeader>().FirstOrDefault();
                //var indices = flatFile.GetRecords<BDIIndice>().ToList();
                //var negocios = flatFile.GetRecords<BDINegociosPapelLayout>();
                //var trailer = flatFile.GetRecords<BDITrailer>().FirstOrDefault();
            }
        }

line being read 00BDIN9999BOVESPA 999920170713201707131807

pelife avatar Jul 13 '17 23:07 pelife

solved my problem, see #42

pelife avatar Jul 17 '17 21:07 pelife

#55

forcewake avatar Sep 22 '17 15:09 forcewake

I really think we need something like WithStringNormalizer but it's an action where we can convert the raw value to the property type... This would allow us to do conversions for boolean values and dates much much easier.

niemyjski avatar Sep 25 '17 20:09 niemyjski

I have submitted PR #78 that would help with this scenario.

mthamil avatar Aug 08 '18 05:08 mthamil