Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Formatters for RFC3339, as discussed in issue #53 #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/Data/Formatter/DateTime/Standards.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- | Formatters for standard date and time formats.
module Data.Formatter.DateTime.Standards
( rfc3339
, rfc3339WithMilliseconds ) where

import Prelude

import Data.Formatter.DateTime (Formatter, FormatterCommand(..))
import Data.List (List(..), (:))

-- | Formats according to RFC 3339, with no fractional seconds and always in
-- | UTC, which is always specified with "Z", not "+00:00".
-- |
-- | E.g. `"2019-03-14T21:01:42Z"` can be parsed by this formatter.
rfc3339 ∷ Formatter
rfc3339 = makeRfc3339 z

-- | Formats according to RFC 3339, with fractional seconds to 3 digits and
-- | always in UTC, which is always specified with "Z", not "+00:00".
-- |
-- | E.g. `"2019-03-14T21:01:42.820Z"` can be parsed by this formatter.
rfc3339WithMilliseconds ∷ Formatter
rfc3339WithMilliseconds = makeRfc3339 $ Placeholder "." : Milliseconds : z

makeRfc3339 ∷ List FormatterCommand → Formatter
makeRfc3339 tail =
let dash = Placeholder "-"
colon = Placeholder ":"
in
YearFull : dash : MonthTwoDigits : dash : DayOfMonthTwoDigits
: Placeholder "T"
: Hours24 : colon : MinutesTwoDigits : colon : SecondsTwoDigits
: tail

z ∷ List FormatterCommand
z = Placeholder "Z" : Nil
8 changes: 7 additions & 1 deletion test/src/DateTime.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ import Data.DateTime (DateTime)
import Data.Either (Either(..))
import Data.Formatter.DateTime as FDT
import Data.List (fromFoldable)
import Test.DateTime.Standards (standardsTest)
import Test.Spec (describe, Spec, it)
import Test.Spec.Assertions (shouldEqual)
import Test.Utils (forAll, makeDateTime)

datetimeTest ∷ Spec Unit
datetimeTest = describe "Data.Formatter.DateTime" do
datetimeTest = do
moduleTest
standardsTest

moduleTest ∷ Spec Unit
moduleTest = describe "Data.Formatter.DateTime" do
forAll (\a → a.format <> " | " <> a.dateStr)
"formatDateTime/unformatDateTime should format/unformat dateTime"
[ { format: "MM/DD/YYYY", dateStr: "04/12/2017" , date: makeDateTime 2017 4 12 11 3 4 234}
Expand Down
30 changes: 30 additions & 0 deletions test/src/DateTime/Standards.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Test.DateTime.Standards (standardsTest) where

import Prelude

import Data.Either (Either(..))
import Data.Formatter.DateTime as FDT
import Data.Formatter.DateTime.Standards as S
import Test.Spec (describe, Spec, it)
import Test.Spec.Assertions (shouldEqual)
import Test.Utils (makeDateTime)

standardsTest ∷ Spec Unit
standardsTest = describe "Data.Formatter.DateTime.Standards" do

let date = makeDateTime 2019 3 14 21 15 23 123
let dateZeroMs = makeDateTime 2019 3 14 21 15 23 0

describe "rfc3339" do
let str = "2019-03-14T21:15:23Z"
it "format" do
FDT.format S.rfc3339 date `shouldEqual` str
it "unformat" do
FDT.unformat S.rfc3339 str `shouldEqual` Right dateZeroMs

describe "rfc3339WithMilliseconds" do
let str = "2019-03-14T21:15:23.123Z"
it "format" do
FDT.format S.rfc3339WithMilliseconds date `shouldEqual` str
it "unformat" do
FDT.unformat S.rfc3339WithMilliseconds str `shouldEqual` Right date