Giter Club home page Giter Club logo

datetypeprovider's Introduction

DateTypeProvider

Build status NuGet Badge

F# Date Type Provider which provides strong type checking for dates.

Reason for the Type Provider

After watching this video 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

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<century = Century.``21``> // returns years from 2001 to 2100
printfn "%A" <| C21.``2012``.November.``01``.ToDateTime()

type C18 = Date<century = Century.``18``> // 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 [<Literal>] c80: Century = enum 80
type C80 = Date<century = c80>
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 [<Literal>] c100: Century = enum 100
type C100 = Date<century = c100>
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 LocalDate.

open NodaTime

type FSharp.DateTypeProvider.Date with
    member self.ToLocalDate() =
        LocalDate(self.Year, self.Month, self.Day)

printfn "%A" <| Date.``2013``.February.``01``.ToLocalDate()

datetypeprovider's People

Contributors

toburger avatar

Stargazers

 avatar  avatar

Watchers

 avatar

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.