module Data.Time.Calendar.Julian
(
module Data.Time.Calendar.JulianYearDay,
toJulian,fromJulian,fromJulianValid,showJulian,julianMonthLength,
addJulianMonthsClip,addJulianMonthsRollOver,
addJulianYearsClip,addJulianYearsRollOver
) where
import Data.Time.Calendar.MonthDay
import Data.Time.Calendar.JulianYearDay
import Data.Time.Calendar.Days
import Data.Time.Calendar.Private
toJulian :: Day -> (Integer,Int,Int)
toJulian date = (year,month,day) where
(year,yd) = toJulianYearAndDay date
(month,day) = dayOfYearToMonthAndDay (isJulianLeapYear year) yd
fromJulian :: Integer -> Int -> Int -> Day
fromJulian year month day = fromJulianYearAndDay year (monthAndDayToDayOfYear (isJulianLeapYear year) month day)
fromJulianValid :: Integer -> Int -> Int -> Maybe Day
fromJulianValid year month day = do
doy <- monthAndDayToDayOfYearValid (isJulianLeapYear year) month day
fromJulianYearAndDayValid year doy
showJulian :: Day -> String
showJulian date = (show4 y) ++ "-" ++ (show2 m) ++ "-" ++ (show2 d) where
(y,m,d) = toJulian date
julianMonthLength :: Integer -> Int -> Int
julianMonthLength year = monthLength (isJulianLeapYear year)
rolloverMonths :: (Integer,Integer) -> (Integer,Int)
rolloverMonths (y,m) = (y + (div (m 1) 12),fromIntegral (mod (m 1) 12) + 1)
addJulianMonths :: Integer -> Day -> (Integer,Int,Int)
addJulianMonths n day = (y',m',d) where
(y,m,d) = toJulian day
(y',m') = rolloverMonths (y,fromIntegral m + n)
addJulianMonthsClip :: Integer -> Day -> Day
addJulianMonthsClip n day = fromJulian y m d where
(y,m,d) = addJulianMonths n day
addJulianMonthsRollOver :: Integer -> Day -> Day
addJulianMonthsRollOver n day = addDays (fromIntegral d 1) (fromJulian y m 1) where
(y,m,d) = addJulianMonths n day
addJulianYearsClip :: Integer -> Day -> Day
addJulianYearsClip n = addJulianMonthsClip (n * 12)
addJulianYearsRollOver :: Integer -> Day -> Day
addJulianYearsRollOver n = addJulianMonthsRollOver (n * 12)