> module Date (date) where > import Posix > import Time > import Type_Data > import Opts date takes the options and a number of seconds since the epoch and returns an IO String, where the string is the date formatted as the options dictate taking into account whether it ir less than half a year ago. For example Main> date [] 979238030 >>= putStr . show Jan 11 18:33 Main> date [] 839238030 >>= putStr . show Aug 5 1996 Main> date [] 839238030 >>= putStr . show Mon Aug 05 10:40:30 1996 > date :: MyOptions -> Integer -> IO String > date opts d = do curtime <- epochTime > caltime <- toCalendarTime $ (TOD d 0) {- 0 picoseconds -} > return $ display_date opts (d - toInteger curtime) caltime Does the hard work for date above > display_date :: MyOptions -> Integer -> CalendarTime -> String > display_date opts diff (CalendarTime year mon day hour min sec _ wd _ _ _ _) > = if full_time opts > then fulldate > else if diff > div seconds_per_year 2 > then olddate > else newdate > where seconds_per_minute = 60 > seconds_per_hour = 60 * seconds_per_minute > seconds_per_day = 24 * seconds_per_hour > seconds_per_year = 365 * seconds_per_day > olddate = month_day ++ " " ++ show year > newdate = month_day ++ ' ':hours_mins > fulldate = dayname ++ ' ':month_dayz ++ ' ':hours_mins_secs ++ ' ':show year > month = head $ drop (fromEnum mon) months_list > dayname = head $ drop (fromEnum wd) days_list > days = pad2 ' ' day > daysz = pad2 '0' day > month_day = month ++ ' ':days > month_dayz = month ++ ' ':daysz > hours = pad2 '0' hour > mins = pad2 '0' min > secs = pad2 '0' sec > hours_mins = hours ++ ':':mins > hours_mins_secs = hours_mins ++ ':':secs > months_list = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", > "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] > days_list = ["Sun", "Mon", "Tue", "Wed", > "Thu", "Fri", "Sat"] > pad2 c n = if n < 10 then c:show n else show n Old code, can't remember if it works fully but doesn't do timezones correctly. > {- > display_date :: MyOptions -> Integer -> Integer -> String > display_date opts d curtime > = if full_time opts > then fulldate > else if toInteger curtime > d + div seconds_per_year 2 > then olddate > else newdate > where seconds_per_minute = 60 > seconds_per_hour = 60 * seconds_per_minute > seconds_per_day = 24 * seconds_per_hour > seconds_per_year = 365 * seconds_per_day > seconds_per_4_years = seconds_per_year * 4 + seconds_per_day > seconds_per_100_years = seconds_per_4_years * 25 - seconds_per_day > seconds_per_400_years = seconds_per_100_years * 4 + seconds_per_day > d1 = d + 3 * seconds_per_100_years > + 17 * seconds_per_4_years > + seconds_per_year > (year400, d2) = divMod d1 seconds_per_400_years > (year100, d3) = divMod d2 seconds_per_100_years > (year4, d4) = divMod d3 seconds_per_4_years > (year, d5) = divMod d4 seconds_per_year > years = 1601 + 400*year400 + 100*year100 + 4*year4 + year > (d6, d7) = divMod d5 seconds_per_day > (day, month) = daymonth d6 months > md = show month ++ ' ':pad2 ' ' day > full_md = show month ++ ' ':pad2 '0' day > (hours', d8) = divMod d7 seconds_per_hour > (mins', secs') = divMod d8 seconds_per_minute > pad2 c n = if n < 10 then c:show n else show n > hours = pad2 '0' hours' > mins = pad2 '0' mins' > secs = pad2 '0' secs' > dayname = head $ drop (mod (fromInteger d6) 7) days > days = ["Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"] > days_feb = if (mod years 400 == 0) > || ((mod years 100 /= 0) && (mod years 4 == 0)) > then 29 else 28 > months = [(31, Jan), (days_feb, Feb), (31, Mar), (30, Apr), > (31, May), (30, Jun), (31, Jul), (31, Aug), > (30, Sep), (31, Oct), (30, Nov), (31, Dec)] > olddate = md ++ " " ++ show years > newdate = md ++ ' ':hours ++ ':':mins > fulldate = dayname ++ ' ':full_md ++ > ' ':hours ++ ':':mins ++ ':':secs ++ ' ':show years > daymonth :: (Num a, Ord a) => a -> [(a, Month)] -> (a, Month) > daymonth t ((d, m):dms) = if t > d then daymonth (t - d) dms > else (t + 1, m) > daymonth _ _ = (0, Jan) Last case should be error > -}