问题描述
我一直在使用Haskell的Date.Time模块来解析日期,如 12-4-1999
或 1-31-1999
。我试过:
I've been working with Haskell's Date.Time modules to parse a date like 12-4-1999
or 1-31-1999
. I tried:
parseDay :: String -> Day
parseDay s = readTime defaultTimeLocale "%m%d%Y" s
而且想想我的几个月和几天要有两位数,而不是1或2 ...
And I think it wants my months and days to have exactly two digits instead of 1 or 2...
这样做的正确方法是什么?
What's the proper way to do this?
此外,我想以这种格式打印出我的日子: 12/4/1999
Haskell的方式是什么?
Also, I'd like to print out my Day in this format: 12/4/1999
what's the Haskell way to?
感谢您的帮助。
推荐答案
这是一些包含两个类型的自制日期,日期只有YMD,没有时间或时区等。
Here is some old code that contains two types of homemade dates, dates with just YMD, no time or timezones, etc.
它显示如何使用 readDec
。请参阅 parseDate
函数。使用 readDec
,读取号码,对前导空格无关紧要(因为 filter
)或前导零,并且解析停止在第一个非数字。然后使用尾部
(跳过非数字)以进入日期的下一个数字字段。
It shows how to parse strings into dates using readDec
. See the parseDate
function. With readDec
, read the number, it doesn't matter about leading spaces(because of filter
) or leading zeros,and the parse stops at the first non-digit. Then used tail
(to skip the non digit) to get to the next numerical field of the date.
它显示了格式化输出的几种方式,但最灵活的方法是使用 Text.printf
。请参阅实例显示LtDate
。使用 printf ,任何事情都可以!
It shows several ways of formatting for output, but the most flexible way is to use Text.printf
. See instance Show LtDate
. With printf, anything is possible!
import Char
import Numeric
import Data.Time.Calendar
import Data.Time.Clock
import Text.Printf
-- ================================================================
-- LtDate
-- ================================================================
type Date=(Int,Int,Int)
data LtDate = LtDate
{ ltYear :: Int,
ltMonth:: Int,
ltDay :: Int
}
instance Show LtDate
where show d = printf "%4d-%02d-%02d" (ltYear d) (ltMonth d) (ltDay d)
toLtDate :: Date -> LtDate
toLtDate (y,m,d)= LtDate y m d
-- =============================================================
-- Date
-- =============================================================
-- | Parse a String mm/dd/yy into tuple (y,m,d)
-- accepted formats
--
-- @
-- 12\/01\/2004
-- 12\/ 1\' 4
-- 12-01-99
-- @
parseDate :: String -> Date
parseDate s = (y,m,d)
where [(m,rest) ] = readDec (filter (not . isSpace) s)
[(d,rest1)] = readDec (tail rest)
[(y, _) ] = parseDate' rest1
-- | parse the various year formats used by Quicken dates
parseDate':: String -> [(Int,String)]
parseDate' (y:ys) =
let [(iy,rest)] = readDec ys
year=case y of '\'' -> iy + 2000
_ ->
if iy < 1900 then iy + 1900 else iy
in [(year,rest)]
-- | Note some functions sort by this format
-- | So be careful when changing it.
showDate::(Int, Int, Int) -> String
showDate (y,m,d)= yy ++ '-':mm ++ '-':dd
where dd=zpad (show d)
mm = zpad (show m)
yy = show y
zpad ds@(_:ds')
| ds'==[] = '0':ds
| otherwise = ds
-- | from LtDate to Date
fromLtDate :: LtDate -> Date
fromLtDate lt = (ltYear lt, ltMonth lt, ltDay lt)
一旦你有(Y,M,D),很容易将其转换为Haskell库类型进行数据操作。完成HS库后,可以使用 Text.printf
格式化显示日期。
Once you have (Y,M,D), it's easy to convert to a Haskell library type for data manipulations. Once you are done with the HS libraries, Text.printf
can be used to format a date for display.
这篇关于Haskell日期解析和格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!