FileHelpers CSV timespan - f#

I'm parsing a CSV with the fileHelpers module in F#. Was wondering what the best approach to converting a time to the best format. I assumed the best format would be a timeSpan, however can't find a convertKind for the timespan
if i had a string e.g. 10:05.0
This would represent 10:05 am. What would be the best way to use ConvertKind with the FileHelpers Library to parse this string into a structure that represented 10:05
thanks for all the help!

You could implement your conversion in a custom Converter (inherit ConverterBase), then use it in your FieldConverter attribute

Related

How can I parse a particular representation of ISO 8601 timestamp (one without any whitespaces) using DateTimeFormatter?

I am trying to parse an ISO 8601 timestamp which looks like this: "20220603T054813Z". My supervisor gave me this timestamp asking me to parse it (she found it on wiki under the ISO-8601 page) but I couldnt find information regarding this type of timestamp anywhere else.
I am trying to parse it using a LocalDateTime instance and using a pre-defined format but I cant find any. Using any of the classes doesnt work neither do the pre-defined formatters for ISO 8601 timestamps. There's no example in the documentation that explains how to handle this particular case and that makes me wonder if this is a valid ISO 8601 case. It does work with my own created format but I am writing a logic to handle ISO 8601 cases without me having to define a custom format, as much as possible. atleast for the ISO 8601 timestamps. Is this a valid ISO 8601 timestamp even? Because usually they have separators for date and time.
Is there a way to handle timestamps like these, which doesnt have any separators or whitespaces, with a pre-defined formatter? Maybe i am missing something. Any help would be appreciated, thanks!

Parsing and pretty printing the same file format in Haskell

I was wondering, if there is a standard, canonical way in Haskell to write not only a parser for a specific file format, but also a writer.
In my case, I need to parse a data file for analysis. However, I also simulate data to be analyzed and save it in the same file format. I could now write a parser using Parsec or something equivalent and also write functions that perform the text output in the way that it is needed, but whenever I change my file format, I would have to change two functions in my code. Is there a better way to achieve this goal?
Thank you,
Dominik
The BNFC-meta package https://hackage.haskell.org/package/BNFC-meta-0.4.0.3
might be what you looking for
"Specifically, given a quasi-quoted LBNF grammar (as used by the BNF Converter) it generates (using Template Haskell) a LALR parser and pretty pretty printer for the language."
update: found this package that also seems to fulfill the objective (not tested yet) http://hackage.haskell.org/package/syntax

F# Dsl for simple expression?

I want to write a generic program to load data from text files or database to a table.
The transformation between the source and destination shouldn't be hard-coded. They may have the format of
ColA = Col1 + Col1 * 1.5
ColB = convert Col3 to date
These rules may need to be converted to SQL or C# code. Does F# already have some library to do these? Is F# the good language to implement it?
With so few specific details in your question, we can't really give you a good answer. But here are a few F# libraries that you might find useful for what you're trying to do:
FSharp.Data - Whether your incoming data is in SQL, CSV, JSON, or XML, there's a type provider that can parse it for you and let you write type-safe queries against it.
FParsec - Lets you easily write custom parsers, so that you can define your transformations in a custom DSL without too much effort. You mentioned custom DSLs in your title, so that's why I'm recommending FParsec. I've used it myself for exactly that purpose, and it was great.
That's about all the help I can give you until I know more details about what you're trying to achieve.

How would I convert ISO 8601 durations into a formatted string in Lua?

I've been using Youtube API v3 (yes, I know, it wasn't meant for Lua) recently, but when I need to convert an ISO 8601 duration into a formatted string, nothing on the web helps. I've been googling all over the place, to search for a specific library that could help with this sort of thing, but unfortunately, there is NONE for Lua. There is thousands of libraries out there for other languages except Lua.
And now, it seems I'm stuck with string patterns which I don't even know how to use. So how else would I go about doing this task?
Example of an ISO 8601 duration:
PT3M33S
I want to convert it into something like this:
3:33
If you don't want to parse the whole ISO 8601 specification, try this code:
s="PT3M33S"
t=s:gsub("^.-(%d+)M(%d+)S","%1:%2")
print(t)
It uses Lua pattern matching. The pattern reads: skip everything until a run of digits followed by an M and then find a run of digits followed by an S. Capture both runs of digits and use them in the replacement pattern.
If you want to extract both numbers, use this:
s="PT3M33S"
M,S=s:match("^.-(%d+)M(%d+)S")
print(M,S)

Time of Day in the JSON response model?

I am using ASP.NET Web Api 2 with Json.NET 6.0.1.
According to ISO 8601, dates should be interchanged in a certain way. I am using the IsoDateTimeConverter() in order to achieve this:
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new IsoDateTimeConverter());
But how should "time of day" be returned in a JSON response model?
I cannot find anything for this in the ISO specification.
Should time perhaps be returned as a:
TimeSpan? (with expectation of the user to not use this as a duration representation)
DateTime? (with expectation of the user to drop off the date part)
A custom Time class
There is no standard structure in JSON for containing dates or times (see JSON.org). The de-facto stardard for dates-time values is using a string in ISO 8601 format, as you mentioned. But since there is no official standard it really comes down to what works best for you and consumers of your API.
Using a DateTime object is a reasonable choice because the support already exists in Json.Net and other serializers for converting these to and from ISO 8601 strings. So this would be the easiest to implement. However, users of your API would have to know to disregard the date portion, as you said. You could set the date to 0001-01-01 to emphasize its irrelevance. This isn't so different from the more common situation where you need only a date in your API and the time doesn't matter. Most people just set the time to midnight in this case and let it go. But, I would agree that this approach does seem to have a little bit of a "code smell" to it, given that part of the value is just noise.
Perhaps a cleaner idea is to format your DateTime value as ISO 8601, but then chop off the date portion before returning it. So users of the API would get a string that looks like 14:35:28.906Z. You could write a simple JsonConverter to handle this for you during serialization. This would sort of give you the best of both worlds -- a cleaner API, but you still can work with the familiar DateTime struct internally.
A custom Time class could also work here, but might be overkill, depending. If you do need to go there, you might want to look into a third-party library such as Noda Time, which has classes already built for these kinds of things, and also has pre-built converters for Json.Net.
I would definitely not choose TimeSpan for this purpose. Wrong tool for the job.

Resources