Class: When::BasicTypes::Date

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

Overview

ISO 8601 Date Representation

see xml schema

Class Method Summary collapse

Methods inherited from DateTime

_to_array

Methods inherited from String

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

Class Method Details

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

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

Raises:

  • (ArgumentError)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/when_exe/basictypes.rb', line 129

def _to_array_basic_ISO8601(date, options={})
  by, bm, bd = options[:abbr]
  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 /^(\d{2})$/                    ; 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 /^([-+]\d{6})(\d{2})(\d{2})$/  ; return nil,     [$1.to_i, $2.to_i, $3.to_i]          # 5.2.1.4 a)
  when /^([-+]\d{6})-(\d{2})$/        ; return nil,     [$1.to_i, $2.to_i]                   # 5.2.1.4 b)
  when /^([-+]\d{6})$/                ; return nil,     [$1.to_i]                            # 5.2.1.4 c)
  when /^([-+]\d{6})W(\d{2})(\d{1})?$/; return :week,   [$1.to_i, $2.to_i, _int($3)]         # 5.2.3.4 a-b)
  when /^([+]\d{4})$/                 ; return :century,[$1.to_i * 100]                      # 5.2.1.4 d)
  when /^([-]\d{4})$/                 ; return :century,[$1.to_i * 100]       unless by      # 5.2.1.4 d)
  when /^([-+]\d{6})(\d{3})$/         ; 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)


173
174
175
176
177
178
179
# File 'lib/when_exe/basictypes.rb', line 173

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



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
# File 'lib/when_exe/basictypes.rb', line 182

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



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/when_exe/basictypes.rb', line 213

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