如何理解 strptime 与 strftime [英] How to understand strptime vs. strftime

查看:59
本文介绍了如何理解 strptime 与 strftime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

strptimestrftime 有什么区别?我看到 strptimeDateTime 类中的一个方法,而 strftimeTime 类中的一个方法.

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.

  1. TimeDateTime 除了核心方法不同之外还有什么区别?Ruby 文档中 Time 类的解释很有帮助,但是 DateTime 只是说日期时间".还有 Date 类,表示它提供 DateDateTime.帮助我理解这一点.
  2. 我看到 strptime 并且我想把它发音为strip time",但这没有意义.有没有好的助记器?
  3. strptimestrftime 到底是什么意思?
  4. 你怎么记得哪个是做什么的?
  1. What's the difference between Time and DateTime, other than that they have different core methods? The explanation for the Time class in the Ruby docs is helpful, but the one for DateTime just says "datetime". There's also the Date class, which says it provides Date and DateTime. Help me make sense of this.
  2. 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?
  3. What do strptime and strftime mean, anyway?
  4. How do you remember which does what?

推荐答案

TimeDateTime 之间的区别与实现有关.大量的 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 是格式化时间"的缩写.也就是说,strptimestrftime 正好相反,尽管它们使用相同的格式规范很方便.我很少看到使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆