Class: EDI::Time

Inherits:
Time
  • Object
show all
Defined in:
lib/edi4r.rb,
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

Constant Summary collapse

@@to_s_callbacks =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#formatObject

Returns the value of attribute format.



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

def format
  @format
end

Class Method Details

.edifact(str, fmt = 102) ⇒ Object



127
128
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
# File 'lib/edi4r/edifact.rb', line 127

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 = EDI::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 = EDI::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 = EDI::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 = EDI::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 = EDI::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_sObject



163
164
165
166
167
168
169
170
# File 'lib/edi4r.rb', line 163

def to_s
  return to_s_orig unless @format
  str = nil
  @@to_s_callbacks.each do |sym|
	return str if (str=self.send(sym)) # Found if not nil
  end
  raise "EDI::Time: Format '#{format}' not supported" 
end

#to_s_edifactObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/edi4r/edifact.rb', line 166

def to_s_edifact
  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
	nil # nil indicates that there was no matching format
  end
end

#to_s_origObject



161
# File 'lib/edi4r.rb', line 161

alias to_s_orig to_s