parent
f4a4f5633b
commit
35f617bbcc
|
@ -0,0 +1,203 @@
|
|||
module Date exposing (..)
|
||||
|
||||
import String exposing (fromInt)
|
||||
|
||||
type Weekday
|
||||
= Mon
|
||||
| Tue
|
||||
| Wed
|
||||
| Thu
|
||||
| Fri
|
||||
| Sat
|
||||
| Sun
|
||||
|
||||
|
||||
type alias Day =
|
||||
Int
|
||||
|
||||
|
||||
type alias Month =
|
||||
Int
|
||||
|
||||
|
||||
type alias Year =
|
||||
Int
|
||||
|
||||
|
||||
type alias Date =
|
||||
( Day, Month, Year )
|
||||
|
||||
|
||||
year : Date -> Year
|
||||
year ( _, _, y ) =
|
||||
y
|
||||
|
||||
|
||||
month : Date -> Month
|
||||
month ( _, m, _ ) =
|
||||
m
|
||||
|
||||
|
||||
day : Date -> Day
|
||||
day ( d, _, _ ) =
|
||||
d
|
||||
|
||||
|
||||
lt : Date -> Date -> Bool
|
||||
lt ( d1, m1, y1 ) ( d2, m2, y2 ) =
|
||||
(y1 < y2) || (y1 == y2 && m1 < m2) || (y1 == y2 && m1 == m2 && d1 < d2)
|
||||
|
||||
|
||||
eq : Date -> Date -> Bool
|
||||
eq ( d1, m1, y1 ) ( d2, m2, y2 ) =
|
||||
y1 == y2 && m1 == m2 && d1 == d2
|
||||
|
||||
|
||||
gt : Date -> Date -> Bool
|
||||
gt ( d1, m1, y1 ) ( d2, m2, y2 ) =
|
||||
(y1 > y2) || (y1 == y2 && m1 > m2) || (y1 == y2 && m1 == m2 && d1 > d2)
|
||||
|
||||
|
||||
toString : Date -> String
|
||||
toString ( d, m, y ) =
|
||||
let
|
||||
d0 =
|
||||
if (d // 10) == 0 then
|
||||
"0"
|
||||
|
||||
else
|
||||
""
|
||||
|
||||
m0 =
|
||||
if (m // 10) == 0 then
|
||||
"0"
|
||||
|
||||
else
|
||||
""
|
||||
in
|
||||
d0 ++ fromInt d ++ "." ++ m0 ++ fromInt m ++ "." ++ fromInt y
|
||||
|
||||
|
||||
next : Date -> Date
|
||||
next ( d, m, y ) =
|
||||
case ( d, m, y ) of
|
||||
( 31, 12, _ ) ->
|
||||
( 1, 1, y + 1 )
|
||||
|
||||
( 30, 11, _ ) ->
|
||||
( 1, 12, y )
|
||||
|
||||
( 31, 10, _ ) ->
|
||||
( 1, 11, y )
|
||||
|
||||
( 30, 9, _ ) ->
|
||||
( 1, 10, y )
|
||||
|
||||
( 31, 8, _ ) ->
|
||||
( 1, 9, y )
|
||||
|
||||
( 31, 7, _ ) ->
|
||||
( 1, 8, y )
|
||||
|
||||
( 30, 6, _ ) ->
|
||||
( 1, 7, y )
|
||||
|
||||
( 31, 5, _ ) ->
|
||||
( 1, 6, y )
|
||||
|
||||
( 30, 4, _ ) ->
|
||||
( 1, 5, y )
|
||||
|
||||
( 31, 3, _ ) ->
|
||||
( 1, 4, y )
|
||||
|
||||
( 29, 2, _ ) ->
|
||||
( 1, 3, y )
|
||||
|
||||
( 28, 2, _ ) ->
|
||||
if leapyear y then
|
||||
( 29, 2, y )
|
||||
|
||||
else
|
||||
( 1, 3, y )
|
||||
|
||||
( 31, 1, _ ) ->
|
||||
( 1, 2, y )
|
||||
|
||||
_ ->
|
||||
( d + 1, m, y )
|
||||
|
||||
|
||||
prev : Date -> Date
|
||||
prev ( d, m, y ) =
|
||||
case ( d, m, y ) of
|
||||
( 1, 12, _ ) ->
|
||||
( 30, 11, y )
|
||||
|
||||
( 1, 11, _ ) ->
|
||||
( 31, 10, y )
|
||||
|
||||
( 1, 10, _ ) ->
|
||||
( 30, 9, y )
|
||||
|
||||
( 1, 9, _ ) ->
|
||||
( 31, 8, y )
|
||||
|
||||
( 1, 8, _ ) ->
|
||||
( 31, 7, y )
|
||||
|
||||
( 1, 7, _ ) ->
|
||||
( 30, 6, y )
|
||||
|
||||
( 1, 6, _ ) ->
|
||||
( 31, 5, y )
|
||||
|
||||
( 1, 5, _ ) ->
|
||||
( 30, 4, y )
|
||||
|
||||
( 1, 4, _ ) ->
|
||||
( 31, 3, y )
|
||||
|
||||
( 1, 3, _ ) ->
|
||||
if leapyear y then
|
||||
( 29, 2, y )
|
||||
|
||||
else
|
||||
( 28, 2, y )
|
||||
|
||||
( 1, 2, _ ) ->
|
||||
( 31, 1, y )
|
||||
|
||||
( 1, 1, _ ) ->
|
||||
( 31, 12, y - 1 )
|
||||
|
||||
_ ->
|
||||
( d - 1, m, y )
|
||||
|
||||
|
||||
leapyear : Year -> Bool
|
||||
leapyear y =
|
||||
(modBy 4 y == 0) && ((modBy 100 y /= 0) || (modBy 400 y == 0))
|
||||
|
||||
|
||||
sub : Date -> Date -> Int
|
||||
sub d1 d2 =
|
||||
subHelp 0 d1 d2
|
||||
|
||||
|
||||
|
||||
{- um optimierbare End-Rekursion zu erreichen: Akkumulator als Argument -}
|
||||
|
||||
|
||||
subHelp : Int -> Date -> Date -> Int
|
||||
subHelp diff d1 d2 =
|
||||
if gt d1 d2 then
|
||||
subHelp (diff + 1) d1 (next d2)
|
||||
|
||||
else if lt d1 d2 then
|
||||
subHelp (diff - 1) d1 (prev d2)
|
||||
|
||||
else
|
||||
diff
|
||||
|
||||
|
|
@ -88,55 +88,6 @@ testToString =
|
|||
Expect.equal "02.06.2024" (toString (2, 6, 2024))
|
||||
]
|
||||
|
||||
testNext : Test
|
||||
testNext =
|
||||
describe "next method test"
|
||||
[test "next of 01.01.1970" <|
|
||||
\() ->
|
||||
Expect.equal (2, 1, 1970) (next (1, 1, 1970))
|
||||
, test "next of 02.06.2024 " <|
|
||||
\() ->
|
||||
Expect.equal (3, 6, 2024) (next (2, 6, 2024))
|
||||
, test "next of 31.12.2020" <|
|
||||
\() ->
|
||||
Expect.equal (1, 1, 2021) (next (31, 12, 2020))
|
||||
-- leap year test
|
||||
, test "next of 28.02.2020" <|
|
||||
\() ->
|
||||
Expect.equal (29, 2, 2020) (next (28, 2, 2020))
|
||||
, test "next of 29.02.2020" <|
|
||||
\() ->
|
||||
Expect.equal (1, 3, 2020) (next (29, 2, 2020))
|
||||
-- non-leap year test
|
||||
, test "next of 28.02.2021" <|
|
||||
\() ->
|
||||
Expect.equal (1, 3, 2021) (next (28, 2, 2021))
|
||||
]
|
||||
|
||||
testPrev : Test
|
||||
testPrev =
|
||||
describe "prev method test"
|
||||
[test "prev of 01.01.2000" <|
|
||||
\() ->
|
||||
Expect.equal (31, 12, 1999) (prev (1, 1, 2000))
|
||||
, test "prev of 02.06.2024 " <|
|
||||
\() ->
|
||||
Expect.equal (1, 6, 2024) (prev (2, 6, 2024))
|
||||
, test "prev of 1.12.2020" <|
|
||||
\() ->
|
||||
Expect.equal (30, 11, 2020) (prev (1, 12, 2020))
|
||||
-- leap year test
|
||||
, test "prev of 28.02.2020" <|
|
||||
\() ->
|
||||
Expect.equal (27, 2, 2020) (prev (28, 2, 2020))
|
||||
, test "prev of 1.03.2020" <|
|
||||
\() ->
|
||||
Expect.equal (29, 2, 2020) (prev (1, 3, 2020))
|
||||
-- non-leap year test
|
||||
, test "prev of 1.03.2021" <|
|
||||
\() ->
|
||||
Expect.equal (28, 2, 2021) (prev (1, 3, 2021))
|
||||
]
|
||||
|
||||
leapYearTest : Test
|
||||
leapYearTest =
|
||||
|
@ -155,36 +106,6 @@ leapYearTest =
|
|||
Expect.equal False (leapyear 2022)
|
||||
]
|
||||
|
||||
subTest : Test
|
||||
subTest =
|
||||
describe "sub test"
|
||||
[test "sub 01.01.1970 01.01.1970" <|
|
||||
\() ->
|
||||
Expect.equal 0 (sub (1, 1, 1970) (1, 1, 1970))
|
||||
, test "sub 01.01.1970 02.01.1970" <|
|
||||
\() ->
|
||||
Expect.equal -1 (sub (1, 1, 1970) (2, 1, 1970))
|
||||
, test "sub 01.01.1970 01.02.1970" <|
|
||||
\() ->
|
||||
Expect.equal -31 (sub (1, 1, 1970) (1, 2, 1970))
|
||||
, test "sub 01.01.1970 01.01.1971" <|
|
||||
\() ->
|
||||
Expect.equal -365 (sub (1, 1, 1970) (1, 1, 1971))
|
||||
, test "sub 01.01.1970 01.01.1972" <|
|
||||
\() ->
|
||||
Expect.equal -730 (sub (1, 1, 1970) (1, 1, 1972))
|
||||
, test "sub 01.01.1970 01.01.1974" <|
|
||||
\() ->
|
||||
Expect.equal -1461 (sub (1, 1, 1970) (1, 1, 1974))
|
||||
-- explicit leap year 2020 test
|
||||
, test "sub 27.02.2020 01.03.2020" <|
|
||||
\() ->
|
||||
Expect.equal -3 (sub (27, 2, 2020) (1, 3, 2020))
|
||||
-- positive test
|
||||
, test "sub 01.01.1970 01.01.1969" <|
|
||||
\() ->
|
||||
Expect.equal 365 (sub (1, 1, 1970) (1, 1, 1969))
|
||||
]
|
||||
|
||||
|
||||
ltTest : Test
|
||||
|
|
Loading…
Reference in New Issue