当月和每月的数量天数

mrq*_*ion 6 time haskell date

我有两个问题:

我怎样才能获得haskell的当前年份(不是日期)?

我怎样才能在某个月某个月获得数量天数?例如04.2011已经有30天了.

Tom*_*mas 5

这将为您提供年,月和日:

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

date :: IO (Integer,Int,Int) -- :: (year,month,day)
date = getCurrentTime >>= return . toGregorian . utctDay
Run Code Online (Sandbox Code Playgroud)

资料来源:http : //www.haskell.org/haskellwiki/Date

Date.Time.Calendar中有一个名为gregorianMonthLength的函数,类型为Integer-> Int-> Int。需要一年零一个月作为参数,并根据需要返回天数。 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)
Run Code Online (Sandbox Code Playgroud)

  • 每当您看到`&gt;&gt; = return .`时,您都应该感到可疑。这就是“您应该使用fmap”的成语。使用date = fmap(toGregorian。utctDay)getCurrentTime或在导入Control.Applicative的情况下使用date = toGregorian。utctDay &lt;$&gt; getCurrentTime`。 (9认同)