Module: ThirdBase::CompatClassMethods
- Defined in:
- lib/third_base/compat.rb
Overview
Compatibility class methods for Date and DateTime, necessary because ruby doesn’t support multiple inheritance.
Instance Method Summary collapse
-
#_parse(str, comp = false) ⇒ Object
Return the parts of the parsed date as a hash witht he following keys:.
-
#ajd_to_amjd(ajd) ⇒ Object
Converts an Astronomical Julian Date to an Astronomical Modified Julian Date (substracts an integer from ajd).
-
#ajd_to_jd(ajd, of = 0) ⇒ Object
Converts an Astronomical Julian Date to a Julian Date (returns ajd, ignores of).
-
#amjd_to_ajd(amjd) ⇒ Object
Converts an Astronomical Modified Julian Date to an Astronomical Julian Date (adds an integer to amjd).
-
#civil_to_jd(year, mon, day, sg = nil) ⇒ Object
Returns the julian date for the given civil date arguments, ignores sg.
-
#commercial_to_jd(cwyear, cweek, cwday, sg = nil) ⇒ Object
Returns the julian date for the given commercial date arguments, ignores sg.
-
#day_fraction_to_time(fr) ⇒ Object
Returns the fraction of the date as an array of hours, minutes, seconds, and fraction on a second.
-
#gregorian?(jd, sg) ⇒ Boolean
True if jd is greater than sg, false otherwise.
-
#jd_to_ajd(j, fr, of = 0) ⇒ Object
Converts a Julian Date to an Astronomical Julian Date (returns j, ignores fr and of).
-
#jd_to_civil(j, sg = nil) ⇒ Object
Returns [year, month, day] for the given julian date, ignores sg.
-
#jd_to_commercial(j, sg = nil) ⇒ Object
Returns [cwyear, cweek, cwday] for the given julian date, ignores sg.
-
#jd_to_ld(j) ⇒ Object
Converts a julian date to the number of days since the adoption of the gregorian calendar in Italy (subtracts an integer from j).
-
#jd_to_mjd(j) ⇒ Object
Convert a Julian Date to a Modified Julian Date (subtracts an integer from j).
-
#jd_to_ordinal(j, sg = nil) ⇒ Object
Returns [year, yday] for the given julian date, ignores sg.
-
#jd_to_wday(j) ⇒ Object
Returns the day of week for the given julian date.
-
#julian?(jd, sg) ⇒ Boolean
Returns true if jd is less than sg, false otherwise.
-
#julian_leap?(y) ⇒ Boolean
All years divisible by 4 are leap years in the Julian calendar.
-
#ld_to_jd(ld) ⇒ Object
Converts a number of days since the adoption of the gregorian calendar in Italy to a julian date (adds an integer to ld).
-
#leap?(y) ⇒ Boolean
(also: #gregorian_leap?)
All years divisible by 4 are leap years in the Gregorian calendar, except for years divisible by 100 and not by 400.
-
#mjd_to_jd(mjd) ⇒ Object
Converts a Modified Julian Date to a Julian Date (adds an integer to mjd).
-
#ordinal_to_jd(year, yday, sg = nil) ⇒ Object
Converts the given year and day of year to a julian date, ignores sg.
-
#time_to_day_fraction(h, min, s) ⇒ Object
Converts the given hour, minute, and second to a fraction of a day as a Float.
-
#valid_civil?(year, mon, day, sg = nil) ⇒ Boolean
(also: #valid_date?)
Return the julian date of the given year, month, and day if valid, or nil if not, ignores sg.
-
#valid_commercial?(cwyear, cweek, cwday, sg = nil) ⇒ Boolean
Return the julian date of the given commercial week year, commercial week, and commercial week day if valid, or nil if not, ignores sg.
-
#valid_jd?(jd, sg = nil) ⇒ Boolean
Returns the julian date if valid (always returns jd, ignores sg).
-
#valid_ordinal?(year, yday, sg = nil) ⇒ Boolean
Return the julian date of the given year and day of year if valid, or nil if not, ignores sg.
-
#valid_time?(h, min, s) ⇒ Boolean
Returns the fraction of the day if the time is valid, or nil if the time is not valid.
-
#zone_to_diff(zone) ⇒ Object
Converts a time zone string to a offset from UTC in seconds.
Instance Method Details
#_parse(str, comp = false) ⇒ Object
Return the parts of the parsed date as a hash witht he following keys:
-
:hour : hour
-
:mday : day of month
-
:min : minute
-
:mon : month
-
:offset : time zone offset from UTC in seconds
-
:sec : second
-
:sec_fraction : fraction of a second
-
:year : year
-
:zone : time zone offset as string
43 44 45 46 |
# File 'lib/third_base/compat.rb', line 43 def _parse(str, comp=false) d = DateTime.parse(str) {:mon=>d.mon, :zone=>d.zone, :sec=>d.sec, :year=>d.year, :hour=>d.hour, :offset=>d.offset, :mday=>d.day, :min=>d.min, :sec_fraction=>d.usec/1000000.0}.reject{|k, v| d.not_parsed.include?(k)} end |
#ajd_to_amjd(ajd) ⇒ Object
Converts an Astronomical Julian Date to an Astronomical Modified Julian Date (substracts an integer from ajd)
49 50 51 |
# File 'lib/third_base/compat.rb', line 49 def ajd_to_amjd(ajd) ajd - MJD_JD end |
#ajd_to_jd(ajd, of = 0) ⇒ Object
Converts an Astronomical Julian Date to a Julian Date (returns ajd, ignores of)
54 55 56 |
# File 'lib/third_base/compat.rb', line 54 def ajd_to_jd(ajd, of=0) ajd end |
#amjd_to_ajd(amjd) ⇒ Object
Converts an Astronomical Modified Julian Date to an Astronomical Julian Date (adds an integer to amjd)
59 60 61 |
# File 'lib/third_base/compat.rb', line 59 def amjd_to_ajd(amjd) amjd + MJD_JD end |
#civil_to_jd(year, mon, day, sg = nil) ⇒ Object
Returns the julian date for the given civil date arguments, ignores sg.
64 65 66 |
# File 'lib/third_base/compat.rb', line 64 def civil_to_jd(year, mon, day, sg=nil) civil(year, mon, day).jd end |
#commercial_to_jd(cwyear, cweek, cwday, sg = nil) ⇒ Object
Returns the julian date for the given commercial date arguments, ignores sg.
69 70 71 |
# File 'lib/third_base/compat.rb', line 69 def commercial_to_jd(cwyear, cweek, cwday, sg=nil) commercial(cwyear, cweek, cwday).jd end |
#day_fraction_to_time(fr) ⇒ Object
Returns the fraction of the date as an array of hours, minutes, seconds, and fraction on a second.
74 75 76 77 78 79 |
# File 'lib/third_base/compat.rb', line 74 def day_fraction_to_time(fr) hours, fr = (fr * 24).divmod(1) minutes, fr = (fr * 60).divmod(1) seconds, sec_fract = (fr * 60).divmod(1) [hours, minutes, seconds, sec_fract] end |
#gregorian?(jd, sg) ⇒ Boolean
True if jd is greater than sg, false otherwise.
82 83 84 |
# File 'lib/third_base/compat.rb', line 82 def gregorian?(jd, sg) jd > sg end |
#jd_to_ajd(j, fr, of = 0) ⇒ Object
Converts a Julian Date to an Astronomical Julian Date (returns j, ignores fr and of)
94 95 96 |
# File 'lib/third_base/compat.rb', line 94 def jd_to_ajd(j, fr, of=0) j end |
#jd_to_civil(j, sg = nil) ⇒ Object
Returns [year, month, day] for the given julian date, ignores sg.
99 100 101 |
# File 'lib/third_base/compat.rb', line 99 def jd_to_civil(j, sg=nil) jd(j).send(:civil) end |
#jd_to_commercial(j, sg = nil) ⇒ Object
Returns [cwyear, cweek, cwday] for the given julian date, ignores sg.
104 105 106 |
# File 'lib/third_base/compat.rb', line 104 def jd_to_commercial(j, sg=nil) jd(j).send(:commercial) end |
#jd_to_ld(j) ⇒ Object
Converts a julian date to the number of days since the adoption of the gregorian calendar in Italy (subtracts an integer from j).
110 111 112 |
# File 'lib/third_base/compat.rb', line 110 def jd_to_ld(j) j - LD_JD end |
#jd_to_mjd(j) ⇒ Object
Convert a Julian Date to a Modified Julian Date (subtracts an integer from j).
115 116 117 |
# File 'lib/third_base/compat.rb', line 115 def jd_to_mjd(j) j - MJD_JD end |
#jd_to_ordinal(j, sg = nil) ⇒ Object
Returns [year, yday] for the given julian date, ignores sg.
120 121 122 |
# File 'lib/third_base/compat.rb', line 120 def jd_to_ordinal(j, sg=nil) jd(j).send(:ordinal) end |
#jd_to_wday(j) ⇒ Object
Returns the day of week for the given julian date.
125 126 127 |
# File 'lib/third_base/compat.rb', line 125 def jd_to_wday(j) jd(j).wday end |
#julian?(jd, sg) ⇒ Boolean
Returns true if jd is less than sg, false otherwise.
130 131 132 |
# File 'lib/third_base/compat.rb', line 130 def julian?(jd, sg) jd < sg end |
#julian_leap?(y) ⇒ Boolean
All years divisible by 4 are leap years in the Julian calendar.
135 136 137 |
# File 'lib/third_base/compat.rb', line 135 def julian_leap?(y) y % 4 == 0 end |
#ld_to_jd(ld) ⇒ Object
Converts a number of days since the adoption of the gregorian calendar in Italy to a julian date (adds an integer to ld).
141 142 143 |
# File 'lib/third_base/compat.rb', line 141 def ld_to_jd(ld) ld + LD_JD end |
#leap?(y) ⇒ Boolean Also known as: gregorian_leap?
All years divisible by 4 are leap years in the Gregorian calendar, except for years divisible by 100 and not by 400.
88 89 90 |
# File 'lib/third_base/compat.rb', line 88 def leap?(y) y % 4 == 0 && y % 100 != 0 || y % 400 == 0 end |
#mjd_to_jd(mjd) ⇒ Object
Converts a Modified Julian Date to a Julian Date (adds an integer to mjd).
146 147 148 |
# File 'lib/third_base/compat.rb', line 146 def mjd_to_jd(mjd) mjd + MJD_JD end |
#ordinal_to_jd(year, yday, sg = nil) ⇒ Object
Converts the given year and day of year to a julian date, ignores sg.
151 152 153 |
# File 'lib/third_base/compat.rb', line 151 def ordinal_to_jd(year, yday, sg=nil) ordinal(year, yday).jd end |
#time_to_day_fraction(h, min, s) ⇒ Object
Converts the given hour, minute, and second to a fraction of a day as a Float.
156 157 158 |
# File 'lib/third_base/compat.rb', line 156 def time_to_day_fraction(h, min, s) (h*3600 + min*60 + s)/86400.0 end |
#valid_civil?(year, mon, day, sg = nil) ⇒ Boolean Also known as: valid_date?
Return the julian date of the given year, month, and day if valid, or nil if not, ignores sg.
161 162 163 |
# File 'lib/third_base/compat.rb', line 161 def valid_civil?(year, mon, day, sg=nil) civil(year, mon, day).jd rescue nil end |
#valid_commercial?(cwyear, cweek, cwday, sg = nil) ⇒ Boolean
Return the julian date of the given commercial week year, commercial week, and commercial week day if valid, or nil if not, ignores sg.
168 169 170 |
# File 'lib/third_base/compat.rb', line 168 def valid_commercial?(cwyear, cweek, cwday, sg=nil) commercial(cwyear, cweek, cwday).jd rescue nil end |
#valid_jd?(jd, sg = nil) ⇒ Boolean
Returns the julian date if valid (always returns jd, ignores sg).
173 174 175 |
# File 'lib/third_base/compat.rb', line 173 def valid_jd?(jd, sg=nil) jd end |
#valid_ordinal?(year, yday, sg = nil) ⇒ Boolean
Return the julian date of the given year and day of year if valid, or nil if not, ignores sg.
178 179 180 |
# File 'lib/third_base/compat.rb', line 178 def valid_ordinal?(year, yday, sg=nil) ordinal(year, yday).jd rescue nil end |
#valid_time?(h, min, s) ⇒ Boolean
Returns the fraction of the day if the time is valid, or nil if the time is not valid.
183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/third_base/compat.rb', line 183 def valid_time?(h, min, s) h += 24 if h < 0 min += 60 if min < 0 s += 60 if s < 0 return unless ((0..23) === h && (0..59) === min && (0..59) === s) || (24 == h && 0 == min && 0 == s) time_to_day_fraction(h, min, s) end |
#zone_to_diff(zone) ⇒ Object
Converts a time zone string to a offset from UTC in seconds.
197 198 199 200 201 202 203 204 |
# File 'lib/third_base/compat.rb', line 197 def zone_to_diff(zone) if m = /\A([+-](?:\d{4}|\d\d:\d\d))\z/.match(zone) x = m[1].gsub(':','') x[0..2].to_i*3600 + x[3..4].to_i*60 else 0 end end |