今日の日付を文字列で取得する。"2009/06/03"

Haskell には System.Time と Data.Time のふたつがあるようだ。


Prelude> :m + System.Time
Prelude System.Time> getClockTime
Wed Jun 3 09:45:04 東京 (標準時) 2009

getClockTime で取得した値を CalendarTime に。

 
Prelude System.Time> tm <- getClockTime
Prelude System.Time> toCalendarTime tm
CalendarTime {ctYear = 2009, ctMonth = June, ctDay = 3, ctHour = 9, ctMin = 45,
ctSec = 48, ctPicosec = 456107000000, ct
WDay = Wednesday, ctYDay = 153,
ctTZName = "\147\140\139\158 (\149W\143\128\142\158)",
ctTZ = 32400, ctIsDST = False}

getClockTime で取得した値を UTCTime に。

 
Prelude System.Time> toUTCTime tm
CalendarTime {ctYear = 2009, ctMonth = June, ctDay = 3, ctHour = 0, ctMin = 45,
ctSec = 48, ctPicosec = 456107000000, ctWDay = Wednesday, ctYDay = 153,
ctTZName = "UTC", ctTZ = 0, ctIsDST = False}

こっちは Data.Time


Prelude> :m + Data.Time
Prelude Data.Time> :t getCurrentTime
getCurrentTime :: IO UTCTime
Prelude Data.Time> getCurrentTime
2009-06-03 00:47:48.476746 UTC

Data.Time の東京 (標準時)


Prelude Data.Time> :t getZonedTime
getZonedTime :: IO ZonedTime

Prelude Data.Time> getCurrentTimeZone
東京 (標準時)

Prelude Data.Time> getZonedTime
2009-06-03 09:50:15.2163775 東京 (標準時)

今日の日付を文字列で取得する。"2009/06/03"


import Data.Time

dateString :: IO [Char]
dateString = do
zonedTm <- getZonedTime
return $ replace '-' '/' $ take 10 $ show zonedTm

-- replace from to xs
replace :: (Eq a) => a -> a -> [a] -> [a]
replace _ _ [ ] = [ ]
replace from to (x:xs) | from == x = to : (replace from to xs)
| otherwise = x : (replace from to xs)

*Japanese> :load Japanese.hs
[1 of 1] Compiling Japanese ( Japanese.hs, interpreted )
Ok, modules loaded: Japanese.
*Japanese> dateString
"2009/06/03"