Class: Surveymonkey::DateString
- Inherits:
-
Object
- Object
- Surveymonkey::DateString
- 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
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#initialize(datestring, args = {}) ⇒ DateString
constructor
A new instance of DateString.
- #to_s ⇒ Object
Constructor Details
#initialize(datestring, args = {}) ⇒ DateString
Returns a new instance of 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
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
7 8 9 |
# File 'lib/surveymonkey/datestring.rb', line 7 def raw @raw end |
#time ⇒ Object (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_s ⇒ Object
19 20 21 |
# File 'lib/surveymonkey/datestring.rb', line 19 def to_s self.time.strftime(TimeFormat) end |