You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Repo Assist] Add PreferDateTimeOffset parameter to CsvProvider, JsonProvider, and XmlProvider (#1668)
* Add PreferDateTimeOffset parameter to CsvProvider, JsonProvider, XmlProvider
Adds a new PreferDateTimeOffset static parameter (default: false) to
CsvProvider, JsonProvider, and XmlProvider.
When set to true, date-time values that would normally be inferred as
DateTime are instead inferred as DateTimeOffset (using the local UTC
offset). Values that already parse with an explicit timezone offset are
already inferred as DateTimeOffset regardless of this parameter.
This addresses the long-standing request in #1100 (configure default
DateTimeKind for dates without timezone) and #1072 (control over
timezone in CSV date writing).
Closes#1100Closes#1072
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* ci: trigger CI checks
* Add PreferDateTimeOffset documentation to CsvProvider, JsonProvider, XmlProvider, and TypeInference docs
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* ci: trigger CI checks
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Don Syme <dsyme@users.noreply.github.com>
Co-authored-by: Repo Assist <repo-assist@github.com>
Copy file name to clipboardExpand all lines: RELEASE_NOTES.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,7 @@
2
2
3
3
## 8.1.0-beta
4
4
5
+
- Add `PreferDateTimeOffset` parameter to `CsvProvider`, `JsonProvider`, and `XmlProvider`: when true, date-time values without an explicit timezone offset are inferred as `DateTimeOffset` (using local offset) instead of `DateTime` (closes #1100, #1072)
5
6
- Make `Http.AppendQueryToUrl` public (closes #1325)
6
7
- Add `PreferOptionals` parameter to `JsonProvider` and `XmlProvider` (defaults to `true` to match existing behavior; set to `false` to use empty string or `NaN` for missing values, like the CsvProvider default) (closes #649)
Copy file name to clipboardExpand all lines: docs/library/CsvProvider.fsx
+14-2Lines changed: 14 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -90,8 +90,8 @@ let msft = Stocks.Load(__SOURCE_DIRECTORY__ + "/../data/MSFT.csv").Cache()
90
90
91
91
// Look at the most recent row. Note the 'Date' property
92
92
// is of type 'DateTime' by default. Set PreferDateOnly = true
93
-
// to use 'DateOnly' on .NET 6+
94
-
// and 'Open' has a type 'decimal'
93
+
// to use 'DateOnly' on .NET 6+, or PreferDateTimeOffset = true
94
+
// to use 'DateTimeOffset'. 'Open' has a type 'decimal'.
95
95
letfirstRow= msft.Rows |> Seq.head
96
96
letlastDate= firstRow.Date
97
97
letlastOpen= firstRow.Open
@@ -111,6 +111,7 @@ to the columns in the CSV file.
111
111
As you can see, the type provider also infers types of individual rows. The `Date`
112
112
property is inferred as `DateTime` by default. When you set `PreferDateOnly = true`
113
113
on .NET 6 and later, date-only strings (without a time component) are inferred as `DateOnly`.
114
+
Set `PreferDateTimeOffset = true` to infer date-time values as `DateTimeOffset`.
114
115
HLOC prices are inferred as `decimal`.
115
116
116
117
## Using units of measure
@@ -276,6 +277,10 @@ On .NET 6 and later, when you set `PreferDateOnly = true`, columns whose values
276
277
are inferred as `DateOnly`. Time-only strings are inferred as `TimeOnly`. If a column mixes `DateOnly` and `DateTime` values, it is unified to `DateTime`.
277
278
By default (`PreferDateOnly = false`), all date values are inferred as `DateTime` for backward compatibility.
278
279
280
+
Set `PreferDateTimeOffset = true` to infer all date-time columns (that would otherwise be `DateTime`) as `DateTimeOffset` instead.
281
+
Values that already carry an explicit timezone offset (e.g. `2023-06-15T10:30:00+05:30`) are always inferred as `DateTimeOffset` regardless of this flag.
282
+
`PreferDateTimeOffset` and `PreferDateOnly` are independent: `DateOnly` columns stay as `DateOnly` even when `PreferDateTimeOffset=true`.
283
+
279
284
If a value is missing in any row, by default the CSV type provider will infer a nullable (for `int`, `int64`, and `DateOnly`) or an optional
280
285
(for `bool`, `DateTime`, `DateTimeOffset`, and `Guid`). When a `decimal` would be inferred but there are missing values, we will infer a
281
286
`float` instead, and use `Double.NaN` to represent those missing values. The `string` type is already inherently nullable,
@@ -403,6 +408,13 @@ You can also explicitly request a `DateOnly` or `TimeOnly` column using schema a
403
408
In the example above, `EventDate` is explicitly annotated as `dateonly` and `Duration` is explicitly
404
409
annotated as `timeonly?` (a nullable `TimeOnly`).
405
410
411
+
### DateTimeOffset
412
+
413
+
Set `PreferDateTimeOffset = true` to infer all date-time columns (that would otherwise be `DateTime`) as
414
+
`DateTimeOffset` instead. This is useful when you need to preserve or work with timezone-aware values.
415
+
Values that already carry an explicit timezone offset in the sample data (e.g. `2023-06-15T10:30:00+05:30`)
416
+
are always inferred as `DateTimeOffset` regardless of this flag.
417
+
406
418
## Transforming CSV files
407
419
408
420
In addition to reading, `CsvProvider` also has support for transforming the row collection of CSV files. The operations
"""<summary>Typed representation of a CSV file.</summary>
@@ -270,7 +279,8 @@ type public CsvProvider(cfg: TypeProviderConfig) as this =
270
279
(e.g. 'MyCompany.MyAssembly, resource_name.csv'). This is useful when exposing types generated by the type provider.</param>
271
280
<param name='PreferDateOnly'>When true on .NET 6+, date-only strings are inferred as DateOnly and time-only strings as TimeOnly. Defaults to false for backward compatibility.</param>
272
281
<param name='StrictBooleans'>When true, only <c>true</c> and <c>false</c> (case-insensitive) are inferred as boolean. Values such as <c>0</c>, <c>1</c>, <c>yes</c>, and <c>no</c> are treated as integers or strings respectively. Defaults to false.</param>
273
-
<param name='UseOriginalNames'>When true, CSV column header names are used as-is for generated property names instead of being normalized (e.g. capitalizing the first letter). Defaults to false.</param>"""
282
+
<param name='UseOriginalNames'>When true, CSV column header names are used as-is for generated property names instead of being normalized (e.g. capitalizing the first letter). Defaults to false.</param>
283
+
<param name='PreferDateTimeOffset'>When true, date-time strings without an explicit timezone offset are inferred as DateTimeOffset (using the local offset) instead of DateTime. Defaults to false.</param>"""
274
284
275
285
do csvProvTy.AddXmlDoc helpText
276
286
do csvProvTy.DefineStaticParameters(parameters, buildTypes)
"""<summary>Typed representation of a JSON document.</summary>
@@ -205,7 +214,8 @@ type public JsonProvider(cfg: TypeProviderConfig) as this =
205
214
<param name='PreferDateOnly'>When true on .NET 6+, date-only strings (e.g. "2023-01-15") are inferred as DateOnly and time-only strings as TimeOnly. Defaults to false for backward compatibility.</param>
206
215
<param name='UseOriginalNames'>When true, JSON property names are used as-is for generated property names instead of being normalized to PascalCase. Defaults to false.</param>
207
216
<param name='OmitNullFields'>When true, optional fields with value None are omitted from the generated JSON rather than serialized as null. Defaults to false.</param>
208
-
<param name='PreferOptionals'>When set to true (default), inference will use the option type for missing or null values. When false, inference will prefer to use empty string or double.NaN for missing values where possible, matching the default CsvProvider behavior.</param>"""
217
+
<param name='PreferOptionals'>When set to true (default), inference will use the option type for missing or null values. When false, inference will prefer to use empty string or double.NaN for missing values where possible, matching the default CsvProvider behavior.</param>
218
+
<param name='PreferDateTimeOffset'>When true, date-time strings without an explicit timezone offset are inferred as DateTimeOffset (using the local offset) instead of DateTime. Defaults to false.</param>"""
209
219
210
220
do jsonProvTy.AddXmlDoc helpText
211
221
do jsonProvTy.DefineStaticParameters(parameters, buildTypes)
"""<summary>Typed representation of a XML file.</summary>
@@ -232,7 +248,8 @@ type public XmlProvider(cfg: TypeProviderConfig) as this =
232
248
<param name='PreferDateOnly'>When true on .NET 6+, date-only strings are inferred as DateOnly and time-only strings as TimeOnly. Defaults to false for backward compatibility.</param>
233
249
<param name='DtdProcessing'>Controls how DTD declarations in the XML are handled. Accepted values: "Ignore" (default, silently skips DTD processing, safe for most cases), "Prohibit" (throws on any DTD declaration), "Parse" (enables full DTD processing including entity expansion, use with caution).</param>
234
250
<param name='UseOriginalNames'>When true, XML element and attribute names are used as-is for generated property names instead of being normalized to PascalCase. Defaults to false.</param>
235
-
<param name='PreferOptionals'>When set to true (default), inference will use the option type for missing or absent values. When false, inference will prefer to use empty string or double.NaN for missing values where possible, matching the default CsvProvider behavior.</param>"""
251
+
<param name='PreferOptionals'>When set to true (default), inference will use the option type for missing or absent values. When false, inference will prefer to use empty string or double.NaN for missing values where possible, matching the default CsvProvider behavior.</param>
252
+
<param name='PreferDateTimeOffset'>When true, date-time strings without an explicit timezone offset are inferred as DateTimeOffset (using the local offset) instead of DateTime. Defaults to false.</param>"""
0 commit comments