如何理解 strptime 与 strftime [英] How to understand strptime vs. strftime
问题描述
strptime
和 strftime
有什么区别?我看到 strptime
是 DateTime
类中的一个方法,而 strftime
是 Time
类中的一个方法.
What's the difference between strptime
and strftime
? I see that strptime
is a method in the DateTime
class, and strftime
is a method in the Time
class.
Time
和DateTime
除了核心方法不同之外还有什么区别?Ruby 文档中Time
类的解释很有帮助,但是DateTime
只是说日期时间".还有Date
类,表示它提供Date
和DateTime
.帮助我理解这一点.- 我看到
strptime
并且我想把它发音为strip time",但这没有意义.有没有好的助记器? strptime
和strftime
到底是什么意思?- 你怎么记得哪个是做什么的?
- What's the difference between
Time
andDateTime
, other than that they have different core methods? The explanation for theTime
class in the Ruby docs is helpful, but the one forDateTime
just says "datetime". There's also theDate
class, which says it providesDate
andDateTime
. Help me make sense of this. - I see
strptime
and I want to pronounce it "strip time", but that doesn't make sense. Is there a good mnemonic-device for it? - What do
strptime
andstrftime
mean, anyway? - How do you remember which does what?
推荐答案
Time
和 DateTime
之间的区别与实现有关.大量的 DateTime
功能来自 Rails 世界,并且是带有时间的任意日期.它更像是一个基于日历的系统.Time
以 UTC 时间 1970 年 1 月 1 日以来的秒数为单位,与时区无关.在某些系统上,它仅限于 1901 和 2038 之间的值,这是传统上如何将此值存储为有符号 32 位整数的限制,但较新版本的 Ruby 可以处理更广泛的范围,使用 64 位值或 BigNum根据需要.
The difference between Time
and DateTime
has to do with implementation. A large amount of the DateTime
functionality comes from the Rails world and is an arbitrary date with time of day. It's more of a calendar-based system. Time
is measured as seconds since January 1, 1970 UTC and is time-zone agnostic. On some systems it is limited to values between 1901 and 2038, a limitation of how traditionally this value is stored as a signed 32-bit integer, but newer versions of Ruby can handle a much wider range, using a 64-bit value or BigNum as required.
简而言之,DateTime
是您从 Rails 中的数据库中获得的,而 Time 是 Ruby 传统上使用的.如果您正在处理日期很重要的值,并且您想知道诸如月末或六周前的哪一天之类的信息,请使用 DateTime.如果您只是测量经过的时间而不关心它,请使用 Time.如有必要,它们之间很容易转换.
In short, DateTime
is what you get from a database in Rails where Time is what Ruby has traditionally used. If you're working with values where dates are important and you want to know things like the end of the month or what day it'll be six weeks ahead, use DateTime. If you're just measuring elapsed time and don't care about that, use Time. They're easy to convert between if necessary.
Date
另一方面只是一个日历日期,没有任何关联的时间.您可能想在与时间无关的地方使用这些.
Date
on the other hand is just a calendar date and doesn't have any associated times. You might want to use these where times are irrelevant.
strptime
是解析时间"的缩写,其中 strftime
是格式化时间"的缩写.也就是说,strptime
与 strftime
正好相反,尽管它们使用相同的格式规范很方便.我很少看到使用 strptime
,因为 DateTime.parse
通常很擅长了解正在发生的事情,但是如果你真的需要把它拼出来,一定要使用遗留解析器.
strptime
is short for "parse time" where strftime
is for "formatting time". That is, strptime
is the opposite of strftime
though they use, conveniently, the same formatting specification. I've rarely seen strptime
used since DateTime.parse
is usually good at picking up on what's going on, but if you really need to spell it out, by all means use the legacy parser.
这篇关于如何理解 strptime 与 strftime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!