diff options
Diffstat (limited to 'src/CosmicCalendar.hs')
-rw-r--r-- | src/CosmicCalendar.hs | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/src/CosmicCalendar.hs b/src/CosmicCalendar.hs new file mode 100644 index 0000000..db29c02 --- /dev/null +++ b/src/CosmicCalendar.hs | |||
@@ -0,0 +1,140 @@ | |||
1 | {-# OPTIONS_GHC | ||
2 | -Wall | ||
3 | -Wno-unused-imports | ||
4 | -Wno-unused-top-binds | ||
5 | -Wno-name-shadowing | ||
6 | #-} | ||
7 | {-# language NoImplicitPrelude #-} | ||
8 | {-# language RecordWildCards #-} | ||
9 | {-# language FlexibleContexts #-} | ||
10 | {-# language TemplateHaskell #-} | ||
11 | {-# language ViewPatterns #-} | ||
12 | {-# language OverloadedStrings #-} | ||
13 | {-# language QuasiQuotes #-} | ||
14 | |||
15 | module CosmicCalendar where | ||
16 | |||
17 | import Rebase.Prelude | ||
18 | import qualified Rebase.Prelude as Prelude | ||
19 | import Control.Lens hiding ((<|)) | ||
20 | import Data.Foldable (toList) | ||
21 | import Data.Ratio | ||
22 | import Text.Printf | ||
23 | import Data.Time.Calendar.OrdinalDate | ||
24 | import Data.Time.LocalTime | ||
25 | import Control.Monad.RWS | ||
26 | import Data.Time.Calendar.OrdinalDate | ||
27 | import Data.Text.Format.Numbers | ||
28 | import NeatInterpolation | ||
29 | import qualified Data.Text as Text | ||
30 | import Data.Text (Text, pack, unpack) | ||
31 | |||
32 | import Rebase.Data.Map.Strict (Map) | ||
33 | import qualified Rebase.Data.Map.Strict as Map | ||
34 | |||
35 | -- 13.787±0.020 billion years. Source: https://en.wikipedia.org/wiki/Age_of_the_universe#cite_note-Planck_2018-2 | ||
36 | ageOfUniverseInYears :: Integer | ||
37 | ageOfUniverseInYears = 13787 * 1000 * 1000 | ||
38 | |||
39 | -- The point of the cosmic calendar is to mentally visualize or model the scale | ||
40 | -- of cosmic/geologic/evolutionary events using the existing internal mental | ||
41 | -- model of the year. This internal mental model of the year is NOT a 365.2422 | ||
42 | -- day earth rotation, but a 365 day calendar year. | ||
43 | -- | ||
44 | -- In order to make the math of the calendar work out, the functions that look | ||
45 | -- up calendary entries (and take as input the LocalTime) must check for leap | ||
46 | -- year, and subtract one day from the input if it is later than February. | ||
47 | daysPerYear :: NominalDiffTime | ||
48 | daysPerYear = 365 -- NOT 365.2422 | ||
49 | |||
50 | lengthOfDay :: NominalDiffTime | ||
51 | lengthOfDay = 24 * 60 * 60 | ||
52 | |||
53 | lengthOfYear :: NominalDiffTime | ||
54 | lengthOfYear = daysPerYear * lengthOfDay | ||
55 | |||
56 | ageOfUniverse :: NominalDiffTime | ||
57 | ageOfUniverse = fromIntegral ageOfUniverseInYears * lengthOfYear | ||
58 | |||
59 | data CalendarEntry = CalendarEntry { | ||
60 | calBeginTime :: NominalDiffTime, | ||
61 | calEndTime :: Maybe NominalDiffTime, | ||
62 | calTitle :: Text, | ||
63 | calSubtitle :: Text, | ||
64 | calDescription :: Text, | ||
65 | calReferences :: Text | ||
66 | } deriving (Show) | ||
67 | |||
68 | -- TODO: Encode the input times like so: | ||
69 | -- | ||
70 | -- data CosmicTime = YearsAgo Rational | YearsAfterBigBang Rational | YearsBCE Rational | YearsCE Rational | ||
71 | -- | ||
72 | -- The absolute time values (YearsBCE and YearsCE) will be computed using the | ||
73 | -- year at program start: | ||
74 | |||
75 | currentYear :: Integer | ||
76 | currentYear = unsafePerformIO $ getZonedTime <&> toGregorian . localDay . zonedTimeToLocalTime <&> view _1 | ||
77 | |||
78 | years :: Rational -> NominalDiffTime | ||
79 | years = (* lengthOfYear) . fromRational | ||
80 | |||
81 | yearsAgo :: Rational -> NominalDiffTime | ||
82 | yearsAgo (fromRational -> n) = lengthOfYear * (1 - (n / fromIntegral ageOfUniverseInYears)) | ||
83 | |||
84 | afterBigBang :: NominalDiffTime -> NominalDiffTime | ||
85 | afterBigBang = (/ ageOfUniverse) . (* lengthOfYear) | ||
86 | |||
87 | thousandYears :: Rational -> NominalDiffTime | ||
88 | thousandYears = years . (* 1000) | ||
89 | |||
90 | millionYears :: Rational -> NominalDiffTime | ||
91 | millionYears = thousandYearsAgo . (* 1000) | ||
92 | |||
93 | billionYears :: Rational -> NominalDiffTime | ||
94 | billionYears = millionYearsAgo . (* 1000) | ||
95 | |||
96 | thousandYearsAgo :: Rational -> NominalDiffTime | ||
97 | thousandYearsAgo = yearsAgo . (* 1000) | ||
98 | |||
99 | millionYearsAgo :: Rational -> NominalDiffTime | ||
100 | millionYearsAgo = thousandYearsAgo . (* 1000) | ||
101 | |||
102 | billionYearsAgo :: Rational -> NominalDiffTime | ||
103 | billionYearsAgo = millionYearsAgo . (* 1000) | ||
104 | |||
105 | yearStart :: LocalTime -> LocalTime | ||
106 | yearStart (LocalTime d _) = LocalTime d' t' | ||
107 | where | ||
108 | d' = fromGregorian y 1 1 | ||
109 | t' = TimeOfDay 0 0 0 | ||
110 | (y, _, _) = toGregorian d | ||
111 | |||
112 | localTimeToYearElapsed :: LocalTime -> NominalDiffTime | ||
113 | localTimeToYearElapsed t = t `diffLocalTime` yearStart t | ||
114 | |||
115 | getPreviousCalendarEntry :: Calendar -> LocalTime -> Maybe CalendarEntry | ||
116 | getPreviousCalendarEntry cal (localTimeToYearElapsed -> t) = snd <$> Map.lookupLT t cal | ||
117 | |||
118 | getCurrentCalendarEntry :: Calendar -> LocalTime -> Maybe CalendarEntry | ||
119 | getCurrentCalendarEntry cal (localTimeToYearElapsed -> t) = snd <$> Map.lookupLE t cal | ||
120 | |||
121 | type Calendar = Map NominalDiffTime CalendarEntry | ||
122 | |||
123 | getNextCalendarEntry :: Calendar -> LocalTime -> Maybe CalendarEntry | ||
124 | getNextCalendarEntry cal (localTimeToYearElapsed -> t) = snd <$> Map.lookupGT t cal | ||
125 | |||
126 | buildCalendar :: [CalendarEntry] -> Map NominalDiffTime CalendarEntry | ||
127 | buildCalendar ls = Map.fromList $ map (\x -> (calBeginTime x, x)) $ map unwrap ls | ||
128 | |||
129 | unwrap :: CalendarEntry -> CalendarEntry | ||
130 | unwrap x@CalendarEntry{..} = x { calDescription = unwrapText calDescription } | ||
131 | where | ||
132 | unwrapText :: Text -> Text | ||
133 | unwrapText = pack . unlines . map unwords . foldr process [] . lines . unpack | ||
134 | process line [] = [[line]] | ||
135 | process line ((x:xs):ys) | shouldMerge line x = (line:x:xs):ys | ||
136 | process line rest = [line]:rest | ||
137 | shouldMerge :: String -> String -> Bool | ||
138 | shouldMerge "" _ = False | ||
139 | shouldMerge _ "" = False | ||
140 | shouldMerge _ _ = True | ||