2011-04-20 82 views
4

我有兩個問題:當前年和月中的天數

如何在haskell中獲得當前年份(不是日期)?

我怎樣才能獲得某一個月的數量天數?例如04.2011已經有30天了。

回答

4

這會給你的年,月,日:

import Data.Time.Clock 
import Data.Time.Calendar 

date :: IO (Integer,Int,Int) -- :: (year,month,day) 
date = getCurrentTime >>= return . toGregorian . utctDay 

來源:

有一個在Date.Time.Calendar稱爲gregorianMonthLength型整型功能 - >詮釋 - >詮釋。 需要一年和一個月的時間作爲參數,返回天數,就像你需要的一樣。 http://www.haskell.org/ghc/docs/7.0.2/html/libraries/time-1.2.0.3/Data-Time-Calendar.html

完整的例子:

import Data.Time.Clock 
import Data.Time.Calendar 

date :: IO (Integer,Int,Int) -- :: (year,month,day) 
date = getCurrentTime >>= return . toGregorian . utctDay 

main = do 
    (year, month, day) <- date 
    putStrLn $ "Days in this month: " ++ (show $ gregorianMonthLength year month) 
+0

THX非常:) – mrquestion 2011-04-20 13:07:55

+6

你看到'>>任何時間=回報.'你應該表示懷疑。這就是「你應該使用fmap」的成語。 'date = fmap(toGregorian。utctDay)getCurrentTime'或者在Control.Applicative導入時,'date = toGregorian。 utctDay <$> getCurrentTime'。 – Carl 2011-04-20 20:04:03

相關問題