class Date
Constants
- EXTENDED_ATTRIBUTES
- FORMATS
- PRECISION
- PRECISIONS
- SYMBOLS
Attributes
Public Class Methods
# File lib/edtf/date.rb 23 def edtf(input, options = {}) 24 edtf!(input, options) 25 rescue 26 nil 27 end
# File lib/edtf/date.rb 29 def edtf!(input, options = {}) 30 ::EDTF::Parser.new(options).parse!(input) 31 end
Public Instance Methods
# File lib/edtf/date.rb 258 def <=>(other) 259 case other 260 when ::Date 261 values <=> other.values 262 when EDTF::Interval, EDTF::Season, EDTF::Epoch 263 other.cover?(self) ? other.min <=> self : 0 264 else 265 nil 266 end 267 end
Provides precise Date
calculations for years, months, and days. The options
parameter takes a hash with any of these keys: :years
, :months
, :weeks
, :days
.
# File lib/edtf/date.rb 63 def advance(options) 64 original_advance(options).copy_extended_attributes(self) 65 end
# File lib/edtf/date.rb 97 def approximate 98 @approximate ||= EDTF::Uncertainty.new(nil, nil, nil, 8) 99 end
# File lib/edtf/date.rb 123 def approximate!(arguments = precision_filter) 124 approximate.uncertain!(arguments) 125 self 126 end
# File lib/edtf/date.rb 117 def approximate?(arguments = precision_filter) 118 approximate.uncertain?(arguments) 119 end
# File lib/edtf/date.rb 107 def certain!(arguments = precision_filter) 108 uncertain.certain!(arguments) 109 self 110 end
Returns a new Date
where one or more of the elements have been changed according to the options
parameter.
# File lib/edtf/date.rb 71 def change(options) 72 d = original_change(options) 73 EXTENDED_ATTRIBUTES.each do |attribute| 74 d.send("#{attribute}=", options[attribute] || send(attribute)) 75 end 76 d 77 end
Returns the Date’s EDTF
string.
# File lib/edtf/date.rb 174 def edtf 175 return "Y#{year}" if long_year? 176 177 v = values 178 s = FORMATS.take(v.length).zip(v).map { |f,d| f % d.abs } 179 s[0] = "-#{s[0]}" if year < 0 180 s = unspecified.mask(s) 181 182 unless (h = ua_hash).zero? 183 # 184 # To efficiently calculate the uncertain/approximate state we use 185 # the bitmask. The primary flags are: 186 # 187 # Uncertain: 1 - year, 2 - month, 4 - day 188 # Approximate: 8 - year, 16 - month, 32 - day 189 # 190 # Invariant: assumes that uncertain/approximate are not set for values 191 # not covered by precision! 192 # 193 y, m, d = s 194 195 # ?/~ if true-false or true-true and other false-true 196 y << SYMBOLS[:uncertain] if 3&h==1 || 27&h==19 197 y << SYMBOLS[:approximate] if 24&h==8 || 27&h==26 198 199 200 # combine if false-true-true and other m == d 201 if 7&h==6 && (48&h==48 || 48&h==0) || 56&h==48 && (6&h==6 || 6&h==0) 202 m[0,0] = '(' 203 d << ')' 204 else 205 case 206 # false-true 207 when 3&h==2 || 24&h==16 208 m[0,0] = '(' 209 m << ')' 210 211 # *-false-true 212 when 6&h==4 || 48&h==32 213 d[0,0] = '(' 214 d << ')' 215 end 216 217 # ?/~ if *-true-false or *-true-true and other m != d 218 m << SYMBOLS[:uncertain] if h!=31 && (6&h==2 || 6&h==6 && (48&h==16 || 48&h==32)) 219 m << SYMBOLS[:approximate] if h!=59 && (48&h==16 || 48&h==48 && (6&h==2 || 6&h==4)) 220 end 221 222 # ?/~ if *-*-true 223 d << SYMBOLS[:uncertain] if 4&h==4 224 d << SYMBOLS[:approximate] if 32&h==32 225 end 226 227 s = s.join('-') 228 s << SYMBOLS[:calendar] << calendar if calendar? 229 s 230 end
# File lib/edtf/date.rb 52 def initialize_copy(other) 53 original_initialize_copy(other) 54 copy_extended_attributes(other) 55 end
Returns true if this Date/Time has year precision and the year exceeds four digits.
# File lib/edtf/date.rb 284 def long_year? 285 year_precision? && year.abs > 9999 286 end
Returns the same date but with negated year.
# File lib/edtf/date.rb 277 def negate 278 change(:year => year * -1) 279 end
Returns an array of the next n days, months, or years depending on the current Date/Time’s precision.
# File lib/edtf/date.rb 238 def next(n = 1) 239 1.upto(n).map { |by| advance(PRECISIONS[precision] => by) } 240 end
# File lib/edtf/date.rb 136 def precise!(arguments = precision_filter) 137 approximate.certain!(arguments) 138 self 139 end
# File lib/edtf/date.rb 130 def precise?(arguments = precision_filter) 131 !approximate?(arguments) 132 end
Returns this Date’s precision.
# File lib/edtf/date.rb 81 def precision 82 @precision ||= :day 83 end
Sets this Date/Time’s precision to the passed-in value.
# File lib/edtf/date.rb 86 def precision=(precision) 87 precision = precision.to_sym 88 raise ArgumentError, "invalid precision #{precision.inspect}" unless PRECISION.include?(precision) 89 @precision = precision 90 update_precision_filter[-1] 91 end
Returns the Date
of the previous day, month, or year depending on the current Date/Time’s precision.
# File lib/edtf/date.rb 250 def prev(n = 1) 251 if n > 1 252 1.upto(n).map { |by| advance(PRECISIONS[precision] => -by) } 253 else 254 advance(PRECISIONS[precision] => -1) 255 end 256 end
Converts the Date
into a season.
# File lib/edtf/date.rb 169 def season 170 Season.new(self) 171 end
Returns false for Dates.
# File lib/edtf/date.rb 160 def season?; false; end
Returns true if the Date’s EDTF
string should be printed without timezone.
# File lib/edtf/date.rb 166 def skip_timezone?; !!@skip_timezone; end
# File lib/edtf/date.rb 152 def specified!(arguments = precision_filter) 153 unspecified.specified!(arguments) 154 self 155 end
# File lib/edtf/date.rb 244 def succ 245 advance(PRECISIONS[precision] => 1) 246 end
# File lib/edtf/date.rb 93 def uncertain 94 @uncertain ||= EDTF::Uncertainty.new 95 end
# File lib/edtf/date.rb 112 def uncertain!(arguments = precision_filter) 113 uncertain.uncertain!(arguments) 114 self 115 end
# File lib/edtf/date.rb 101 def unspecified 102 @unspecified ||= EDTF::Unspecified.new 103 end
# File lib/edtf/date.rb 145 def unspecified!(arguments = precision_filter) 146 unspecified.unspecified!(arguments) 147 self 148 end
Returns an array of the current year, month, and day values filtered by the Date/Time’s precision.
# File lib/edtf/date.rb 272 def values 273 precision_filter.map { |p| send(p) } 274 end
Protected Instance Methods
# File lib/edtf/date.rb 314 def copy_extended_attributes(other) 315 @uncertain = other.uncertain.dup 316 @approximate = other.approximate.dup 317 @unspecified = other.unspecified.dup 318 319 @calendar = other.calendar.dup if other.calendar? 320 @precision = other.precision 321 322 self 323 end
Private Instance Methods
# File lib/edtf/date.rb 295 def precision_filter 296 @precision_filter ||= update_precision_filter 297 end
# File lib/edtf/date.rb 291 def ua_hash 292 uncertain.hash + approximate.hash 293 end
# File lib/edtf/date.rb 299 def update_precision_filter 300 @precision_filter = case precision 301 when :year 302 [:year] 303 when :month 304 [:year,:month] 305 else 306 [:year,:month,:day] 307 end 308 end