来自《the ruby way (the second edition)》
Time、Date、Datetime 的转换和区别:
Ruby有三个处理日期和时间的基本类:Time、Date、Datetime,对它们的描述如下:
1.Time类主要是对底层的C库时间函数的封装。这些函数通常基于UNIX纪元,因此不能表示1970年前的时间。
2.Date类是为了弥补Time类的缺点创建的,它能轻松地处理较早的时间,如达芬奇的生日(1452年4月15日),还能够智能处理历法改革的日期。但它也有缺点,如不能处理达芬奇的出生时间,它只能处理日期。
3.DateTime类继承了Date,旨在比上述两者都好。它可以像Date那样表示日期,也可以像Time那样表示时间。它通常是表示日期——时间值的“正确”方式。
7.19. Interconverting Between Time, Date, and DateTime
Ruby has three basic classes dealing with dates and times: Time, Date, and DateTime. The following is a description of each:
-
The Time class is mostly a wrapper for the underlying time functions in the C library. These are typically based on the UNIX epoch and thus cannot represent times before 1970.
-
The Date class was created to address this shortcoming of the Time class. It can easily deal with older dates such as Leonardo da Vinci's birthday (April 15, 1452), and it is intelligent about the dates of calendar reform. But it has its own shortcoming; it can't deal with the time of day that Leonardo was born. It deals strictly with dates.
-
The DateTime class inherits from Date and tries to be the best of both worlds. It can represent dates as well as Date can, and times as well as Time can. This is often the "right" way to represent a date-time value.
But don't be fooled into thinking that a DateTime is just a Date with an embedded Time. There are, in fact, several methods missing from DateTime, such as usec, dst?, and others.
So we have these three classes. Unfortunately there is no good standard way to convert between them. As Ruby continues to change, some of these details will be ironed out. For now, these methods in Listing 7.2 will suffice. Thanks to Kirk Haines for providing them.
Listing 7.2. Interconverting Between Date and Time Classes
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/1327ab569c1ae82736693a50b8e33378.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Any exceptions will be propagated except NameError. Why do we check for this one? Because it is conceivable that the program doesn't do a require of the date library (remember that Date and DateTime are part of this standard library, not part of the core). In such a case, to_datetime and to_date will both return nil.