https://github.com/toburger/datetypeprovider
F# date type provider which provides strong type checking for dates.
https://github.com/toburger/datetypeprovider
date datetime dotnet dotnet-core dotnet-standard fsharp nodatime typeprovider
Last synced: 3 months ago
JSON representation
F# date type provider which provides strong type checking for dates.
- Host: GitHub
- URL: https://github.com/toburger/datetypeprovider
- Owner: toburger
- License: mit
- Created: 2014-07-24T11:12:53.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2018-05-28T17:43:34.000Z (over 7 years ago)
- Last Synced: 2025-08-16T04:17:09.565Z (5 months ago)
- Topics: date, datetime, dotnet, dotnet-core, dotnet-standard, fsharp, nodatime, typeprovider
- Language: F#
- Size: 118 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
DateTypeProvider
================
[](https://ci.appveyor.com/project/toburger/datetypeprovider)
[](https://www.nuget.org/packages/FSharp.DateTypeProvider/)
F# Date Type Provider which provides strong type checking for dates.
Reason for the Type Provider
----------------------------
After watching [this video](https://vimeo.com/97349221) from Scott Meyers
where he showed an example of a strongly typed datetime class
I thought this is a good fit for an F# Type Provider.
Example
-------
```fsharp
open DateProvider
// dates can be represented with month digits
Date.``2004``.``02``.``29``.ToDateTime()
// or with month names
Date.``2004``.February.``29``.ToDateTime()
// does not compile
Date.``2002``.``13``.``10``.ToDateTime()
Date.``2002``.``02``.``29``.ToDateTime()
// you can also return a DateTimeOffset
Date.``2002``.``13``.``10``.ToDateTimeOffset()
// you can also provide a century parameter
type C21 = Date // returns years from 2001 to 2100
printfn "%A" <| C21.``2012``.November.``01``.ToDateTime()
type C18 = Date // returns years from 1701 to 1800
printfn "%A" <| C18.``1789``.July.``14``.ToDateTime()
// you can force a century even if it is not defined in the enum
let [] c80: Century = enum 80
type C80 = Date
printfn "%A" <| C80.``7908``.July.``14``.ToDateTime()
// providing a Century value which is not in the valid range (1 to 99) the provider falls back to the current century
let [] c100: Century = enum 100
type C100 = Date
printfn "%A" <| C100.``2004``.July.``14``.ToDateTime()
```
Working with NodaTime
---------------------
The Type Provider returns a ```Date``` record with the Fields ```Year```, ```Month``` and ```Day```. You can call the methods ```ToDateTime()``` and ```ToDateTimeOffset(?offset)``` to return a ```DateTime``` or a ```DateTimeOffset```.
Likewise you can write your own extension method to return a [NodaTime](http://nodatime.org/) ```LocalDate```.
```fsharp
open NodaTime
type FSharp.DateTypeProvider.Date with
member self.ToLocalDate() =
LocalDate(self.Year, self.Month, self.Day)
printfn "%A" <| Date.``2013``.February.``01``.ToLocalDate()
```