Sha256: 1f08f73520b0c8fb60cfd3a0dda6005941c7939ed65523e0e8b41bbd85e742dd

Contents?: true

Size: 1.65 KB

Versions: 5

Compression:

Stored size: 1.65 KB

Contents

require "timeliness"

require "surveymonkey/logging"

module Surveymonkey
  class DateString
    attr_reader :raw
    attr_reader :time

    # SurveyMonkey DateStrings are always in UTC
    Timeliness.default_timezone = :utc

    # and have a specific format
    TimelinessFormat = 'yyyy-mm-dd hh:nn:ss'

    # but for stringification we have to use strftime format
    TimeFormat = '%Y-%m-%d %H:%M:%S'

    def to_s
      self.time.strftime(TimeFormat)
    end

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

    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
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
surveymonkey-1.0.0 lib/surveymonkey/datestring.rb
surveymonkey-0.6.0 lib/surveymonkey/datestring.rb
surveymonkey-0.5.0 lib/surveymonkey/datestring.rb
surveymonkey-0.4.4 lib/surveymonkey/datestring.rb
surveymonkey-0.4.3 lib/surveymonkey/datestring.rb