Home > OS >  FileHelpers not using correct Date format
FileHelpers not using correct Date format

Time:08-11

I am attempting to parse records with a DateTime field, but FileHelpers is not using the format I am providing. The following is an F# script that should parse the records but throws an error. I believe the problem is related to it being an F# Script because this code works when I use it in a .fsproj and run it with dotnet run.

#r "nuget: FileHelpers, 3.5.1"

open System
open FileHelpers

[<DelimitedRecord(","); IgnoreFirst; CLIMutable>]
type RateRecord =
    {
        [<FieldConverter(ConverterKind.Date, "yyyy-MM-dd hh:mm")>]
        DateTime : DateTime
        Value : float
    }
  
let sampleData = """DateTime,Value
2020-02-01 00:00,259
2020-02-01 00:01,267
2020-02-01 00:02,270"""

let engine = FileHelperEngine<RateRecord>()
let values = engine.ReadString sampleData

The output of running the script. The error message is at the end. Notice that the error message says it attempted to use the 'ddMMyyyy' format, which is not what I specified.

> #r "nuget: FileHelpers, 3.5.1"
-
- open System
- open FileHelpers
-
- [<DelimitedRecord(","); IgnoreFirst; CLIMutable>]
- type RateRecord =
-     {
-         [<FieldConverter(ConverterKind.Date, "yyyy-MM-dd hh:mm")>]
-         DateTime : DateTime
-         Value : float
-     }
-
-
- let sampleData = """DateTime,Value
- 2020-02-01 00:00,259
- 2020-02-01 00:01,267
- 2020-02-01 00:02,270"""
-
- let engine = FileHelperEngine<RateRecord>()
- let values = engine.ReadString sampleData
- ;;
[Loading C:\Users\mcrews\AppData\Local\Temp\22000--4251218c-2322-40be-9ae7-fce17ffe54c3\Project.fsproj.fsx]
namespace FSI_0012.Project

FileHelpers.ConvertException: Error Converting '2020-02-01 00:00' to type: 'DateTime'.  There are more chars in the Input String than in the Format string: 'ddMMyyyy'
   at FileHelpers.Converters.DateTimeConverter.StringToField(String from)
   at FileHelpers.FieldBase.AssignFromString(ExtractedInfo fieldString, LineInfo line)
   at FileHelpers.FieldBase.ExtractFieldValue(LineInfo line)
   at FileHelpers.RecordOperations.StringToRecord(Object record, LineInfo line, Object[] values)
   at FileHelpers.FileHelperEngine`1.ReadStreamAsList(TextReader reader, Int32 maxRecords, DataTable dt)
   at FileHelpers.FileHelperEngine`1.ReadStream(TextReader reader, Int32 maxRecords)
   at FileHelpers.FileHelperEngine`1.ReadString(String source, Int32 maxRecords)
   at FileHelpers.FileHelperEngine`1.ReadString(String source)
   at <StartupCode$FSI_0013>.$FSI_0013.main@() in d:\Documents\GitHub\Scratchpad\FileHelpers\Test.fsx:line 158
Stopped due to error
>

CodePudding user response:

It looks like this has something to do with support for .NET Core in F# interactive. Assuming you're using Visual Studio, if you turn off "Use .NET Core Scripting" in Tools > Options, the script works as expected:

Set to False

Output:

Microsoft (R) F# Interactive version 12.0.4.0 for F# 6.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

...

val values: RateRecord[] =
  [|{ DateTime = 2/1/2020 12:00:00 AM
      Value = 259.0 }; { DateTime = 2/1/2020 12:01:00 AM
                         Value = 267.0 }; { DateTime = 2/1/2020 12:02:00 AM
                                            Value = 270.0 }|]

The problem is that the FieldConverter attribute is ignored when .NET Core scripting is enabled. I'm guessing this is somehow caused by the way that FileHelpers is built and/or issues in .NET Core Scripting, which leads to reflection misbehavior (perhaps subtle incompatibilities in assembly formats between different versions of .NET), but I can't say for sure. Will update if I learn more.

  • Related