1 module Data.Gargantext.Utils.Chronos where
3 import Data.Maybe (fromJust, isJust, isNothing)
4 import qualified Data.Time as DT
5 import qualified Data.UTC as DU
8 import Data.Time.Clock.POSIX
11 parseDate :: String -> Maybe [String]
12 parseDate d = matchRegex (mkRegex "(.*)/(.*)/(.*)") d
14 getDate' :: Maybe [String] -> (Integer, Int, Int)
16 | isJust d == True = toGregorian $ fromGregorian (read year) (read month) (read day)
17 | otherwise = toGregorian $ fromGregorian 2015 1 1
19 Just [day, month, year] = d
21 getDate :: String -> (Integer, Int, Int)
22 getDate = getDate' . parseDate
24 --getDateDay :: Maybe [String] -> Day
25 --getDateDay d = fromGregorian (read year) (read month) (read day)
26 -- where Just [day, month, year] = matchRegex (mkRegex "(.*)/(.*)/(.*)") d
28 getDateDay' :: Maybe [String] -> Day
30 | isJust d == True = fromGregorian (read year) (read month) (read day)
31 | otherwise = fromGregorian 2015 1 1
32 where Just [day, month, year] = d
34 getDateDay :: String -> Day
35 getDateDay = getDateDay' . parseDate
37 getDateUTC :: String -> String
38 getDateUTC d = show $ DT.UTCTime (getDateDay d) (DT.timeOfDayToTime $ DT.TimeOfDay 0 0 0)
40 getYear :: String -> String
41 getYear date = s where
42 (y, m, d) = getDate date
45 getMonth :: String -> String
46 getMonth date = s where
47 (y, m, d) = getDate date
50 getDay :: String -> String
52 (y, m, d) = getDate date
55 --for Dates exported via xls2csv tool
56 type MT = Maybe (DU.Local DU.DateTime)
57 type MS = Maybe String
59 --getDate'' :: String -> String
60 --getDate'' gd = d where
61 -- start = "1900-01-01T00:00:00Z"
62 -- da = (DU.parseRfc3339 start :: MT) >>= DU.addDays ( (read gd :: Integer) -2) >>= DU.renderRfc3339 :: MS
65 --getDate''' :: String -> String
66 --getDate''' gd = d where
67 -- start = "1900-01-01T00:00:00Z"
68 -- da = (DU.parseRfc3339 start :: MT) >>= DU.addDays ( (read gd :: Integer) -2) >>= DU.renderIso8601CalendarDate :: MS
71 --date2greg :: String ->
72 date2greg date = (y, m, d) where
73 (y, m, d) = DT.toGregorian $ DT.addDays ((read date :: Integer) -2) $ DT.utctDay (read "1900-01-01 00:00:00" :: DT.UTCTime)
76 getYear' :: String -> String
77 getYear' date = s where
78 (y, m, d) = date2greg date
82 getMonth' :: String -> String
83 getMonth' date = s where
84 (y, m, d) = date2greg date
88 getDay' :: String -> String
89 getDay' date = s where
90 (y, m, d) = date2greg date