Finished exercise Date.elm
parent
16a5cd7a5a
commit
f4a4f5633b
|
@ -46,21 +46,49 @@ toString (a,b,c) = (if a < 10 then "0" ++ String.fromInt(a) else String.fromInt(
|
||||||
else if c < 100 then "00" ++ String.fromInt(c)
|
else if c < 100 then "00" ++ String.fromInt(c)
|
||||||
else if c < 1000 then "0" ++ String.fromInt(c)
|
else if c < 1000 then "0" ++ String.fromInt(c)
|
||||||
else String.fromInt(c))
|
else String.fromInt(c))
|
||||||
-- TT.MM.YYYY
|
|
||||||
|
|
||||||
|
-- https://web.archive.org/web/20170507133619/https://alcor.concordia.ca/~gpkatch/gdate-algorithm.html
|
||||||
|
|
||||||
|
convertDateToDayNumber : Date -> Int
|
||||||
|
convertDateToDayNumber (a, b, c) =
|
||||||
|
let m = (modBy 12 (b + 9))
|
||||||
|
y = c - m // 10
|
||||||
|
in 365 * y + y // 4 - y // 100 + y // 400 + (m * 306 + 5) // 10 + ( a - 1 )
|
||||||
|
|
||||||
|
convertDayNumberToDate : Int -> Date
|
||||||
|
convertDayNumberToDate g =
|
||||||
|
let y = (10000 * g + 14780) // 3652425
|
||||||
|
ddd = g - (365 * y + y // 4 - y // 100 + y // 400)
|
||||||
|
in
|
||||||
|
if (ddd < 0) then
|
||||||
|
let y2 = y - 1 -- y = y2
|
||||||
|
ddd2 = g - (365 * y2 + y2 // 4 - y2 // 100 + y2 // 400) -- ddd = ddd2
|
||||||
|
mi = (100 * ddd2 + 52) // 3060
|
||||||
|
mm = (modBy (12) (mi + 2)) + 1
|
||||||
|
y3 = y2 + (mi + 2) // 12 -- y = y3
|
||||||
|
dd = ddd2 - (mi * 306 + 5) // 10 + 1
|
||||||
|
in (dd, mm, y3)
|
||||||
|
else
|
||||||
|
let mi = (100 * ddd + 52) // 3060
|
||||||
|
mm = (modBy (12) (mi + 2)) + 1
|
||||||
|
y2 = y + (mi + 2) // 12 -- y = y2
|
||||||
|
dd = ddd - (mi * 306 + 5) // 10 + 1
|
||||||
|
in (dd, mm, y2)
|
||||||
|
|
||||||
|
-- 1 Punkt:
|
||||||
|
leapyear : Year -> Bool
|
||||||
|
leapyear y = ((modBy 4 y == 0) && (modBy 100 y /= 0)) || (modBy 400 y == 0)
|
||||||
|
|
||||||
|
|
||||||
-- 2 Punkte:
|
-- 2 Punkte:
|
||||||
next : Date -> Date
|
next : Date -> Date
|
||||||
next date = (1, 1, 1)
|
next date = convertDayNumberToDate ((convertDateToDayNumber date) + 1)
|
||||||
|
|
||||||
-- 2 Punkte:
|
-- 2 Punkte:
|
||||||
prev : Date -> Date
|
prev : Date -> Date
|
||||||
prev date = (1, 1, 1)
|
prev date = convertDayNumberToDate ((convertDateToDayNumber date) - 1)
|
||||||
|
|
||||||
-- 1 Punkt:
|
|
||||||
leapyear : Year -> Bool
|
|
||||||
leapyear y = False
|
|
||||||
|
|
||||||
-- 2 Punkte:
|
-- 2 Punkte:
|
||||||
sub : Date -> Date -> Int
|
sub : Date -> Date -> Int
|
||||||
sub date1 date2 = 0
|
sub date1 date2 = convertDateToDayNumber date1 - convertDateToDayNumber date2
|
||||||
|
|
|
@ -155,10 +155,6 @@ leapYearTest =
|
||||||
Expect.equal False (leapyear 2022)
|
Expect.equal False (leapyear 2022)
|
||||||
]
|
]
|
||||||
|
|
||||||
-- sub a b soll die Anzahl der Tage liefern, die zwischen den Daten a und b liegt. Wenn a
|
|
||||||
-- vor b liegt, soll das Ergebnis negativ sein, sonst positiv (außer a und b sind gleich, dann soll
|
|
||||||
-- das Ergebnis 0 sein).
|
|
||||||
|
|
||||||
subTest : Test
|
subTest : Test
|
||||||
subTest =
|
subTest =
|
||||||
describe "sub test"
|
describe "sub test"
|
||||||
|
|
Loading…
Reference in New Issue