Class: Reckon::DateColumn

Inherits:
Array
  • Object
show all
Defined in:
lib/reckon/money.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arr = [], options = {}) ⇒ DateColumn

Returns a new instance of DateColumn.



118
119
120
121
122
123
124
125
126
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
# File 'lib/reckon/money.rb', line 118

def initialize( arr = [], options = {} )
  arr.each do |value|
    if options[:date_format]
      begin
        value = Date.strptime(value, options[:date_format])
      rescue
        puts "I'm having trouble parsing #{value} with the desired format: #{options[:date_format]}"
        exit 1
      end
    else
      value = [$1, $2, $3].join("/") if value =~ /^(\d{4})(\d{2})(\d{2})\d+\[\d+\:GMT\]$/ # chase format
      value = [$3, $2, $1].join("/") if value =~ /^(\d{2})\.(\d{2})\.(\d{4})$/            # german format
      value = [$3, $2, $1].join("/") if value =~ /^(\d{2})\-(\d{2})\-(\d{4})$/            # nordea format
      value = [$1, $2, $3].join("/") if value =~ /^(\d{4})\-(\d{2})\-(\d{2})$/            # yyyy-mm-dd format
      value = [$1, $2, $3].join("/") if value =~ /^(\d{4})(\d{2})(\d{2})/                 # yyyymmdd format


      unless @endian_precedence # Try to detect endian_precedence
        reg_match = value.match( /^(\d\d)\/(\d\d)\/\d\d\d?\d?/ )
        # If first one is not \d\d/\d\d/\d\d\d?\d set it to default
        if !reg_match
          @endian_precedence = [:middle, :little]
        elsif reg_match[1].to_i > 12
          @endian_precedence = [:little]
        elsif reg_match[2].to_i > 12
          @endian_precedence = [:middle]
        end
      end
    end
    self.push( value )
  end
  # if endian_precedence still nil, raise error
  unless @endian_precedence || options[:date_format]
    raise( "Unable to determine date format. Please specify using --date-format" )
  end
end

Instance Attribute Details

#endian_precedenceObject

Returns the value of attribute endian_precedence.



117
118
119
# File 'lib/reckon/money.rb', line 117

def endian_precedence
  @endian_precedence
end

Instance Method Details

#for(index) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/reckon/money.rb', line 155

def for( index )
  value = self.at( index )
  guess = Chronic.parse(value, :context => :past,
                        :endian_precedence => @endian_precedence )
  if guess.to_i < 953236800 && value =~ /\//
    guess = Chronic.parse((value.split("/")[0...-1] + [(2000 + value.split("/").last.to_i).to_s]).join("/"), :context => :past,
                          :endian_precedence => @endian_precedence)
  end
  guess
end

#pretty_for(index) ⇒ Object



166
167
168
# File 'lib/reckon/money.rb', line 166

def pretty_for(index)
  self.for(index).strftime("%Y/%m/%d")
end