简体   繁体   English

DateTime类有什么意义?

[英]What is the point of the DateTime class?

The DateTime class seems redundant, and after reading this bit in the documentation for the Rails extension of the class, it also seems potentially dangerous: DateTime类似乎是多余的,在读取该类的Rails扩展的文档中的这一位后,它似乎也有潜在危险:

DateTimes aren't aware of DST rules, so use a consistent non-DST offset when creating a DateTime with an offset in the local zone DateTimes不知道DST规则,因此在本地区域中创建具有偏移量的DateTime时使用一致的非DST偏移量

There's also this, in the Rails documentation for DateTime#to_time : 还有这个,在DateTime#to_time的Rails文档中:

Attempts to convert self to a Ruby Time object; 尝试将self转换为Ruby Time对象; returns self if out of range of Ruby Time class. 如果超出Ruby Time类的范围,则返回self。 If self has an offset other than 0, self will just be returned unaltered, since there's no clean way to map it to a Time. 如果self的偏移量不是0,那么self将不会被返回,因为没有干净的方法将它映射到Time。

I assume that last part is due to DateTime not recognizing DST. 我假设最后一部分是由于DateTime没有识别DST。

It seems to me we have this: 在我看来,我们有这个:

  • The Date class represents a simple date without a time. Date类表示没有时间的简单日期。
  • The Time class represents a specific point in time, which implicitly includes the date. Time类表示特定时间点,隐含地包括日期。
  • The DateTime class is the same as the Time class, but doesn't understand DST and sometimes can't convert to a regular Time class. DateTime类与Time类相同,但不了解DST,有时无法转换为常规Time类。

So should DateTime just be banished from the code base or does it serve a useful purpose which I am missing? 那么, DateTime是否应该从代码库中消失,还是它有助于我缺少一个有用的目的?

DateTime had an advantage over Time on 32 bit machines in Rubies < 1.9.2 - Time was a victim of the Y2K38 problem and limited to a 32 bit range. Rubies <1.9.2中, DateTime在32位机器上具有优于Time的优势 - TimeY2K38问题的受害者,并且限制在32位范围内。 This problem is solved either on 64 bit machines and/or in recent Ruby versions. 在64位计算机和/或最近的Ruby版本中解决了这个问题。 You still may need to use DateTime if for example 1.8 compatibility is a must or you rely on using methods from its API which often deviates from that of Time . 您仍然可能需要使用DateTime ,例如,1.8兼容性是必须的,或者您依赖于使用其API的方法,这些方法通常与Time

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM