Module: ThirdBase::CompatClassMethods

Included in:
Date, DateTime
Defined in:
lib/third_base/make_compat.rb

Overview

Compatibility class methods for Date and DateTime, necessary because ruby doesn’t support multiple inheritance.

Instance Method Summary collapse

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



40
41
42
43
# File 'lib/third_base/make_compat.rb', line 40

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.utc_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)



46
47
48
# File 'lib/third_base/make_compat.rb', line 46

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)



51
52
53
# File 'lib/third_base/make_compat.rb', line 51

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)



56
57
58
# File 'lib/third_base/make_compat.rb', line 56

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.



61
62
63
# File 'lib/third_base/make_compat.rb', line 61

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.



66
67
68
# File 'lib/third_base/make_compat.rb', line 66

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.



71
72
73
74
75
76
# File 'lib/third_base/make_compat.rb', line 71

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.

Returns:

  • (Boolean)


79
80
81
# File 'lib/third_base/make_compat.rb', line 79

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)



91
92
93
# File 'lib/third_base/make_compat.rb', line 91

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.



96
97
98
# File 'lib/third_base/make_compat.rb', line 96

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.



101
102
103
# File 'lib/third_base/make_compat.rb', line 101

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



107
108
109
# File 'lib/third_base/make_compat.rb', line 107

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



112
113
114
# File 'lib/third_base/make_compat.rb', line 112

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.



117
118
119
# File 'lib/third_base/make_compat.rb', line 117

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.



122
123
124
# File 'lib/third_base/make_compat.rb', line 122

def jd_to_wday(j)
  jd(j).wday
end

#julian?(jd, sg) ⇒ Boolean

Returns true if jd is less than sg, false otherwise.

Returns:

  • (Boolean)


127
128
129
# File 'lib/third_base/make_compat.rb', line 127

def julian?(jd, sg)
  jd < sg
end

#julian_leap?(y) ⇒ Boolean

All years divisible by 4 are leap years in the Julian calendar.

Returns:

  • (Boolean)


132
133
134
# File 'lib/third_base/make_compat.rb', line 132

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



138
139
140
# File 'lib/third_base/make_compat.rb', line 138

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.

Returns:

  • (Boolean)


85
86
87
# File 'lib/third_base/make_compat.rb', line 85

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



143
144
145
# File 'lib/third_base/make_compat.rb', line 143

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.



148
149
150
# File 'lib/third_base/make_compat.rb', line 148

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.



153
154
155
# File 'lib/third_base/make_compat.rb', line 153

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.

Returns:

  • (Boolean)


158
159
160
# File 'lib/third_base/make_compat.rb', line 158

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.

Returns:

  • (Boolean)


165
166
167
# File 'lib/third_base/make_compat.rb', line 165

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

Returns:

  • (Boolean)


170
171
172
# File 'lib/third_base/make_compat.rb', line 170

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.

Returns:

  • (Boolean)


175
176
177
# File 'lib/third_base/make_compat.rb', line 175

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.

Returns:

  • (Boolean)


180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/third_base/make_compat.rb', line 180

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.



194
195
196
197
198
199
200
201
# File 'lib/third_base/make_compat.rb', line 194

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