class EDI::Time

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

Basic idea (UN/EDIFACT example):

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

Basic idea:

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

Attributes

format[RW]

Public Class Methods

edifact(str, fmt=102) click to toggle source
# File lib/edi4r/edifact.rb, line 126
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

Public Instance Methods

to_s() click to toggle source
# File lib/edi4r.rb, line 162
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
Also aliased as: to_s_orig
to_s_edifact() click to toggle source
# File lib/edi4r/edifact.rb, line 165
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_orig()
Alias for: to_s