Haskell 日期解析和格式化
我一直在使用 Haskell 的 Date.Time 模块来解析像 12-4-1999
或 1-31-1999
这样的日期。我尝试过:
parseDay :: String -> Day
parseDay s = readTime defaultTimeLocale "%m%d%Y" s
而且我认为它希望我的月份和日期恰好有两位数而不是 1 或 2...
执行此操作的正确方法是什么?
另外,我想以这种格式打印我的一天:12/4/1999
Haskell 的方式是什么?
感谢您的帮助。
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
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?
Also, I'd like to print out my Day in this format: 12/4/1999
what's the Haskell way to?
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
Data.Time.Format
中的函数读取日期。我在下面包含了一个简单的程序,它以一种格式读入日期并以两种不同的格式写回该日期。要以单位数月份或天数读取,请在 % 和格式说明符之间放置一个连字符 (-)。换句话说,要解析 9-9-2012 等格式的日期,请在 % 和格式字符之间包含一个连字符。因此,要解析“9-9-2012”,您需要格式字符串“%-d-%-m-%Y”。注意:考虑到 Haskell 包的发展速度,这个答案有点过时了。也许是时候寻找更好的解决方案了。我不再写 Haskell,所以如果其他人创建了另一个答案,那就太好了,因为这个问题在 Google 结果中排名很高。
自 2017 年 7 月起,鼓励您使用 parseTimeOrError。代码变为:
.cabal 文件中的版本:
构建依赖:基础 >=4.9 && <4.10, time >= 1.6.0.1
截至 2014 年 8 月,最好从
System.Locale
包获取语言环境,而不是从 Haskell 1998区域设置包。考虑到这一点,上面的示例代码现在如下所示:
输出现在如下所示:
原始,2012 年 1 月 答案:
输出如下所示:
Data.Time.Format
可从time
包中获取。如果您需要解析个位数的月份或日期,换句话说,像 9-9-2012 这样的日期,则在 % 和格式字符之间包含一个连字符。因此,要解析“9-9-2012”,您需要格式字符串“%-d-%-m-%Y”。You can use the functions in
Data.Time.Format
to read in the dates. I've included a trivial program below that reads in a date in one format and writes that date back out in two different formats. To read in single-digit months or days, place a single hyphen (-) between the % and format specifier. In other words to parse dates formatted like 9-9-2012 then include a single hyphen between the % and the format characters. So to parse "9-9-2012" you would need the format string "%-d-%-m-%Y".Note: This answer is getting a bit long-in-the-tooth given the rate at which Haskell packages evolve. It might be time to look for a better solution. I'm no longer writing Haskell so it would be nice if someone else created another answer since this question ranks highly in Google results.
As of July 2017, you are encouraged to use parseTimeOrError. The code becomes:
The versions from the .cabal file:
build-depends: base >=4.9 && <4.10, time >= 1.6.0.1
As of August, 2014, the locale was best obtained from the
System.Locale
package rather than the Haskell 1998Locale
package. With that in mind, the sample code from above now reads:output now looks like this:
Original, January 2012 answer:
The output looks like this:
Data.Time.Format
is available from thetime
package. If you need to parse single-digit months or days, in other words dates like 9-9-2012 then include a single hyphen between the % and the format characters. So to parse "9-9-2012" you would need the format string "%-d-%-m-%Y".使用 %-d 和 %-m 而不是 %d 和 %m 意味着单位数日/月是可以的,即
这可能就是 sclv 的意思,但他的评论对我来说有点太神秘了。
Using %-d and %-m instead of %d and %m means single digit day/month is OK, i.e.
This may be what sclv meant, but his comment was a little too cryptic for me.
从最近开始,我建议使用 strptime 包来满足您的所有日期/时间解析需求。
Since recently, I'll advice to use strptime package for all your date/time parsing needs.
这是一些旧代码,其中包含两种类型的自制日期,仅包含 YMD 的日期,没有时间或时区等。
它展示了如何使用
readDec
将字符串解析为日期。请参阅parseDate
函数。使用readDec
,读取数字,前导空格(因为过滤器
)或前导零并不重要,并且解析停止于第一个非数字。然后使用tail
(跳过非数字)到达日期的下一个数字字段。它显示了几种输出格式设置的方法,但最灵活的方法是使用
Text.printf
。请参阅实例显示 LtDate
。有了printf,一切皆有可能!一旦有了 (Y,M,D),就可以轻松转换为 Haskell 库类型以进行数据操作。使用完 HS 库后,可以使用
Text.printf
来设置显示日期的格式。Here is some old code that contains two types of homemade dates, dates with just YMD, no time or timezones, etc.
It shows how to parse strings into dates using
readDec
. See theparseDate
function. WithreadDec
, read the number, it doesn't matter about leading spaces(because offilter
) or leading zeros,and the parse stops at the first non-digit. Then usedtail
(to skip the non digit) to get to the next numerical field of the date.It shows several ways of formatting for output, but the most flexible way is to use
Text.printf
. Seeinstance Show LtDate
. With printf, anything is possible!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.