-- #hide
module Data.Time.Calendar.JulianYearDay
(
-- * Year and day format
module Data.Time.Calendar.JulianYearDay
) where
import Data.Time.Calendar.Days
import Data.Time.Calendar.Private
-- | convert to proleptic Julian year and day format. First element of result is year (proleptic Julian calendar),-- second is the day of the year, with 1 for Jan 1, and 365 (or 366 in leap years) for Dec 31.toJulianYearAndDay :: Day -> (Integer,Int)
toJulianYearAndDay (ModifiedJulianDaymjd) = (year,yd) where
a = mjd+678577quad = diva1461d = moda1461y = min (divd365) 3yd = fromInteger (d- (y*365) +1)
year = quad*4+y+1-- | convert from proleptic Julian year and day format.-- Invalid day numbers will be clipped to the correct range (1 to 365 or 366).fromJulianYearAndDay :: Integer -> Int -> DayfromJulianYearAndDayyearday = ModifiedJulianDaymjd where
y = year-1mjd = (fromIntegral (clip1 (if isJulianLeapYearyear then 366 else 365) day)) + (365*y) + (divy4) -678578-- | convert from proleptic Julian year and day format.-- Invalid day numbers will return NothingfromJulianYearAndDayValid :: Integer -> Int -> MaybeDayfromJulianYearAndDayValidyearday = do
day' <- clipValid1 (if isJulianLeapYearyear then 366 else 365) day
let
y = year-1mjd = (fromIntegralday') + (365*y) + (divy4) -678578return (ModifiedJulianDaymjd)
-- | show in proleptic Julian year and day format (yyyy-ddd)showJulianYearAndDay :: Day -> StringshowJulianYearAndDaydate = (show4 (Just'0') y) ++"-"++ (show3 (Just'0') d) where
(y,d) = toJulianYearAndDaydate-- | Is this year a leap year according to the proleptic Julian calendar?isJulianLeapYear :: Integer -> BoolisJulianLeapYearyear = (modyear4==0)