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
      [
        /^(\d{2})$/,
        /^([-+]\d{#{4+ex}})(\d{2})(\d{2})$/,
        /^([-+]\d{#{4+ex}})-(\d{2})$/,
        /^([-+]\d{#{4+ex}})$/,
        /^([-+]\d{#{4+ex}})W(\d{2})(\d{1})?$/,
        /^([+]\d{#{2+ex}})$/,
        /^([-]\d{#{2+ex}})$/,
        /^([-+]\d{#{4+ex}})(\d{3})$/
      ]
    elsif ex == 0
      [/^(\d{2})$/] + [/^(\d{4})$/] * 6
    else
      [/^(\d{4})$/] * 7
    end
}

Class Method Summary collapse

Methods inherited from DateTime

_to_array

Methods inherited from String

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

Class Method Details

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

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

Raises:

  • (ArgumentError)

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/when_exe/basictypes.rb', line 152

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 /^(\d{4})(\d{2})(\d{2})$/    ; return nil,     [$1.to_i, $2.to_i, $3.to_i]          # 5.2.1.1
  when /^(\d{4})-(\d{2})$/          ; return nil,     [$1.to_i, $2.to_i]                   # 5.2.1.2
  when /^(\d{4})$/                  ; return nil,     [$1.to_i]                            # 5.2.1.2
  when extra_reg[0]                 ; return :century,[$1.to_i * 100]                      # 5.2.1.2
  when /^(\d{4})(\d{3})$/           ; return :day,    [$1.to_i, $2.to_i]                   # 5.2.2.1
  when /^(\d{4})W(\d{2})(\d{1})?$/  ; 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 /^(\d{2})(\d{2})(\d{2})$/    ; return nil,     [_century($1,by), $2.to_i, $3.to_i]  # 5.2.1.3 a)
  when /^-(\d{2})(\d{2})?$/         ; return nil,     [_century($1,by), _int($2)]          # 5.2.1.3 b-c)
  when /^--(\d{2})(\d{2})?$/        ; return nil,     [by, $1.to_i,     _int($2)]          # 5.2.1.3 d-e)
  when /^(\d{2})(\d{3})$/           ; return :day,    [_century($1,by), $2.to_i]           # 5.2.2.2 a)
  when /^-(\d{3})$/                 ; return :day,    [by, $1.to_i]                        # 5.2.2.2 b)
  when /^(\d{2})W(\d{2})(\d{1})?$/  ; return :week,   [_century($1,by), $2.to_i, _int($3)] # 5.2.3.3 a-b)
  when /^-(\d{1})W(\d{2})(\d{1})?$/ ; return :week,   [_decade($1,by),  $2.to_i, _int($3)] # 5.2.3.3 c-d)
  when /^-W(\d{2})(\d{1})?$/        ; 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 /^---(\d{2})$/               ; return nil,     [by, bm, $1.to_i]                    # 5.2.1.3 f)
  when /^-W-(\d{1})$/               ; return :week,   [by, bm, $1.to_i]                    # 5.2.3.3 g)
  when /^----$/                     ; 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)

197
198
199
200
201
202
203
# File 'lib/when_exe/basictypes.rb', line 197

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 拡張形式の表現を分解して配列化する


206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/when_exe/basictypes.rb', line 206

def _to_array_extended_ISO8601(date, options={})
  return nil unless date
  abbr = Array(options[:abbr]).dup
  unless date =~ /^([-+]?\d+#{abbr[0] ? '|-' : ''})([-*=.])?(.*)/
    raise ArgumentError, "Wrong date format: #{date}"
  end
  date = $3
  dd   = [Coordinates::Pair._en_pair(_completion($1, abbr.shift), $2)]
  while date =~ /^(\d+#{abbr[0] ? '|-' : ''})([-+*&%@!>=<?.])(.+)/
    date = $3
    dd << Coordinates::Pair._en_pair(_completion($1, abbr.shift), $2)
  end
  case date
  when /^W(\d+#{abbr[0] ? '|-' : ''})([-+*&%@!>=<?.])?(\d+)?([-*=?%@])?$/
    dd << Coordinates::Pair._en_pair(_completion($1, abbr.shift), $2)
    dd << Coordinates::Pair._en_pair($3, $4)
    return :week, dd
  when /^(\d{3})$/
    dd << $1.to_i
    return :day, dd
  when /^(\d+#{abbr[0] ? '|-' : ''})([-+*&%@!>=<?.])?$/
    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 拡張形式の表現を分解して配列化する


237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/when_exe/basictypes.rb', line 237

def _to_array_extended_X0301(date, era, options={})
  if (date =~ /^([-+\d]+)\(([-+\d]+)\)(.*)$/)
    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