Purescript library to handle the formatting of dates and times.
1-- SPDX-License-Identifier: MIT
2-- Copyright (C) 2022 Roland Csaszar
3--
4-- Project: purescript-datetimeformat
5-- File: CalendarFormat.purs
6-- Date: 12.Feb.2022
7--
8-- ==============================================================================
9-- | Module Data.CalendarFormat, the type of the format of a calendar.
10module Data.CalendarFormat
11 ( CalendarFormat(..)
12 , calendarFormatToString
13 , toString
14 ) where
15
16import Prelude
17import Data.Argonaut (class DecodeJson, class EncodeJson)
18import Data.Argonaut.Decode.Generic (genericDecodeJson)
19import Data.Argonaut.Encode.Generic (genericEncodeJson)
20import Data.Generic.Rep (class Generic)
21import Data.Show.Generic (genericShow)
22import Test.QuickCheck (class Arbitrary, arbitrary)
23
24{-------------------------------------------------------------------------------
25| The calendar format.
26|
27| The calendar "islamicc" is deprecated, use `IslamicCivil` ("islamic-civil")
28| instead.
29|
30| See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/calendar#unicode_calendar_keys
31|
32| One of
33| * Buddhist - Thai Buddhist calendar
34| * Chinese - Traditional Chinese calendar
35| * Coptic - Coptic calendar
36| * Dangi - Traditional Korean calendar
37| * Ethioaa - Ethiopic calendar, Amete Alem (epoch approx. 5493 B.C.E)
38| * Ethiopic - Ethiopic calendar, Amete Mihret (epoch approx, 8 C.E.)
39| * Gregory - Gregorian calendar
40| * Hebrew - Traditional Hebrew calendar
41| * Indian - Indian calendar
42| * Islamic - Islamic calendar
43| * IslamicUmalqura - Islamic calendar, Umm al-Qura
44| * IslamicTbla - Islamic calendar, tabular (intercalary years [2,5,7,10,13,16,18,21,24,26,29] - astronomical epoch)
45| * IslamicCivil - Islamic calendar, tabular (intercalary years [2,5,7,10,13,16,18,21,24,26,29] - civil epoch)
46| * IslamicRgsa - Islamic calendar, Saudi Arabia sighting
47| * Iso8601 - ISO calendar (Gregorian calendar using the ISO 8601 calendar week rules)
48| * Japanese - Japanese Imperial calendar
49| * Persian - Persian calendar
50| * Roc - Civil (algorithmic) Arabic calendar
51-}
52data CalendarFormat
53 = Buddhist
54 | Chinese
55 | Coptic
56 | Dangi
57 | Ethioaa
58 | Ethiopic
59 | Gregory
60 | Hebrew
61 | Indian
62 | Islamic
63 | IslamicUmalqura
64 | IslamicTbla
65 | IslamicCivil
66 | IslamicRgsa
67 | Iso8601
68 | Japanese
69 | Persian
70 | Roc
71
72{-------------------------------------------------------------------------------
73| Return the string representation of a `CalendarFormat`.
74|
75| A shorter alias for `calendarFormatToString`.
76-}
77toString ∷ CalendarFormat → String
78toString = calendarFormatToString
79
80{-------------------------------------------------------------------------------
81| Return the string representation of a `CalendarFormat`.
82-}
83calendarFormatToString :: CalendarFormat -> String
84calendarFormatToString Buddhist = "buddhist"
85
86calendarFormatToString Chinese = "chinese"
87
88calendarFormatToString Coptic = "coptic"
89
90calendarFormatToString Dangi = "dangi"
91
92calendarFormatToString Ethioaa = "ethioaa"
93
94calendarFormatToString Ethiopic = "ethiopic"
95
96calendarFormatToString Gregory = "gregory"
97
98calendarFormatToString Hebrew = "hebrew"
99
100calendarFormatToString Indian = "indian"
101
102calendarFormatToString Islamic = "islamic"
103
104calendarFormatToString IslamicUmalqura = "islamic-umalqura"
105
106calendarFormatToString IslamicTbla = "islamic-tbla"
107
108calendarFormatToString IslamicCivil = "islamic-civil"
109
110calendarFormatToString IslamicRgsa = "islamic-rgsa"
111
112calendarFormatToString Iso8601 = "iso8601"
113
114calendarFormatToString Japanese = "japanese"
115
116calendarFormatToString Persian = "persian"
117
118calendarFormatToString Roc = "roc"
119
120derive instance eqCalendarFormat :: Eq CalendarFormat
121
122derive instance ordCalendarFormat :: Ord CalendarFormat
123
124derive instance genericCalendarFormat :: Generic CalendarFormat _
125
126instance decodeJsonCalendarFormat :: DecodeJson CalendarFormat where
127 decodeJson = genericDecodeJson
128
129instance encodeJsonCalendarFormat :: EncodeJson CalendarFormat where
130 encodeJson = genericEncodeJson
131
132instance showCalendarFormat :: Show CalendarFormat where
133 show = genericShow
134
135{-------------------------------------------------------------------------------
136 ATTENTION: 18 is the number of values of `CalendarFormat`.
137-}
138instance arbitraryCalendarFormat :: Arbitrary CalendarFormat where
139 arbitrary = map intToCalendarFormat arbitrary
140 where
141 intToCalendarFormat :: Int -> CalendarFormat
142 intToCalendarFormat n
143 | n >= 0 = case n `mod` 18 of
144 0 -> Buddhist
145 1 -> Chinese
146 2 -> Coptic
147 3 -> Dangi
148 4 -> Ethioaa
149 5 -> Ethiopic
150 6 -> Gregory
151 7 -> Hebrew
152 8 -> Indian
153 9 -> Islamic
154 10 -> IslamicUmalqura
155 11 -> IslamicTbla
156 12 -> IslamicCivil
157 13 -> IslamicRgsa
158 14 -> Iso8601
159 15 -> Japanese
160 16 -> Persian
161 _ -> Roc
162 | otherwise = intToCalendarFormat (-n)