Class: When::BasicTypes::Date

Inherits:
DateTime show all
Defined in:
lib/when_exe/basictypes.rb

Overview

ISO 8601 Date Representation

see xml schema

Constant Summary collapse

Extra_Year_Digits =
Hash.new {|hash, key|
  ex = (key || 1).to_i
  hash[key] =
    if ex > 0
      [
        /\A(\d{2})\z/,
        /\A([-+]\d{#{4+ex}})(\d{2})(\d{2})\z/,
        /\A([-+]\d{#{4+ex}})-(\d{2})\z/,
        /\A([-+]\d{#{4+ex}})\z/,
        /\A([-+]\d{#{4+ex}})W(\d{2})(\d{1})?\z/,
        /\A([+]\d{#{2+ex}})\z/,
        /\A([-]\d{#{2+ex}})\z/,
        /\A([-+]\d{#{4+ex}})(\d{3})\z/
      ]
    elsif ex == 0
      [/\A(\d{2})\z/] + [/\A(\d{4})\z/] * 6
    else
      [/\A(\d{4})\z/] * 7
    end
}
Ordinal_Date_Digits =
Hash.new {|hash, key|
  hash[key] = /\A(\d{#{(key || 3).to_i}})\z/
}

Class Method Summary collapse

Methods inherited from DateTime

_to_array

Methods inherited from String

#^, #calendar, #calendar_era, #calendar_note, #clock, #encode, #era, #ideographic_unification, #m17n, #ord, #resource, #to_m17n, #to_month_name, #to_pair, #to_r, #to_residue, #translate, #when?

Methods included from EncodingConversion

#+@, #-@, #to_external_encoding, #to_internal_encoding

Class Method Details

._to_array_basic_ISO8601(date, options = {}) ⇒ Object

ISO 8601 基本形式の表現を分解して配列化する

Raises:

  • (ArgumentError)


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/when_exe/basictypes.rb', line 182

def _to_array_basic_ISO8601(date, options={})
  by, bm, bd = options[:abbr]
  extra_reg  = Extra_Year_Digits[options[:extra_year_digits]]
  case date
  when nil                            ; return nil
  when /\A(\d{4})(\d{2})(\d{2})\z/    ; return nil,     [$1.to_i, $2.to_i, $3.to_i]          # 5.2.1.1
  when /\A(\d{4})-(\d{2})\z/          ; return nil,     [$1.to_i, $2.to_i]                   # 5.2.1.2
  when /\A(\d{4})\z/                  ; return nil,     [$1.to_i]                            # 5.2.1.2
  when extra_reg[0]                   ; return :century,[$1.to_i * 100]                      # 5.2.1.2
  when /\A(\d{4})(\d{3})\z/           ; return :day,    [$1.to_i, $2.to_i]                   # 5.2.2.1
  when /\A(\d{4})W(\d{2})(\d{1})?\z/  ; return :week,   [$1.to_i, $2.to_i, _int($3)]         # 5.2.3.1-2
  when extra_reg[1]                   ; return nil,     [$1.to_i, $2.to_i, $3.to_i]          # 5.2.1.4 a)
  when extra_reg[2]                   ; return nil,     [$1.to_i, $2.to_i]                   # 5.2.1.4 b)
  when extra_reg[3]                   ; return nil,     [$1.to_i]                            # 5.2.1.4 c)
  when extra_reg[4]                   ; return :week,   [$1.to_i, $2.to_i, _int($3)]         # 5.2.3.4 a-b)
  when extra_reg[5]                   ; return :century,[$1.to_i * 100]                      # 5.2.1.4 d)
  when extra_reg[6]                   ; return :century,[$1.to_i * 100]       unless by      # 5.2.1.4 d)
  when extra_reg[7]                   ; return :day,    [$1.to_i, $2.to_i]                   # 5.2.2.3 a)
  else                                ; raise ArgumentError, "Wrong date format" unless by
  end

  by = by.to_i
  case date
  when /\A(\d{2})(\d{2})(\d{2})\z/    ; return nil,     [_century($1,by), $2.to_i, $3.to_i]  # 5.2.1.3 a)
  when /\A-(\d{2})(\d{2})?\z/         ; return nil,     [_century($1,by), _int($2)]          # 5.2.1.3 b-c)
  when /\A--(\d{2})(\d{2})?\z/        ; return nil,     [by, $1.to_i,     _int($2)]          # 5.2.1.3 d-e)
  when /\A(\d{2})(\d{3})\z/           ; return :day,    [_century($1,by), $2.to_i]           # 5.2.2.2 a)
  when /\A-(\d{3})\z/                 ; return :day,    [by, $1.to_i]                        # 5.2.2.2 b)
  when /\A(\d{2})W(\d{2})(\d{1})?\z/  ; return :week,   [_century($1,by), $2.to_i, _int($3)] # 5.2.3.3 a-b)
  when /\A-(\d{1})W(\d{2})(\d{1})?\z/ ; return :week,   [_decade($1,by),  $2.to_i, _int($3)] # 5.2.3.3 c-d)
  when /\A-W(\d{2})(\d{1})?\z/        ; return :week,   [by, $1.to_i,     _int($2)]          # 5.2.3.3 e-f)
  else                                ; raise ArgumentError, "Wrong date format" unless bm
  end

  bm = bm.to_i
  case date
  when /\A---(\d{2})\z/               ; return nil,     [by, bm, $1.to_i]                    # 5.2.1.3 f)
  when /\A-W-(\d{1})\z/               ; return :week,   [by, bm, $1.to_i]                    # 5.2.3.3 g)
  when /\A----\z/                     ; return nil,     [by, bm, bd.to_i] if bd              # extension
  end

  raise ArgumentError, "Wrong date format: #{date}"
end

._to_array_basic_X0301(date, options = {}) ⇒ Object

JIS X0301 基本形式の表現を分解して配列化する

Raises:

  • (ArgumentError)


227
228
229
230
231
232
233
# File 'lib/when_exe/basictypes.rb', line 227

def _to_array_basic_X0301(date, options={})
  raise ArgumentError, "Wrong date format" unless date =~ /\./
  date.scan(/\d+\./) do |part|
    raise ArgumentError, "Wrong date format" unless part.length == 3
  end
  _to_array_basic_ISO8601(date.gsub(/\./, ''), {:abbr=>options[:abbr]||1})
end

._to_array_extended_ISO8601(date, options = {}) ⇒ Object

ISO 8601 拡張形式の表現を分解して配列化する



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/when_exe/basictypes.rb', line 236

def _to_array_extended_ISO8601(date, options={})
  return nil unless date
  abbr = Array(options[:abbr]).dup
  unless date =~ /\A([-+]?\d+#{abbr[0] ? '|-' : ''})([-*=.])?(.*)/
    raise ArgumentError, "Wrong date format: #{date}"
  end
  date = $3
  dd   = [Coordinates::Pair._en_pair(_completion($1, abbr.shift), $2)]
  while date =~ /\A(\d+#{abbr[0] ? '|-' : ''})([-+*&%@!>=<?.])(.+)/
    date = $3
    dd << Coordinates::Pair._en_pair(_completion($1, abbr.shift), $2)
  end
  case date
  when /\AW(\d+#{abbr[0] ? '|-' : ''})([-+*&%@!>=<?.])?(\d+)?([-*=?%@])?\z/
    dd << Coordinates::Pair._en_pair(_completion($1, abbr.shift), $2)
    dd << Coordinates::Pair._en_pair($3, $4)
    return :week, dd
  when Ordinal_Date_Digits[options[:ordinal_date_digits]]
    dd << $1.to_i
    return :day, dd
  when /\A(\d+#{abbr[0] ? '|-' : ''})([-+*&%@!>=<?.])?\z/
    dd << Coordinates::Pair._en_pair(_completion($1, abbr.shift), $2)
    return nil, dd
  when ''
    return nil, dd
  else
   raise ArgumentError, "Wrong date format: #{date}"
  end
end

._to_array_extended_X0301(date, era, options = {}) ⇒ Object

JIS X0301 拡張形式の表現を分解して配列化する



267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/when_exe/basictypes.rb', line 267

def _to_array_extended_X0301(date, era, options={})
  if (date =~ /\A([-+\d]+)\(([-+\d]+)\)(.*)\z/)
    year = $2
    date = $1 + $3
  end
  format, date  = _to_array_extended_ISO8601(date, options)
  if (year)
    yy   = year.to_i
    ee   = date[0] * 1
    era  = [era, yy-ee, yy+ee]
  end
  return format, date, era
end