Class: Surveymonkey::DateString

Inherits:
Object
  • Object
show all
Defined in:
lib/surveymonkey/datestring.rb

Constant Summary collapse

TimelinessFormat =

and have a specific format

'yyyy-mm-dd hh:nn:ss'
TimeFormat =

but for stringification we have to use strftime format

'%Y-%m-%d %H:%M:%S'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datestring, args = {}) ⇒ DateString



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/surveymonkey/datestring.rb', line 27

def initialize(datestring, args = {})
  begin
    $log.debug(sprintf("%s: parsing '%s'", __method__, datestring))

    @raw = datestring

    timeliness_args = { :format => TimelinessFormat }

    # merge additional args if provided
    begin
      timeliness_args.merge(args)
    rescue TypeError => e
      $log.error(sprintf("%s: '%s' (%s) is not a valid arguments hash", __method__, args.inspect, args.class))
    end

    parsed = Timeliness.parse(datestring, timeliness_args)

    if parsed.nil?
      # add a time component and try again
      $log.info(sprintf("%s: '%s' cannot be parsed as a datetime, adding a time component", __method__, datestring))
      datestring.concat(' 00:00:00')
      parsed = Timeliness.parse(datestring, timeliness_args)
    end

    if parsed.nil?
      raise StandardError, sprintf("'%s' is not a valid DateString", datestring)
    else
      @time = parsed
    end

  rescue StandardError => e
    $log.error(sprintf("%s: unable to parse '%s' as DateString", __method__, datestring))
    raise e
  end
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



7
8
9
# File 'lib/surveymonkey/datestring.rb', line 7

def raw
  @raw
end

#timeObject (readonly)

Returns the value of attribute time.



8
9
10
# File 'lib/surveymonkey/datestring.rb', line 8

def time
  @time
end

Instance Method Details

#<=>(other) ⇒ Object



23
24
25
# File 'lib/surveymonkey/datestring.rb', line 23

def <=>(other)
  self.time.<=>(other)
end

#to_sObject



19
20
21
# File 'lib/surveymonkey/datestring.rb', line 19

def to_s
  self.time.strftime(TimeFormat)
end