Code Monkey home page Code Monkey logo

Comments (5)

MarkPflug avatar MarkPflug commented on September 25, 2024 1

You should be able to mix as needed. I wouldn't necessarily recommend that approach, but it should work.

from sylvan.

MarkPflug avatar MarkPflug commented on September 25, 2024

The "databinder" doesn't know about formats, that's the responsibility of the CsvDataReader. You can set the date format on the CsvDataReaderOptions parameter. Here is anexample test:

var data = "d,x\n01/02/03,X";
var opts = new CsvDataReaderOptions { DateTimeFormat = "dd/MM/yy" };
var csv = CsvDataReader.Create(new StringReader(data), opts);
Assert.True(csv.Read());
Assert.Equal(new DateTime(2003, 2, 1), csv.GetDateTime(0));

from sylvan.

CaiusJard avatar CaiusJard commented on September 25, 2024

Thanks Mark.. Til now I'd just used a hack of binding to a string property and having the setter do the parse into an ignored DateTime field, because there were actually two different date formats in the file (sigh). I take it there's no scope for supporting that with a CSVDR as-is? i.e. are we currently stuck with the date format being a file-level config rather than column-level configurable?

from sylvan.

MarkPflug avatar MarkPflug commented on September 25, 2024

No, I support that too. You can set the format on a per-column basis via the "Schema" property. The schema is essentially a collection of DbColumn (bcl type) that specifies information about the column. I've extended the base type to support a "format" custom value. You can also "remap" the column names with the schema, to avoid putting attributes on your data record types. Here's an example:

	[Fact]
	public void DateFormat()
	{
		var data = "a,b\n01/02/03,01/02/03";

		var schema = Schema.Parse("a>Something:DateTime{MM/dd/yy},b>Other:DateTime{dd/MM/yy}");
		var opts = new CsvDataReaderOptions { 
			Schema = new CsvSchema(schema) };
		var csv = CsvDataReader.Create(new StringReader(data), opts);

		var records = csv.GetRecords<TestRecord>().ToList();

		Assert.Equal(new DateTime(2003, 1, 2), records[0].Something);
		Assert.Equal(new DateTime(2003, 2, 1), records[0].Other);
	}

	class TestRecord
	{
		public DateTime Something { get; set; }
		public DateTime Other { get; set; }
	}

from sylvan.

CaiusJard avatar CaiusJard commented on September 25, 2024

Does a Schema have to completely specify the entire mapping for a file, or can we mix DataMember attribs in with schema specifiers?

from sylvan.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.