Module: Matiox::Initializer::Default

Included in:
Matiox
Defined in:
lib/matiox/initializers/default.rb

Instance Method Summary collapse

Instance Method Details

#initialize(date = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/matiox/initializers/default.rb', line 5

def initialize(date=nil)

  case date

  when DateTime

    @gregorian = date

  when Time

    seconds = date.sec + Rational(date.usec, 10**6)
    offset = Rational(date.utc_offset, 60 * 60 * 24)
    @gregorian = DateTime.new(
      date.year,
      date.month,
      date.day,
      date.hour,
      date.min,
      seconds,
      offset
    )


  #de TODO: Move these into their own self.from_ methods and invoke in general initializer here:
  #de TODO: Use Regular expressions?
  when String

    begin
      @gregorian = DateTime.strptime(date, '%Y-%m-%dT%H:%M:%S%z')
    rescue
      begin
        @gregorian = DateTime.strptime(date, '%Y-%m-%d %H:%M:%S')
      rescue
        begin
          @gregorian = DateTime.strptime(date, '%Y-%m-%d')
        rescue
          raise Matiox::Error::GregorianDate::IncorrectlyFormatted.new("Bad date string: #{date}")
        end
      end
    end

  else
    @gregorian = DateTime.now
  end

end