How do I use ruby Date constants GREGORIAN, JULIAN

2019-03-03 15:04发布

'scuse the upper case, they are constants.

I am having fun learning ruby's Date helpers.

1.9.3p125 :057 > Date::ABBR_MONTHNAMES
 => [nil, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] 
1.9.3p125 :058 > Date::ABBR_DAYNAMES
 => ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] 
1.9.3p125 :059 > Date::MONTHNAMES
 => [nil, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] 
1.9.3p125 :060 > Date::DAYNAMES
 => ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] 
1.9.3p125 :070 > Date::MONTHNAMES[Time.new.month]
=> "August" 

Fun stuff! But what about the GREGORIAN, JULIAN, ENGLAND and ITALY (!) constants. What are they for / how do I use them? I can output:

1.9.3p125 :061 > Date::GREGORIAN
 => -Infinity 
1.9.3p125 :062 > Date::JULIAN
 => Infinity 
1.9.3p125 :063 > Date::ENGLAND
 => 2361222 

or

1.9.3p125 :067 > Date.new
 => #<Date: -4712-01-01 ((0j,0s,0n),+0s,2299161j)> 
1.9.3p125 :068 > Date.new.new_start(Date::JULIAN)
 => #<Date: -4712-01-01 ((0j,0s,0n),+0s,Infj)> 
1.9.3p125 :069 > Date.new.new_start(Date::ENGLAND)
 => #<Date: -4712-01-01 ((0j,0s,0n),+0s,2361222j)> 

From the following it looks like Julian is a calendar that is off by a few days. I remember learning about the calendar reset for that a few centuries ago so that makes sense, however the ENGLAND and ITALY and how they would be used is still unclear to me.

1.9.3p125 :076 > Date.new(1977,7,1).new_start(Date::ENGLAND)
 => #<Date: 1977-07-01 ((2443326j,0s,0n),+0s,2361222j)> 
1.9.3p125 :077 > Date.new(1977,7,1).new_start(Date::ITALY)
 => #<Date: 1977-07-01 ((2443326j,0s,0n),+0s,2299161j)> 
1.9.3p125 :078 > Date.new(1977,7,1).new_start(Date::JULIAN)
 => #<Date: 1977-06-18 ((2443326j,0s,0n),+0s,Infj)> 

2条回答
冷血范
2楼-- · 2019-03-03 15:30

You could go into the ext/date/date_core.c and just get there values. They are constants yes but you asked how to use them. Your question should be how to extract them. There usage is for calculating Date and DateTime objects given the data for creating the desired time and date.

For example this would be one that has elements with certain values.

#<DateTime: 2017-01-06T12:05:55+00:00 ((2457760j,43555s,553855002n),+0s,2299161j)>

Notice all the parts? Two of them have j after the number. Calculating those is what these constants are for and are part of the class object. There are many ways to use them and many types as well. A good read is the Julian Day on wikipedia. Take a look at the table for all those different values. The history is interesting too because they relate to what Michael has referred you to.

As far as the values you got back for two of them, notice that they are also classes. That is curious too because that is relating to how far forward or backward in time your system can crunch the numbers.

Infinity

查看更多
闹够了就滚
3楼-- · 2019-03-03 15:31

All the constants are explained in the documentation. As a rule of thumb, if the below explanations don't mean anything to you, you probably don't need to worry about those constants at all.

ENGLAND The Julian day number of the day of calendar reform for England and her colonies.

GREGORIAN The Julian day number of the day of calendar reform for the proleptic Gregorian calendar

ITALY The Julian day number of the day of calendar reform for Italy and some catholic countries.

JULIAN The Julian day number of the day of calendar reform for the proleptic Julian calendar

Here's more info on the different calendar systems:

查看更多
登录 后发表回答