Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/edi4r/edifact.rb

Overview

Here we extend class Time by some methods that help us maximize its use in the UN/EDIFACT context.

Basic idea:

  • Use the EDIFACT qualifiers of DE 2379 in DTM directly to parse dates and to create them upon output.

  • Use augmented Time objects as values of DE 2380 instead of strings

Currently supported formats: 101, 102, 201, 203, 204

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#formatObject

Returns the value of attribute format


157
158
159
# File 'lib/edi4r/edifact.rb', line 157

def format
  @format
end

Class Method Details

.edifact(str, fmt = 102) ⇒ Object


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
195
196
# File 'lib/edi4r/edifact.rb', line 159

def Time.edifact(str, fmt=102)
  msg = "Time.edifact: #{str} does not match format #{fmt}"
  case fmt.to_s
  when '101'
    rc = str =~ /(\d\d)(\d\d)(\d\d)(.+)?/
    raise msg unless rc and rc==0; warn msg if $4
    year = $1.to_i
    year += (year < 69) ? 2000 : 1900 # See ParseDate
    dtm = Time.local(year, $2, $3)

  when '102'
    rc = str =~ /(\d\d\d\d)(\d\d)(\d\d)(.+)?/
    raise msg unless rc and rc==0; warn msg if $4
    dtm = Time.local($1, $2, $3)

  when '201'
    rc = str =~ /(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(.+)?/
    raise msg unless rc and rc==0; warn msg if $6
    year = $1.to_i
    year += (year < 69) ? 2000 : 1900 # See ParseDate
    dtm = Time.local(year, $2, $3, $4, $5)

  when '203'
    rc = str =~ /(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(.+)?/
    raise msg unless rc and rc==0; warn msg if $6
    dtm = Time.local($1, $2, $3, $4, $5)

  when '204'
    rc = str =~ /(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(.+)?/
    raise msg unless rc and rc==0; warn msg if $7
    dtm = Time.local($1, $2, $3, $4, $5, $6)

  else
    raise "Time.edifact: Format #{fmt} not supported - sorry"
  end
  dtm.format = fmt.to_s
  dtm
end

Instance Method Details

#to_s(*p) ⇒ Object


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/edi4r/edifact.rb', line 200

def to_s(*p)
  return to_s_orig(*p) unless @format
  case @format.to_s
  when '101'
    "%02d%02d%02d" % [year % 100, mon, day]
  when '102'
    "%04d%02d%02d" % [year, mon, day]
  when '201'
    "%02d%02d%02d%02d%02d" % [year % 100, mon, day, hour, min]
  when '203'
    "%04d%02d%02d%02d%02d" % [year, mon, day, hour, min]
  when '204'
    "%04d%02d%02d%02d%02d%2d" % [year, mon, day, hour, min, sec]
  else # Should never occur
    raise "Time.edifact: Format #{format
} not supported - sorry"
  end
end

#to_s_origObject


198
# File 'lib/edi4r/edifact.rb', line 198

alias to_s_orig to_s