How do I use ruby Date constants GREGORIAN, JULIAN, ENGLAND and even ITALY

做~自己de王妃 提交于 2019-12-02 14:41:33

问题


'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)> 

回答1:


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:

  • http://en.wikipedia.org/wiki/Julian_calendar
  • http://en.wikipedia.org/wiki/Gregorian_calendar



回答2:


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:


In the western world, it is common to express a date using a year, a month, and a day. This would be a perfect way to express any day in history if the definition of our calendar wouldn't have changed in the past.

The calendar as we know today was introduced by Julius Cesar and is thus known as the Julian calendar. Unfortunately it had a tiny flaw. The Julian calendar uses the concept of a leap year every four years to compensate for the fact that a year doesn't have exactly 365 but around 365.25 days. Yet this isn't correct; a year is in fact a bit shorter and if you add a leap year every four years, this error will sum up over hundreds of years and sooner or later you will be off by whole days. Today we have a leap year every four years, but not if the year is dividable by 100, unless it is also dividable by 1000. This is done to approximate 365.2425 days a year. The reformation is called the Gregorian calendar after the pope Gregor XIII who introduced it. And to make things even more complicated, the Gregorian calendar wasn't introduced in every country at the same time.

When creating a Date object in Ruby, you can use one of four constants:

Date.new(2019, 6, 29, Date::ITALY)
Date.new(2019, 6, 29, Date::ENGLAND)
Date.new(2019, 6, 29, Date::GREGORIAN)
Date.new(2019, 6, 29, Date::JULIAN)

If you use Date::ITALY, which is the default if you don't specify anything at all, then dates before 1582-10-15 are interpreted as dates of the Julian calendar, all other dates are interpreted as dates of the Gregorian one. 1582-10-15 is the date when Italy introduced the Gregorian calendar.

If you use Date::ENGLAND, then dates before 1752-09-14 are interpreted as dates of the Julian calendar, all other dates are interpreted as dates of the Gregorian one. 1752-09-14 is the date when the British Empire, and thus also the American colonies, introduced the Gregorian calendar.

If you use Date::GREGORIAN, the Date object will behave as if the Gregorian calendar had always been in place, no matter which date. All dates, even dates prior to the reformation are interpreted as Gregorian dates.

If you use Date::JULIAN, the Date object will behave as if this date reformation had never existed. All dates are interpreted according the the Julian calendar.

What is the correct behavior? Well, there is none. Different standards suggest different solutions. If you need to process historical dates, you need to know according to which calendar these dates should be interpreted. And as you may require a different reformation date for that, you can also pass your own integer value instead of these predefined constant. This number is then interpreted as the Julian day number at that the switch to the Gregorian calendar took place.



来源:https://stackoverflow.com/questions/11835193/how-do-i-use-ruby-date-constants-gregorian-julian-england-and-even-italy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!